高效的 Maven Plugins

一、 mybatis-generator-maven-plugin

这是个好东西,实体类、mapper.java、 mapper.xml 文件,再也不用去一个个的手写了,只需动下小手手,轻轻一点,手起刀落,自动生成

第一步: pom.xml配置

  <plugin>
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-maven-plugin</artifactId>
        <version>1.3.2</version>
        <configuration>
           <!--这里的路径要和下面的generatorConfig.xml 文件路径一致--> <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
            <overwrite>true</overwrite>
            <verbose>true</verbose>
        </configuration>
        <dependencies>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.41</version>
            </dependency>
            <dependency>
                <groupId>tk.mybatis</groupId>
                <artifactId>mapper</artifactId>
                <version>3.4.0</version>
            </dependency>
        </dependencies>
</plugin>

第二步: 创建generatorConfig.xml,配置完整的配置信息(耐心看一下,很多地方都可以通用的,标 TODO 的是根据自己的数据库配置需要改动的)

<generatorConfiguration>
    <!-- 配置mysql 驱动jar包路径.用了绝对路径 -->
    <classPathEntry
            location="/users/apple/.m2/repository/mysql/mysql-connector-java/8.0.15/mysql-connector-java-8.0.15.jar"/>

    <context id="theMall" targetRuntime="MyBatis3">
        <property name="beginningDelimiter" value="`"/>
        <property name="endingDelimiter" value="`"/>
        <property name="javaFileEncoding" value="UTF-8"/>
        <!-- 为模型生成序列化方法-->
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>
        <!-- 为生成的Java模型创建一个toString方法 -->
        <plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>

        <!-- 防止生成的代码中有很多注释,加入下面的配置控制 -->
        <commentGenerator>
            <property name="suppressAllComments" value="true"/>
            <property name="suppressDate" value="true"/>
            <property name="addRemarkComments" value="true"/>
        </commentGenerator>

        <!-- TODO 修改数据库连接 -->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://172.0.0.1:3306/db_name?useUnicode=true&amp;characterEncoding=utf-8&amp;serverTimezone=Asia/Shanghai"
                        userId="root"
                        password="123456">
            <property name="nullCatalogMeansCurrent" value="true"/>
        </jdbcConnection>

        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!-- TODO修改数据表对应的Entity层  -->
        <javaModelGenerator targetPackage="com.abc.entity" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <!-- TODO修改 sql dao 映射配置文件 -->
        <sqlMapGenerator targetPackage="mybatis.mapper" targetProject="src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <!-- TODO修改mybatis3中的mapper接口 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.abc.mapper"
                             targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <!-- TODO修改数据表进行生成操作 schema:相当于库名; tableName:表名; domainObjectName:对应的Entity -->
        <table schema="db_name" tableName="table_name"
               domainObjectName="abEntity"
               enableCountByExample="true" enableUpdateByExample="true"
               enableDeleteByExample="false" enableSelectByExample="true"
               selectByExampleQueryId="true">
            <!-- 如果设置为true,生成的model类会直接使用column本身的名字,而不会再使用驼峰命名方法,比如BORN_DATE,生成的属性名字就是BORN_DATE,而不会是bornDate -->
            <property name="useActualColumnNames" value="false"/>
        </table>

    </context>
</generatorConfiguration>

第三步: 执行命令

 在maven -> plugins中找到mybais-generator:generate命令,
 执行,就可以生成数据表对应的entity、mapper.java、mapper.xml 文件了。

二、Spring Boot Maven Plugin

