首页 > 代码库 > maven 常用插件总结

maven 常用插件总结

  • maven-javadoc-plugin

(1) 说明:该插件生成项目的javadoc.对于构建jar目标,javadoc会首先生成并打包放入jar文件中。

(2) 默认用法:

    • pom.xml配置
<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-javadoc-plugin</artifactId>
        <version>2.10.4</version>
        <configuration>
          ...
        </configuration>
      </plugin>
    </plugins>
    ...
  </build>
  ...
</project>
    • 执行命令
mvn javadoc:javadoc
mvn javadoc:jar
mvn javadoc:aggregate
mvn javadoc:aggregate-jar
mvn javadoc:test-javadoc
mvn javadoc:test-jar
mvn javadoc:test-aggregate
mvn javadoc:test-aggregate-jar

 

(3) 扩展配置:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-javadoc-plugin</artifactId>
    <version>2.9</version>
    <executions>
       <execution>
          <id>attach-javadocs</id>
             <goals>
                <goal>jar</goal><!--执行goal时,完成doc附加-->
             </goals>
       </execution>
   </executions>
</plugin>

 

  • maven-source-plugin

(1) 说明:在target目录中生成当前项目的源文件的jar包。

(2) 默认用法:

    • pom.xml配置
<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-source-plugin</artifactId>
   <version>3.0.1</version>
   <executions>
      <execution>
         <id>attach-sources</id>
         <goals>
            <goal>jar</goal>
        </goals>
      </execution>
   </executions>
</plugin>
    • 执行命令:
  • source:aggregate 合并所有模块的源码;
  • source:jar 用于项目主源码的打包归档;
  • source:test-jar 用于项目测试源码的打包归档;
  • source:jar-no-fork 类似于source:jar, 但不会fork进程来构建周期
  • source:test-jar-no-fork 类似于source:test-jar, 但不会fork进程来构建周期。

(3) 扩展配置:

  • 绑定阶段
<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-source-plugin</artifactId>
        <version>3.0.1</version>
        <executions>
          <execution>
            <id>attach-sources</id>
            <phase>verify</phase>
            <goals>
              <goal>jar-no-fork</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  ...
</project>
  • 在profile中使用
<project>
  ...
  <profiles>
    <profile>
      <id>release</id>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <version>3.0.1</version>
            <executions>
              <execution>
                <id>attach-sources</id>
                <goals>
                  <goal>jar-no-fork</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>
  ...
</project>

 

  • maven-compiler-plugin

(1) 说明:指定项目编译使用的jdk。

(2) 默认用法:

    • pom.xml配置
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.5.1</version>
    <configuration>
        <encoding>UTF-8</encoding>
    </configuration>
    <executions>
        <execution>
            <id>default-testCompile</id>
            <phase>test-compile</phase>
            <goals>
                <goal>testCompile</goal>
            </goals>
            <configuration>
                <skip>false</skip>
            </configuration>
        </execution>
    </executions>
</plugin>
    • 执行命令

         compiler:compile 绑定compile阶段,编译main源码

         compiler:testCompile 绑定test-compile阶段,编译test源码

(3) 扩展配置:没有executions 标签。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <configuration>
        <source>1.7</source> <!-- 源代码使用的开发版本 -->
        <target>1.7</target> <!-- 需要生成的目标class文件的编译版本 -->
        <!-- 一般而言,target与source是保持一致的,但是,有时候为了让程序能在其他版本的jdk中运行(对于低版本目标jdk,源代码中需要没有使用低版本jdk中不支持的语法),会存在target不同于source的情况 -->
    
        <!-- 这下面的是可选项 -->
        <meminitial>128m</meminitial>
        <maxmem>512m</maxmem>
        <fork>true</fork> <!-- fork is enable,用于明确表示编译版本配置的可用 --> 
        <compilerVersion>1.3</compilerVersion>
        
        <!-- 这个选项用来传递编译器自身不包含但是却支持的参数选项 -->
        <compilerArgument>-verbose -bootclasspath ${java.home}\lib\rt.jar</compilerArgument>
    </configuration>
