有用的maven插件

摘要:本文介绍maven的几个重要的插件:解压压缩包的插件(maven-dependency-plugin)、复制,删除,移动文件插件(maven-antrun-plugin)、执行任务插件(exec-maven-plugin)、打war包插件(maven-war-plugin)、打jar包插件(maven-jar-plugin)、打源码包插件(maven-source-plugin)。

--------------------------------------------------------解压压缩文件war(maven-dependency-plugin)--------------------------------------------------------------------------

一、解压压缩文件war(maven-dependency-plugin)

<!--解压文件-->
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-dependency-plugin</artifactId>
  <executions>
    <execution>
      <id>unpack</id>
      <phase>process-sources</phase>
      <goals>
        <goal>unpack</goal>
      </goals>
      <configuration>
        <artifactItems>
          <!-- 解压 ${test-artifactId}-${test-version}.war包-->
          <artifactItem>
            <groupId>com.hikvision</groupId>
            <artifactId>${test-artifactId}</artifactId>
            <version>${test-version}</version>
            <type>war</type>
            <classifier>war</classifier>
            <overWrite>true</overWrite>
            <outputDirectory>${resource-webapp}/${test-context}</outputDirectory>
            <includes>**/*.*</includes>
            <excludes>**/maven/,**/lib/</excludes>
          </artifactItem>
          <!-- 解压${test1-artifactId}-${test1-version}.war包-->
          <artifactItem>
            <groupId>com.hikvision</groupId>
            <artifactId>${test1-artifactId}</artifactId>
            <version>${test1-version}</version>
            <type>war</type>
            <overWrite>true</overWrite>
            <outputDirectory>${resource-webapp}/${test1-context}</outputDirectory>
            <includes>**/*.*</includes>
            <excludes>**/maven/,**/lib/</excludes>
          </artifactItem>
        </artifactItems>
      </configuration>
    </execution>
  </executions>
</plugin>

---------------------------------------------------------------------复制,删除,移动文件插件(maven-antrun-plugin)----------------------------------------------------------

二、复制,删除,移动文件插件(maven-antrun-plugin)

<!--拷贝文件-->
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-antrun-plugin</artifactId>
  <version>1.8</version>
  <executions>
    <execution>
      <id>copy</id>
      <phase>generate-resources</phase>
      <goals>
        <goal>run</goal>
      </goals>
      <configuration>
        <tasks>
          <!-- 复制 -->
          <copy todir="${test-webapp}/META-INF" overwrite="true">
            <fileset dir="${test-webapp}/${test-context}/META-INF" />
            <fileset dir="${test1-webapp}/${test1-context}/META-INF" />
          </copy>

          <delete dir="${test-webapp}/${test-context}/META-INF"/>
          <move file="${test-webapp}/${test-context}/WEB-INF/classes/version.properties" tofile="${test-webapp}/WEB-INF/classes/version/test.properties"/>
          <move todir="${resource-webapp}/${br-context}">
            <fileset dir="${resource-webapp}/${br-context}/${br-context}"/>
          </move>
          <copyfile src="${test-webapp}/${test-context}/fav.ico" dest="${test-webapp}/fav.ico"/>
        </tasks>
      </configuration>
    </execution>
  </executions>
</plugin>

---------------------------------------------------------------------------执行任务插件(exec-maven-plugin)-------------------------------------------------------------------------

三、执行任务插件(exec-maven-plugin)

说明:maven构建过程中,有时候需要执行一些脚本或者程序达到我们的特殊目的,执行任务插件(exec-maven-plugin)就是做这个用的。

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>exec-maven-plugin</artifactId>
  <version>1.1.1</version>
  <executions>
    <execution>
      <id>genVersion</id>
      <phase>compile</phase>
      <goals>
        <goal>java</goal>
      </goals>
      <configuration>
        <mainClass>com.test.core.utils.svn.SVNVersionBuild</mainClass>
        <arguments>
          <argument>test</argument>
          <argument>8.2.0</argument>
        </arguments>
      </configuration>
    </execution>
  </executions>
</plugin>

备注:com.test.core.utils.svn.SVNVersionBuild是一个java类,该类有main方法,同时该main方法需要两个参数。

---------------------------------------------------------------------------打war包插件(maven-war-plugin)-------------------------------------------------------------------------------

四、打war包插件(maven-war-plugin)

<pluginManagement>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-war-plugin</artifactId>
      <version>2.3</version>
    </plugin>
  </plugins>
</pluginManagement>

备注:默认打出来的war包名字为${artifactId}.war。参见文章:http://blog.csdn.net/zgmzyr/article/details/8691470

-----------------------------------------------------------------打jar包插件(maven-jar-plugin)--------------------------------------------------------------------------------

五、打jar包插件(maven-jar-plugin)