(官网介绍地址:https://docs.spring.io/spring-boot/docs/current/reference/html/build-tool-plugins.html#build-tool-plugins-maven-plugin

首先看一下官方文档的介绍:

The Spring Boot Maven Plugin provides Spring Boot support in Maven, letting you package executable jar or war archives and run an application “in-place”. To use it, you must use Maven 3.2 (or later).

就是说,Spring Boot Maven Plugin 在Maven中提供了Spring Boot支持,使得我们可以使用mvn package 命令,将我们的程序 打包可执行jar或war包并直接运行应用程序,官方文档中也给出我们提示: 要使用它,必须使用Maven 3.2(或更高版本)。

看一下如何引用,很简单的,在 pom 文件中引入插件就可以啦:

<plugins>
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <version>2.2.0.RELEASE</version>
        <executions>
            <execution>
                <goals>
                    <goal>repackage</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
</plugins>

然后在 idea 中运行命令 mvn package,等执行完以后你会发现,target 包下多了一个 xxx.jar (xxx 就是你项目的项目名啦)的 jar 包 。

一般的maven项目的打包命令,不会把依赖的jar包也打包进去的,只是会放在jar包的同目录下,能够引用就可以了,
但是spring-boot-maven-plugin插件,会将依赖的jar包全部打包进去,放在lib目录下。

另外,还提供了一些其他的<goals>支持:
run:运行Spring Boot应用程序。
repackage:将jar / war重新打包为可执行文件。(一般我们项目中都用这个)
start和stop:来管理Spring Boot应用程序的生命周期(即用于集成测试)。
build-info:生成供执行器使用的构建信息。

三、maven-resources-plugin

官网地址:http://maven.apache.org/plugins/maven-resources-plugin/

官网介绍:

  • Resources插件负责处理项目资源文件并拷贝到输出目录。Maven将main resources和test resources分开,
    一般main resources关联main source code,而test resources关联test source code。
    因此,我们可以将main resources和test resources分离。
    从2.3版开始,此插件使用Maven Filtering 共享组件来筛选资源。
  • Resources插件通过<goal></goal>标签输出文件复制到指定目录,其中<goal></goal>有三个可选项:
    • 1.resources:resources,拷贝main resources到main output directory。默认情况下它绑定了process-resources生命周期阶段。
    • 2.resources:testResources,拷贝test resources到test output directory。它绑定了process-test-resources生命周期阶段,当执行surefire:test插件目标前就会执行此阶段。
    • 3.resources:copy-resources,手动拷贝资源到输出目录,因此要配置要复制的资源,并指定outputDirectory。

前两种比较简单,就不贴代码了,给出第三种手动拷贝资源的官方示例:

<build>
    <plugins>
      <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>3.1.0</version>
        <executions>
          <execution>
            <id>copy-resources</id>
            <!-- here the phase you need -->
            <phase>validate</phase>
            <goals>
              <goal>copy-resources</goal>
            </goals>
            <configuration>
              <outputDirectory>${basedir}/target/extra-resources</outputDirectory>
              <resources>          
                <resource>
                  <directory>src/non-packaged-resources</directory>
                  <filtering>true</filtering>
                </resource>
              </resources>              
            </configuration>            
          </execution>
        </executions>
      </plugin>
    </plugins>
    ...
  </build>

其中<phase>validate</phase> 指出在项目 validate 阶段拷贝资源文件,<outputDirectory>标签指出资源文件拷贝到哪里。

默认情况下,Maven会从项目的src/main/resources目录下查找资源。如果你的资源不在此目录下,可以用<resources>标签指定:

  <resources>
     <resource>
       <directory>src/my-resources</directory>
     </resource>
   </resources>

此外,可以通过<resource>标签来添加多个资源目录:

<resources>
     <resource>
       <directory>resource1</directory>
     </resource>
     <resource>
       <directory>resource2</directory>
     </resource>
     <resource>
       <directory>resource3</directory>
     </resource>
   </resources>

四、docker-maven-plugin

官方文档:https://github.com/spotify/docker-maven-plugin#bind-docker-commands-to-maven-phases

作用:

我们可以使用此插件从我们的Maven项目中构建Docker映像。 例如,在项目的构建过程输出运行该服务的Docker映像。

配置:

配置我们的镜像有两种方式,

第一种:
直接在pom中指定要添加到映像中的base image, entry point, cmd, maintainer 和 files ,而无需单独的Dockerfile。

 <plugin>
    <groupId>com.spotify</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>1.2.0</version>
    <configuration>
        <!-- 注意imageName一定要是符合正则[a-z0-9-_.]的,否则构建不会成功 -->
        <!-- 详见:https://github.com/spotify/docker-maven-plugin    Invalid repository name ... only [a-z0-9-_.] are allowed-->
        <imageName>example</imageName>
        <baseImage>java:openjdk-8-jdk-alpine</baseImage>
        <entryPoint>["java", "-Dspring.profiles.active=${spring.profiles.active}", "-Duser.timezone=Asia/Shanghai", "-jar", "/${project.build.finalName}.jar"]
        </entryPoint>
        <resources>
            <resource>
                <targetPath>/</targetPath>
               <directory>${project.build.directory}</directory>
               <include>${project.build.finalName}.jar</include>
            </resource>
        </resources>
    </configuration>
</plugin>

第二种:
如果使用 VOLUME 或其他 Dockerfile 中的命令的时候,需要使用该种方式,创建一个 Dockerfile,并在需要在 POM 中配置 dockerDirectory 来指定路径。

<build>
  <plugins>
    ...
    <plugin>
      <groupId>com.spotify</groupId>
      <artifactId>docker-maven-plugin</artifactId>
      <version>VERSION GOES HERE</version>
      <configuration>
        <imageName>example</imageName>
        <dockerDirectory>docker</dockerDirectory>
        <resources>
           <resource>
             <targetPath>/</targetPath>
             <directory>${project.build.directory}</directory>
             <include>${project.build.finalName}.jar</include>
           </resource>
        </resources>
      </configuration>
    </plugin>
    ...
  </plugins>
</build>
总结:

一个用于构建和推送Docker映像的Maven插件。使用Maven 插件构建Docker 镜像,在<configuration></configuration> 中配置生成镜像的信息,以及生成镜像的时机,
即可在指定的maven生命周期内生成镜像,搭配jenkins使用效果更佳。

五、maven-assembly-plugin

官方文档:http://maven.apache.org/plugins/maven-assembly-plugin/

官方介绍:

Assembly 插件的主要作用是,允许用户将项目输出与它的依赖项、模块、站点文档、和其他文件一起组装成一个可分发的归档文件。

简单说来也是个打包组件,定制化的打包项目,指定打包哪些文件,指定输出地址,打包阶段,指定输出的包类型

贴出一个官方示例吧:

      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.1.1</version>
        <dependencies>
          <dependency>
            <groupId>your.group.id</groupId>
            <artifactId>my-assembly-descriptor</artifactId>
            <version>1.0-SNAPSHOT</version>
          </dependency>
        </dependencies>
        <executions>
          <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
            <configuration>
              <!-- This is where we use our shared assembly descriptor -->
              <descriptorRefs>
                <descriptorRef>myassembly</descriptorRef>
              </descriptorRefs>
            </configuration>
          </execution>
        </executions>
      </plugin>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容