</plugin>

 

  • maven-resources-plugin

(1) 说明:该插件处理项目的资源文件拷贝到输出目录。可以分别处理main resources 和 test resources。

(2) 默认用法:

    • pom.xml配置:
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>3.0.1</version>
    <configuration>
          <encoding>UTF-8</encoding>
     </configuration>
 </plugin>

<!--最好先指定过如下属性—>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
...
</properties>
    • 执行命令:

resources:resources 拷贝主源码的资源文件到output目录;这个goals通常自动运行。

通常使用project.build.resources 元素来指定资源,默认拷贝到project.build.outputDirectory指定的目录。

resources:testResources 拷贝测试源码的资源文件到output目录;这个goals通常自动运行。

通常使用project.build.testResources元素来指定测试资源,默认拷贝到project.build.testOutputDirectory目录。

resources:copy-resources 拷贝资源到一个输出目录。这个goals要求将指定的资源拷贝到指定的outputDirectory中。

(3) 扩展配置:

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>3.0.2</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>
  ...
</project>

 

  • maven-surefire-plugin

(1) 说明:在构建期间执行单元测试。产生2种格式的测试案例执行报告:*.txt, *.xml。默认情况下,这些结果文件存放在${basedir}/target/surefire-reports下。

(2) 默认用法:

    • pom.xml配置
<plugins>
    [...]
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.19.1</version>
        <configuration>
          <systemPropertyVariables>
           <!--指定参数,也可以通过testng的@Parameter注解进行指定--> 
            <propertyName>firefox</propertyName>
          </systemPropertyVariables>
        </configuration>
      </plugin>
    [...]
</plugins>
<plugins>
    [...]
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.19.1</version>
        <configuration>
          <!--指定组-->
          <groups>functest,perftest</groups>
        </configuration>
      </plugin>
    [...]
</plugins>
    • 执行命令:
  • surefire:test :运行单元测试案例;
  • mvn -Dmaven.surefire.debug test :debug测试案例(5005端口)
  • mvn –Dmaven.surefire.debug="-Xdebug –Xrunjdwp:transport=dt_socket, server=y,suspend=y,address=8000 –Xnoagent –Djava.compiler=NONE" test :自定义8000端口进行debug
  • mvn –DforkCount=0 test :强制maven不会fork进程执行案例。
  • mvnDebug -DforkCount=0 test :debug maven自身。
 

(3) 扩展配置:

</plugins>
    [...]
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.19.1</version>
        <configuration>
          <!--并行运行参数-->
          <parallel>methods</parallel>
          <threadCount>10</threadCount>
        </configuration>
      </plugin>
    [...]
</plugins>
<plugins>
    [...]
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.19.1</version>
        <configuration>
          <properties>
            <property>
              <name>parallel</name>
              <value>methods</value>
            </property>
            <property>
             <!--使用dataprovider并行运行时配置并发数-->
              <name>dataproviderthreadcount</name>
              <value>30</value>
            </property>
          </properties>
        </configuration>
      </plugin>
    [...]
</plugins>
<plugins>
    [...]
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.19.1</version>
        <configuration>
          <suiteXmlFiles>
            <!—suite并行运行-->
            <file>src/test/resources/testng1.xml</file>
            <file>src/test/resources/testng2.xml</file>
          </suiteXmlFiles>
          <properties>
            <property>
              <name>suitethreadpoolsize</name>
              <value>2</value>
            </property>
          </properties>
        </configuration>
      </plugin>
    [...]
</plugins>
<!--自定义listener 和 reports-->
<dependencies>
[...]
  <dependency>
    <groupId>your-testng-listener-artifact-groupid</groupId>
    <artifactId>your-testng-listener-artifact-artifactid</artifactId>
    <version>your-testng-listener-artifact-version</version>
    <scope>test</scope>
  </dependency>