说明:有时候我们需要将一个maven工程打成一个war包,同时需要将一部分代码以及资源打到一个jar包里面供其它项目使用,打jar包插件(maven-jar-plugin)就是做这个用的。

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
  <executions>
    <execution>
      <id>classJar</id>
      <phase>package</phase>
      <goals>
        <goal>jar</goal>
      </goals>
      <configuration>
        <classifier>classes</classifier>
        <excludes>
          <exclude>struts.xml</exclude>
        </excludes>
      </configuration>
    </execution>
  </executions>
</plugin>

备注:打出来的jar包名字为${artifactId}-${version}-${classifier}.jar

-----------------------------------------------------------------------打源码包插件(maven-source-plugin)----------------------------------------------------------------------------------

六、打源码包插件(maven-source-plugin)

说明:maven工程默认打出来的包里面只有class文件,有时候为了调试项目的需要,我们需要打出一个jar包带有java源文件,打源码包插件(maven-source-plugin)就是做这个用的。

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-source-plugin</artifactId>
  <executions>
    <execution>
      <id>attach-sources</id>
      <goals>
        <goal>jar</goal>
      </goals>
    </execution>
  </executions>
</plugin>

备注:打出来的包名字为${artifactId}-${version}-sources.jar

-------------------------------------------------------------------findbugs-maven-plugin--------------------------------------------------------------------------------------------------

七、findbugs-maven-plugin插件

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>findbugs-maven-plugin</artifactId>
  <version>2.3.2</version>
  <configuration>
    <excludeFilterFile>tools/findbugs/findbugs-exclude.xml</excludeFilterFile>
    <threshold>High</threshold>
    <effort>Default</effort>
  </configuration>
</plugin>

运行findbugs任务前请先运行“mvn package”编译工程。

1、mvn findbugs:help       查看findbugs插件的帮助  

2、mvn findbugs:check      检查代码是否通过findbugs检查,如果没有通过检查,检查会失败,但检查不会生成结果报表  

3、mvn findbugs:findbugs   检查代码是否通过findbugs检查,如果没有通过检查,检查不会失败,会生成结果报表保存在target/findbugsXml.xml文件中  

4、mvn findbugs:gui        检查代码并启动gui界面来查看结果  

可以提高findbugs检查规则文件来使用用户自己的规则。

<configuration>  

  <excludeFilterFile>findbugs-exclude.xml</excludeFilterFile>  

  <includeFilterFile>findbugs-include.xml</includeFilterFile>  

</configuration>  

具体findbugs插件的配置项可以参考:http://mojo.codehaus.org/findbugs-maven-plugin/findbugs-mojo.html

备注:findbugs与maven兼容性

maven V3.0.4与findbugs V2.3.2兼容,同时findbugs插件依赖于groovy-all这个jar包,版本为V2.3.5,配置如下:

<dependency>
  <groupId>org.codehaus.groovy</groupId>
  <artifactId>groovy-all</artifactId>
  <version>2.3.5</version>
</dependency>

运行:findbugs:findbugs会生成findbugsXml.xml这个文件。

-------------------------------------------------------------build-helper-maven-plugin配置多个source resource文件----------------------------------

八、build-helper-maven-plugin插件

1、maven默认的源文件夹及资源文件夹的配置代码

<sourceDirectory > src/main/java </sourceDirectory >

<testSourceDirectory > src/test/java </testSourceDirectory >

 <resources>

   <resource>

    <directory>src/main/resources<directory>

  </resource>

</resources>

<testResources>

  <testResource>

    <directory>src/test/resources<directory>

  </testResource>

</testResources>

2、配置多个源文件夹

从上面Maven的默认配置来看,原文件夹和测试源文件夹都只能配置一个,要配置多个源文件夹比较困难。这里我们先展示如何配置多个资源文件夹。

 <resources>

   <resource>

    <directory>src/main/resources<directory>

  </resource>

   <resource>

    <directory>src/labs/resources<directory>

  </resource>

</resources>

配置好以后,IDE(eclipse)不会识别我们的配置,我们需要更新项目的配置:右击工程->Maven->update Project Configuration,更新后,在eclipse下项目资源文件夹src/labs/resources会展示成资源包。

有人会认为,在Eclipse下将这些文件夹都设置为源文件夹不就OK了吗?其实不然,Maven在构建的时候并不会去

读取任何IDE的配置信息,所以他不会知道我们再eclipse下指定的源文件夹,也不会将这些源文件夹下的代码编译打包。

3、配置读取源文件夹里面的资源文件

为了让Maven能够从元文件夹下读取资源文件(或将所有资源文件配置到源文件夹下),我们可以这样配置:

       < resources >
           < resource >
              < directory > src/main/resources </ directory >
           </ resource >
           < resource >
              < directory > src/labs/resources </ directory >
           </ resource >
           < resource >
              < directory > src/main/java </ directory >
              < includes >
                  < include > **/*.hbm.xml </ include >
              </ includes >
           </ resource >
           < resource >
              < directory > src/labs/java </ directory >
              < includes >
                  < include > **/*.hbm.xml </ include >
              </ includes >
           </ resource >
       </ resources >
