高效的 Maven Plugins

1、 mybatis-generator-maven-plugin

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

第一步: pom.xml配置html

<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 的是根据本身的数据库配置须要改动的)java

<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>

第三步: 执行命令mysql

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

2、Spring Boot Maven Plugin

(官网介绍地址:https://docs.spring.io/spring-boot/docs/current/reference/html/build-tool-plugins.html#build-tool-plugins-maven-plugin)<br> 首先看一下官方文档的介绍:git

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(或更高版本)。github

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

<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 包 。<br> 通常的maven项目的打包命令,不会把依赖的jar包也打包进去的,只是会放在jar包的同目录下,可以引用就能够了, 可是spring-boot-maven-plugin插件,会将依赖的jar包所有打包进去,放在lib目录下。sql

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

3、maven-resources-plugin

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

官网介绍:数据库

  • 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。

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

<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>

4、docker-maven-plugin

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

做用:

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

配置:

配置咱们的镜像有两种方式,<br> 第一种: 直接在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使用效果更佳。

5、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>
相关文章
相关标签/搜索