[...]
</dependencies>
[...]
</plugins>
    [...]
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.19.1</version>
        <configuration>
          <properties>
            <property>
              <name>usedefaultlisteners</name>
              <value>false</value> <!-- disabling default listeners is optional -->
            </property>
            <property>
              <name>listener</name>
              <value>com.mycompany.MyResultListener,com.mycompany.MyAnnotationTransformer,com.mycompany.MyMethodInterceptor</value>
            </property>
            <property>
              <name>reporter</name>
              <value>listenReport.Reporter</value>
            </property>
          </properties>
        </configuration>
      </plugin>
    [...]
</plugins>
<!--用户可以自行实现implements org.testng.ITestListener在your-testng-listener-artifact中,可以使用scope=test或代码在/src/test/java中。在当前surefire-testng provider的类载入器中,可以使用参数dependenciesToScan参数过滤test artifact 来载入它的类。 testng reporter 也必须实现org.testng.IReporter—>
<plugins>
    [...]
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.19.1</version>
        <configuration>
          [...]
          <properties>
            <property>
              <name>surefire.testng.verbose</name>
              <value>10</value>
            </property>
          </properties>
          [...]
        </configuration>
      </plugin>
    [...]
</plugins>
<!--配置日志等级,区间为0-10,10为最详细。-1时testng为debug模式。默认为0—>
<plugins>
    [...]
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.19.1</version>
        <configuration>
          [...]
          <properties>
            <property>
              <name>objectfactory</name>
              <value>testng.objectfactory.TestNGCustomObjectFactory</value>
            </property>
          </properties>
          [...]
        </configuration>
      </plugin>
    [...]
</plugins>
<!--自定义Testng Object Factory:通过实现org.testng.IObjectFactory 和绑定类名到关键字:objectfactory-->
</plugins>
    [...]
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.19.1</version>
        <configuration>
          [...]
          <properties>
            <property>
              <name>testrunfactory</name>
              <value>testng.testrunnerfactory.TestNGCustomTestRunnerFactory</value>
            </property>
          </properties>
          [...]
        </configuration>
      </plugin>
    [...]
</plugins>
<!--自定义TestNG TestRunner Factory: 实现org.testng.ITestRunnerFactory, 绑定类名到关键字testrunfactory—>
<plugins>
    [...]
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.19.1</version>
        <configuration>
          [...]
          <suiteXmlFiles>
            <file>src/test/resources/suite.xml</file>
          </suiteXmlFiles>
          <properties>
            <property>
              <name>testnames</name>
              <value>a-t1,a-t3</value>
            </property>
          </properties>
          [...]
        </configuration>
      </plugin>
    [...]
</plugins>
<!--只运行指定test name 下的案例,此处只运行test 名称为a-t1和a-t3的案例-->
<argLine>-Djava.endorsed.dirs=...</argLine>
<!--指定VM的参数-->

 

  • maven-dependency-plugin

(1) 说明:该插件提供了操作artifact的能力。它能够从本地或远程库拷贝、打包artifact到指定位置。

(2) 默认用法:

    • pom.xml配置
<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>3.0.0</version>
        <executions>
          <execution>
            <id>copy</id>
            <phase>package</phase>
            <goals>
              <goal>copy</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <artifactItems>
            <artifactItem>
              <groupId>junit</groupId>
              <artifactId>junit</artifactId>
              <version>3.8.1</version>
              <type>jar</type>
              <overWrite>false</overWrite>
              <outputDirectory>${project.build.directory}/alternateLocation</outputDirectory>
              <destFileName>optional-new-name.jar</destFileName>
            </artifactItem>
          </artifactItems>
          <outputDirectory>${project.build.directory}/wars</outputDirectory>
          <overWriteReleases>false</overWriteReleases>
          <overWriteSnapshots>true</overWriteSnapshots>
        </configuration>
      </plugin>
    </plugins>
  </build>
  [...]