这样,不仅 src/main/resources 和 src/labs/resources 这两个目录下的文件会作为资源文件被打包, src/main/java 和 src/labs/java 目录下的所有 .hbm.xml 文件也都会作为资源文件被打包。否则,Maven 构建时不会打包这两个目录下的 .hbm.xml 文件,导致运行时因为找不到资源文件而抛异常。
4、配置多个源文件夹
为了在Maven构建生命周期内为项目添加源文件夹:
<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>build-helper-maven-plugin</artifactId>
  <version>1.4</version>
  <executions>
     <execution>
        <id>add-source</id>
	 <phase>generate-sources</phase>
	 <goals>
	   <goal>add-source</goal>
      </goals>
      <configuration>
	   <sources>
	     <source>${basedir}/src/labs/java</source>
	     <!-- 我们可以通过在这里添加多个source节点,来添加任意多个源文件夹 -->
	    </sources>
      </configuration>
  </execution>                                                          
还可以加resources:                                
<execution>   <id>add-resource</id>   <phase>generate-sources</phase>   <goals>     <goal>add-test-resource</goal>   </goals>   <configuration>     <resources>      < resource >   < directory >${basedir} src/labs/resource </ directory > < /resource > </resources>   </configuration> </execution>
更新项目配置,就可以在eclipse中展现出正常的目录了。

-------------------------------------------------------------maven-surefire-plugin(测试用的maven插件)---------------------------------------

九、maven-surefire-plugin(测试用的maven插件)

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <configuration>
    <argLine>-Xms512m -Xmx2048m -XX:PermSize=256m -XX:MaxPermSize=512m -Dfile.encoding=utf-8</argLine>
    <includes>
      <include>**/*TestSuite.java</include>
    </includes>
    <excludes>
      <exculde>**/*Test.java</exculde>
    </excludes>
  </configuration>
</plugin>

备注:

Maven运行测试用例时,是通过调用maven的surefire插件并fork一个子进程来执行用例的。forkmode属性中指明是要为每个测试创建一个进程,还是所有测试在同一个进程中完成。

forkMode 可设置值有 “never”, “once”, “always” 和 “pertest”。

pretest: 每一个测试创建一个新进程,为每个测试创建新的JVM是单独测试的最彻底方式,但也是最慢的,不适合hudson上持续回归。
once:在一个进程中进行所有测试。once为默认设置,在Hudson上持续回归时建议使用默认设置。
always:在一个进程中并行的运行脚本,Junit4.7以上版本才可以使用,surefire的版本要在2.6以上提供这个功能,其中 threadCount:执行时,指定可分配的线程数量。只和参数parallel配合使用有效。默认:5。

<parallel>methods</parallel>  

<threadCount>4</threadCount></span> 

surefire里还有其它一些有趣的参数,如果有兴趣,你可以访问
http://maven.apache.org/plugins/maven-surefire-plugin/examples/inclusion-exclusion.html 来了解更多信息。

----------------------------------------------------------------maven-compiler-plugin----------------------------------------------------------------------------------------------------------

十、maven-compiler-plugin

用了一段时间maven了,但命令行跑maven命令还比较少,这一跑,还有好多问题。使用maven-compiler-plugin指定JDK版本和编码,才解决

mvn compile的错误。

compiler插件能解决: 
1:maven 2.1默认用jdk 1.3来编译,maven 3 貌似是用jdk 1.5,如果项目用的jdk 1.6也会有问题,compiler插件可以指定JDK版本为1.6。 
2:windows默认使用GBK编码,java项目经常编码为utf8,也需要在compiler插件中指出,否则中文乱码可能会出现编译错误。 
如:
未结束的字符串字面值
需要 ';'
非法的表达式开始
需要 ';'
 
pom增加插件配置
    <plugins> 
         <plugin> 
            <groupId>org.apache.maven.plugins</groupId> 
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version> 
            <configuration> 
                <source>1.6</source> 
                <target>1.6</target> 
                <encoding>UTF8</encoding> 
            </configuration> 
         </plugin> 
    </plugins>
然后mvn compile就没有以上错误了。
 
      idea增加maven支持很简单,在module上右键--Add framework support,选上maven就可以了。还有idea中,依赖的scope如果是test的话,如果测试代码写在main/java下也会出错,确实对junit的依赖。idea的源码文件夹中直接有测试源码文件夹这种类型,测试类不能随便放。而eclipse则没有这个限制。 
maven-compiler-plugin还可以更改编译jdk
<profiles>
    [...]
    <profile>
      <id>compiler</id>
        <properties>
          <JAVA_1_4_HOME>C:Program FilesJavaj2sdk1.4.2_09</JAVA_1_4_HOME>
        </properties>
    </profile>
  </profiles>

dependencyManagement

pluginExecutionFilter

cobertura-maven-plugin

maven-assembly-plugin

原文地址:https://www.cnblogs.com/man-li/p/6407383.html