</project>
<!--在执行mvn package后,junit被拷贝到指定位置—>
<!--artifact处理顺序:当前运行器,本地库,已配置的远程库-->
    • 执行命令
  • dependency:analyze 分析项目中的依赖,确定哪些是已使用已声明、已使用未声明及未使用已声明的依赖。
  • dependency:analyze-dep-mgt 分析项目中的依赖,并列出已解决依赖的和依赖管理中列出的引入依赖的错误匹配。
  • dependency:analyze-only 与analyze相同, 但只约束在一个pom 中. 不再fork进程执行编译和test-compile。
  • dependency:analyze-report 分析项目依赖,生成依赖概述报告,阐明已使用已声明、已使用未声明及未使用已声明的依赖。
  • dependency:analyze-duplicate 分析 <dependencies/> and <dependencyManagement/> 标签,确定声明重复的依赖。
  • dependency:build-classpath 在本地库的classpath中使用java –cp,告诉Maven用来输出的依赖目录。classpath 文件会被附加到main artifact并一起安装。
  • dependency:copy  获取在插件管理器中已定义的artifact列表,拷贝他们到一个指定的位置,必要时重命名或去除版本。这个goal能够解决从远程库获取的artifact在本地库或使用库中不存在的问题。
  • dependency:copy-dependencies 获取项目直接依赖及可选的传递依赖列表,必要时拷贝到指定位置,重命名或去除版本。这个goal可以从命令行运行。 
  • dependency:display-ancestors 显示当前项目的所有POM祖先。当CI中需要了解项目的所有POM时非常有用。这个goals可以从命令行运行。 
  • dependency:get 解决单独的artifact, 甚至是来自远程库的间接引用的依赖。
  • dependency:go-offline 告诉Maven,使用离线模块,解决项目所有依赖的所有(依赖,插件,报告)
  • dependency:list 列出项目的依赖列表
  • dependency:list-repositories 显示所有的项目依赖并列出已使用的。
  • dependency:properties 对文件系统中包含artifact的每一个项目依赖设置一个property。
  • dependency:purge-local-repository 告诉maven 清除非本地库的依赖artifact文件,并重新解决他们。
  • dependency:resolve 告诉maven解决所有的依赖并显示他们的版本。
  • dependency:resolve-plugins 告诉maven解决所有的插件及他们的依赖。
  • dependency:sources 告诉maven解决所有的依赖及他们的源码,显示他们的版本。
  • dependency:tree 显示树状依赖。
  • dependency:unpack 与copy相同,但不打包。
  • dependency:unpack-dependencies 与copy-dependencies,但不打包。

(3) 扩展配置:

<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>3.0.0</version>
        <executions>
          <execution>
            <id>copy-installed</id>
            <phase>install</phase>
            <goals>
              <goal>copy</goal>
            </goals>
            <configuration>
              <artifactItems>
                <artifactItem>
                  <groupId>${project.groupId}</groupId>
                  <artifactId>${project.artifactId}</artifactId>
                  <version>${project.version}</version>
                  <type>${project.packaging}</type>
                </artifactItem>
              </artifactItems>
              <outputDirectory>some-other-place</outputDirectory>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  [...]
</project>
<!--必须绑定package之后的阶段,确保jar包被已生成-->

 

  • maven-assembly-plugin

(1) 说明:该插件允许用户整合项目的输出,包括依赖,模块,网站文档和其他文档到一个单独的文档,即可用定制化打包。

创建的文档格式包括:zip, tar, tar.gz(tgz), gar.bz2(tbgz2), jar, dir,war 等等。四种预定义的描述器可用:bin, jar-with-dependencies, src, project.

<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
  <id>bin</id>
  <formats>
    <format>tar.gz</format>
    <format>tar.bz2</format>
    <format>zip</format>
  </formats>
  <fileSets>
    <fileSet>
      <directory>${project.basedir}</directory>
      <outputDirectory>/</outputDirectory>
      <includes>
        <include>README*</include>
        <include>LICENSE*</include>
        <include>NOTICE*</include>
      </includes>
    </fileSet>
    <fileSet>
      <directory>${project.build.directory}</directory>
      <outputDirectory>/</outputDirectory>
      <includes>
        <include>*.jar</include>
      </includes>
    </fileSet>
    <fileSet>
      <directory>${project.build.directory}/site</directory>
      <outputDirectory>docs</outputDirectory>
    </fileSet>
  </fileSets>
</assembly>
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
  <!-- TODO: a jarjar format would be better -->
  <id>jar-with-dependencies</id>
  <formats>
    <format>jar</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <dependencySets>
    <dependencySet>
      <outputDirectory>/</outputDirectory>
      <useProjectArtifact>true</useProjectArtifact>
      <unpack>true</unpack>
      <scope>runtime</scope>
    </dependencySet>
  </dependencySets>
</assembly>
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
  <id>src</id>
  <formats>
    <format>tar.gz</format>
    <format>tar.bz2</format>
    <format>zip</format>
  </formats>
  <fileSets>
    <fileSet>
      <directory>${project.basedir}</directory>
      <includes>
        <include>README*</include>
        <include>LICENSE*</include>
        <include>NOTICE*</include>
        <include>pom.xml</include>
      </includes>
      <useDefaultExcludes>true</useDefaultExcludes>
    </fileSet>
    <fileSet>
      <directory>${project.basedir}/src</directory>
      <useDefaultExcludes>true</useDefaultExcludes>
    </fileSet>
  </fileSets>
</assembly>
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
  <id>project</id>
  <formats>
    <format>tar.gz</format>
    <format>tar.bz2</format>
    <format>zip</format>
  </formats>
  <fileSets>
    <fileSet>
      <directory>${project.basedir}</directory>
      <outputDirectory>/</outputDirectory>
      <useDefaultExcludes>true</useDefaultExcludes>
      <excludes>
        <exclude>**/*.log</exclude>
        <exclude>**/${project.build.directory}/**</exclude>
      </excludes>
    </fileSet>
  </fileSets>
</assembly>

(2) 默认用法

    • pom.xml配置
<plugin>  
    <groupId>org.apache.maven.plugins</groupId>  
    <artifactId>maven-assembly-plugin</artifactId>  
    <version>2.4</version>  
    <executions>  
        <execution>  
            <phase>package</phase>  
            <goals>  
                <goal>single</goal>  
            </goals>  
        </execution>  
    </executions>  
</plugin>
<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <!-- NOTE: We don‘t need a groupId specification because the group is
             org.apache.maven.plugins ...which is assumed by default.
         -->
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.0.0</version>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
        [...]
</project>
<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.0.0</version>
        <configuration>
          <descriptors>
            <descriptor>src/assembly/src.xml</descriptor>
          </descriptors>
        </configuration>
        [...]
</project>
 
  • 执行命令:
assembly:single

(3) 扩展配置:

<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.0.0</version>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
        <executions>
          <execution>
            <id>make-assembly</id> <!-- this is used for inheritance merges -->
            <phase>package</phase> <!-- bind to the packaging phase -->
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      [...]
</project>
<!--自定义组装描述符-->
<?xml version=‘1.0‘ encoding=‘UTF-8‘?>  
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"  
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0  
                    http://maven.apache.org/xsd/assembly-1.1.0.xsd">  
    <id>demo</id>  
    <formats>  
        <format>jar</format> 
<!--指定打包类型--> 
    </formats>  
    <includeBaseDirectory>false</includeBaseDirectory>
 <!--指定是否包含打包层目录(比如finalName是output,当值为true,所有文件被放在output目录下,否则直接放在包的根目录下)--> 
    <fileSets>  
<!--指定要包含的文件集,可以定义多个fileSet-->
        <fileSet> 
<!--指定要包含的目录--> 
            <directory>${project.build.directory}/classes</directory><!--指定当前要包含的目录的目的地-->  
            <outputDirectory>/</outputDirectory>  
        </fileSet>  
    </fileSets>  
</assembly>
 
<!--使用-->
<configuration>
<finalName>demo</finalName>
<descriptors>
<descriptor>assemblies/demo.xml</descriptor>
</descriptors>
<outputDirectory>output</outputDirectory>
</configuration>

 

  • maven-antrun-plugin

(1) 说明:该插件提供了在maven中运行ant任务的方式。

(2) 默认用法:

    • pom.xml配置
<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.8</version>
        <executions>
          <execution>
            <phase> <!-- a lifecycle phase --> </phase>
            <configuration>
              <target>

                <!--
                  Place any Ant task here. You can add anything
                  you can add between <target> and </target> in a
                  build.xml.
                -->

              </target>
            </configuration>
            <goals>
              <goal>run</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  [...]
</project>
    • 执行命令:

        无

(3) 扩展配置:

...  
<build>  
    <plugins>  
        <plugin>  
            <artifactId>maven-antrun-plugin</artifactId>  
            <executions>  
                <execution>  
                    <id>package</id>  
                    <phase>package</phase>  
                    <goals>  
                        <goal>run</goal>  
                    </goals>  
                    <configuration>  
                        <tasks>  
                            <echo message="make ..."/>  
                            <exec dir="src/main/c" executable="make" failonerror="true" />  
                        </tasks>  
                    </configuration>  
                </execution>  
                <execution>  
                    <id>clean</id>  
                    <phase>clean</phase>  
                    <goals>  
                        <goal>run</goal>  
                    </goals>  
                    <configuration>  
                        <tasks>  
                            <echo message="make clean ..."/>  
                            <exec dir="src/main/c" executable="make" failonerror="true">  
                                <arg line="clean"/>  
                            </exec>  
                        </tasks>  
                    </configuration>  
                </execution>  
            </executions>  
        </plugin>  
    </plugins>  
</build>  
...

 

  • maven-replacer-plugin

(1) 说明:

(2) 默认用法:

    • pom.xml配置
<build>  
  <plugins>  
   ...  
   <plugin>  
    <groupId>com.google.code.maven-replacer-plugin</groupId>  
    <artifactId>replacer</artifactId>  
    <version>1.5.3</version>  
       <executions>  
        ...  
       </executions>  
       <configuration>  
        ...  
       </configuration>  
   </plugin>  
  </plugins>  
 </build>
<configuration> <!--文本替换--> 
    <file>src/test/resources/a.txt</file>  
    <outputFile>src/main/resources/a.txt</outputFile>  
    <regex>false</regex>  
    <token>{book.name}</token>  
    <value>Thinkin in Java</value>  
</configuration>
<!--多个替换-->
                <configuration>
                    <file>src/test/resources/a.txt</file>
                    <outputFile>src/main/resources/a.txt</outputFile>
                    <regex>false</regex>
                    <replacements>
                        <replacement>
                            <token>{author.name}</token>
                            <value>Bruce Eckel </value>
                        </replacement>
                        <replacement>
                            <token>{book.name}</token>
                            <value>Thinkin in Java </value>
                        </replacement>
                    </replacements>
                </configuration>
<!--排除文件-->
                <configuration>
                    <basedir>${basedir}/src/test/resources</basedir>
                    <includes>
                        <include>**/*.txt</include>
                    </includes>
                    <excludes>
                        <exclude>**/a.txt</exclude>
                    </excludes>
                    <outputBasedir>${basedir}/src/main/resources</outputBasedir>
                    <outputDir>.</outputDir>
                    <regex>false</regex>
                    <preserveDir>false</preserveDir>
                    <tokenValueMap>src/test/resources/book.conf</tokenValueMap>
                </configuration>
    • 执行命令:

       无

(3) 扩展配置:

<plugin>
                <groupId>com.google.code.maven-replacer-plugin</groupId>
                <artifactId>replacer</artifactId>
                <version>1.5.2</version>
                <executions>
                    <execution>
                        <phase>generate-test-resources</phase>
                        <goals>
                            <goal>replace</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <file>src/test/resources/prop.properties</file>
                    <regex>true</regex>
                    <token>BrowserCoreType.*</token>
                    <value>BrowserCoreType=${BrowserCoreType}</value>
                </configuration>
            </plugin>

 

参考网址: http://maven.apache.org/plugins

maven 常用插件总结