Maven常用插件

一、基础知识

用户可以通过两种方式调用Maven插件目标。

第一种方式是将插件目标与生命周期阶段(lifecycle phase)绑定,这样用户在命令行只是输入生命周期阶段而已,例如Maven默认将maven-compiler-plugin的compile目标与compile生命周期阶段绑定,因此命令mvn compile实际上是先定位到compile这一生命周期阶段,然后再根据绑定关系调用maven-compiler-plugin的compile目标。

第二种方式是直接在命令行指定要执行的插件目标,例如mvn archetype:generate 就表示调用maven-archetype-plugin的generate目标,这种带冒号的调用方式与生命周期无关。

插件目标(goal)可以附着在生命周期阶段上。随着Maven沿着生命周期的阶段移动,它会执行附着在特定阶段上的目标。每个阶段可能绑定了零个或者多个目标。


二、org.apache.maven.plugins:maven-antrun-plugin

查看介绍

mvn help:describe -Dplugin=antrun -Ddetail

mvn help:describe -Dplugin=antrun -Dgoal=run -Ddetail

例子:maven-antrun-plugin_pom.xml

[html]  view plain copy
  1. <project>      
  2.     <build>  
  3.         <plugins>  
  4.             <plugin>  
  5.                 <groupId>org.apache.maven.plugins</groupId>  
  6.                 <artifactId>maven-antrun-plugin</artifactId>  
  7.                 <version>1.7</version>  
  8.                 <executions>  
  9.                     <execution>  
  10.                         <!-- 默认执行的ID -->  
  11.                         <id>default-cli</id>  
  12.                         <configuration>  
  13.                             <target>  
  14.                                 <property name="compile_classpath" refid="maven.compile.classpath" />  
  15.                                 <echo message="compile classpath: ${compile_classpath}" />  
  16.                                 <echo message="run SecureCRT.exe" />  
  17.                             </target>  
  18.                         </configuration>  
  19.                         <goals>  
  20.                             <goal>run</goal>  
  21.                         </goals>  
  22.                     </execution>  
  23.                     <execution>  
  24.                         <id>clean</id>  
  25.                         <phase>clean</phase>  
  26.                         <configuration>  
  27.                             <target>  
  28.                                 <echo message="delete file;delete dir" />  
  29.                                 <!-- 删除文件 -->  
  30.                                 <delete file="d:\aa.txt"></delete>  
  31.                                 <!-- 删除文件夹 -->  
  32.                                 <delete dir="d:\test"></delete>  
  33.                             </target>  
  34.                         </configuration>  
  35.                         <goals>  
  36.                             <goal>run</goal>  
  37.                         </goals>  
  38.                     </execution>  
  39.                     <execution>  
  40.                         <id>copy</id>  
  41.                         <phase>generate-resources</phase>  
  42.                         <configuration>  
  43.                             <target>  
  44.                                 <echo message="corp file;copy dir" />  
  45.                                 <!-- 拷贝文件,选择是否overwrite -->  
  46.                                 <copy tofile="D:\aa.txt" file="D:\bb.txt" overwrite="true"></copy>  
  47.                                 <!-- 拷贝文件夹,将test1、test2文件夹中的内容拷贝到test文件夹中 -->  
  48.                                 <copy todir="d:\test">  
  49.                                     <fileset dir="d:\test1"></fileset>  
  50.                                     <fileset dir="d:\test2"></fileset>  
  51.                                 </copy>  
  52.                             </target>  
  53.                         </configuration>  
  54.                         <goals>  
  55.                             <goal>run</goal>  
  56.                         </goals>  
  57.                     </execution>  
  58.                     <execution>  
  59.                         <id>execute</id>  
  60.                         <phase>package</phase>  
  61.                         <configuration>  
  62.                             <target>  
  63.                                 <echo message="run SecureCRT.exe" />  
  64.                                 <!-- 执行exe -->  
  65.                                 <exec executable="H:\develop\soft\SecureCRT\SecureCRT.exe"/>  
  66.                                 <echo message="run ipconfig"/>  
  67.                                 <!-- 执行cmd命令 -->  
  68.                                 <exec executable="cmd">  
  69.                                     <arg value="/c ipconfig"></arg>  
  70.                                 </exec>  
  71.                                 <!-- 运行bat脚本,并传递参数 -->  
  72.                                 <exec executable="cmd" dir="xxx">  
  73.                                     <arg value="/C"></arg>  
  74.                                     <arg value="d:/signtool.bat"></arg>  
  75.                                     <arg value="d:/aa.txt"></arg>  
  76.                                 </exec>  
  77.                             </target>  
  78.                         </configuration>  
  79.                         <goals>  
  80.                             <goal>run</goal>  
  81.                         </goals>  
  82.                     </execution>  
  83.                 </executions>  
  84.             </plugin>  
  85.         </plugins>  
  86.     </build>  
  87. </project>  


(1)mvn -f maven-antrun-plugin_pom.xml antrun:run

[plain]  view plain copy
  1. [INFO] --- maven-antrun-plugin:1.7:run (default-cli) @ maven-antrun-plugin ---  
  2. [INFO] Executing tasks  
  3.   
  4. main:  
  5.      [echo] compile classpath: H:\develop\workspace\learn_workspace\maven-plugin  
  6. \target\classes  
  7. [INFO] Executed tasks  

(2)mvn -f maven-antrun-plugin_pom.xml clean

(3)mvn -f maven-antrun-plugin_pom.xml generate-resources

(4)mvn -f maven-antrun-plugin_pom.xml package


三、org.codehaus.mojo:wagon-maven-plugin

http://maven.apache.org/wagon/

文件的上传下载,执行ftp,sshd服务

查看介绍:mvn help:describe -Dplugin=wagon

This plugin has 9 goals

wagon:copy wagon:download wagon:download-single wagon:exist wagon:help wagon:list wagon:merge-maven-repos wagon:upload-single

例子:通过sshd服务将本地windows环境的文件scp(Secure Copy)到linux服务器上

wagon-maven-plugin_pom.xml

[html]  view plain copy
  1. <properties>  
  2.     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  3.     <serverId>linuxServer</serverId>  
  4.     <serverUrl>xx.xx.xx.xx</serverUrl>  
  5.     <serverToTempDir>/usr/local/test</serverToTempDir>  
  6. </properties>  
  7.   
  8. <build>  
  9.     <plugins>  
  10.         <plugin>  
  11.             <groupId>org.codehaus.mojo</groupId>  
  12.             <artifactId>wagon-maven-plugin</artifactId>  
  13.             <version>1.0-beta-4</version>  
  14.             <executions>  
  15.                 <execution>  
  16.                     <id>default-cli</id>  
  17.                     <phase>install</phase>  
  18.                     <goals>  
  19.                         <goal>upload</goal>  
  20.                     </goals>  
  21.                     <configuration>  
  22.                         <serverId>${serverId}</serverId>  
  23.                         <fromDir>${basedir}/src</fromDir>  
  24.                         <url>scp://${serverUrl}</url>  
  25.                         <toDir>${serverToTempDir}</toDir>  
  26.                         <!-- 在mavend的settings.xml中进行配置对应serverId的用户名和密码 -->  
  27.                         <!-- Points to your settings.xml where the connection settings are   
  28.                                     stored as shown below  
  29.                             <server>  
  30.                                 <id>p2Repo</id>  
  31.                                 <username>tomcat</username>  
  32.                                 <password>u1m9s0k8@gfb</password>  
  33.                             </server> -->  
  34.                     </configuration>  
  35.                 </execution>  
  36.             </executions>  
  37.             <dependencies>  
  38.                 <dependency>  
  39.                                                <!-- 如果使用sshd服务,需要依赖wagon-ssh插件 -->  
  40.                                                <groupId>org.apache.maven.wagon</groupId>  
  41.                     <artifactId>wagon-ssh</artifactId>  
  42.                     <version>1.0-beta-6</version>  
  43.                 </dependency>  
  44.             </dependencies>  
  45.         </plugin>  
  46.     </plugins>  
  47. </build>  

settings.xml

[html]  view plain copy
  1. <server>  
  2.       <id>linuxServer</id>  
  3.       <username>root</username>  
  4.       <password>xxxxxx</password>  
  5.  </server>  

运行:mvn -f wagon-maven-plugin_pom.xml wagon:upload

第一次运行时,会提示:

[html]  view plain copy
  1. The authenticity of host '42.96.187.171' can't be established.  
  2. RSA key fingerprint is 6b:d6:f0:c5:df:4c:c1:ec:ac:76:71:a9:0f:f0:86:4b.  
  3. Are you sure you want to continue connecting? (yes/no): yes  
在提示信息后输入yes。

没有找到是否可以设置默认选择yes的方式。如果需要在hudson环境执行,需要先手工在命令行执行一次。


四、org.apache.maven.plugins:maven-jarsigner-plugin

查看介绍:

mvn help:describe -Dplugin=jarsigner -Ddetail

mvn help:describe -Dplugin=jarsigner -Dgoal=sign -Ddetail

例子:

[html]  view plain copy
  1. <project>  
  2.     <build>  
  3.         <plugins>  
  4.             <plugin>  
  5.                 <groupId>org.apache.maven.plugins</groupId>  
  6.                 <artifactId>maven-jarsigner-plugin</artifactId>  
  7.                 <version>1.2</version>  
  8.                 <executions>  
  9.                     <execution>  
  10.                         <!-- 使用default-cli为ID,通过goal的方式才可以执行 -->  
  11.                         <id>default-cli</id>  
  12.                         <goals>  
  13.                             <goal>sign</goal>  
  14.                         </goals>  
  15.                         <configuration>  
  16.                             <keystore>%JAVA_HOME%/jre/lib/security/cacerts</keystore>  
  17.                             <alias>xxxx</alias>  
  18.                             <storepass>changeit</storepass>  
  19.                             <keypass>xxxx</keypass>  
  20.                             <!-- 方式(1):指定要进行签名jar文件名称 -->  
  21. <!--                             <archive>d:/org.eclipse.epp.mpc.ui_1.1.1.5.jar</archive> -->  
  22.                             <!-- 方式(2):指定对某个文件夹中的jar文件进行签名 -->  
  23.                             <archiveDirectory>d:/test/</archiveDirectory>  
  24.                             <!-- 指定文件夹的同时,可进行过滤,包含哪些jar,也可使用正则表达式过滤 -->  
  25.                             <includes>  
  26.                                 <include>org.eclipse.epp.mpc.ui_1.1.1.5.jar</include>  
  27.                                 <include>*nl_zh*.jar</include>  
  28.                             </includes>  
  29.                         </configuration>  
  30.                     </execution>  
  31.                     <execution>  
  32.                         <!-- 验签 -->  
  33.                         <id>verifyTest</id>  
  34.                         <phase>clean</phase>  
  35.                         <goals>  
  36.                             <goal>verify</goal>  
  37.                         </goals>  
  38.                         <configuration>  
  39.                             <keystore>%JAVA_HOME%/jre/lib/security/cacerts</keystore>  
  40.                             <alias>xxxx</alias>  
  41.                             <storepass>changeit</storepass>  
  42.                             <keypass>xxxx</keypass>  
  43.                             <archive>d:/test/org.eclipse.help.ui_3.5.2.r36_v20100702.jar</archive>  
  44.                         </configuration>  
  45.                     </execution>  
  46.                 </executions>  
  47.             </plugin>  
  48.         </plugins>  
  49.     </build>  
  50. </project>  


(1)执行sign插件签名goal

mvn -f maven-jarsigner-plugin_pom.xml jarsigner:sign -X

(2)在clean生命周期中执行verify

mvn -f maven-jarsigner-plugin_pom.xml clean -X

(3)执行install,也会对当前插件进行签名


五、org.apache.maven.plugins:maven-scm-plugin

介绍:http://maven.apache.org/scm/maven-scm-plugin/index.html

使用该插件可进行与svn的checkin和checkout等操作

mvn help:describe -Dplugin=scm

This plugin has 19 goals

例子:

[html]  view plain copy
  1. <properties>  
  2.     <checkout.basedir>${basedir}/checkout</checkout.basedir>  
  3.     <checkout.username>svnusername</checkout.username>  
  4.     <checkout.password>svnpassword</checkout.username>  
  5.   
  6.     <base.connectionURL>scm:svn:http://svnip:81/svn/repos/testProject</base.connectionURL>  
  7.   
  8. </properties>  
  9.   
  10. <profiles>  
  11.     <profile>  
  12.         <!-- checkout -->  
  13.         <id>checkout</id>  
  14.         <build>  
  15.             <plugins>  
  16.                 <plugin>  
  17.                     <groupId>org.apache.maven.plugins</groupId>  
  18.                     <artifactId>maven-scm-plugin</artifactId>  
  19.                     <inherited>false</inherited>  
  20.                     <executions>  
  21.                         <execution>  
  22.                             <id>core-checkout</id>  
  23.                             <goals>  
  24.                                 <goal>checkout</goal>  
  25.                             </goals>  
  26.                             <phase>validate</phase>  
  27.                             <configuration>  
  28.                                 <scmVersion>HEAD</scmVersion>  
  29.                                 <scmVersionType>revision</scmVersionType>  
  30.                                 <skipCheckoutIfExists>true</skipCheckoutIfExists>  
  31.                                 <checkoutDirectory>  
  32.                                     ${checkout.basedir}/  
  33.                                 </checkoutDirectory>  
  34.                                 <connectionUrl>  
  35.                                     ${base.connectionURL}/  
  36.                                 </connectionUrl>  
  37.                                 <username>${checkout.username}</username>  
  38.                                 <password>${checkout.password}</password>  
  39.                                 <includes>testProject.im/*,testProject.server/*,eclipse_ext/epp1/*,eclipse_ext/epp2/*</includes>  
  40.                             </configuration>  
  41.                         </execution>  
  42.                     </executions>  
  43.                 </plugin>  
  44.             </plugins>  
  45.         </build>  
  46.     </profile>  
  47.     <profile>  
  48.         <!-- update -->  
  49.         <id>update</id>  
  50.         <build>  
  51.             <plugins>  
  52.                 <plugin>  
  53.                     <groupId>org.apache.maven.plugins</groupId>  
  54.                     <artifactId>maven-scm-plugin</artifactId>  
  55.                     <inherited>false</inherited>  
  56.                     <executions>  
  57.                         <execution>  
  58.                             <id>core-update</id>  
  59.                             <goals>  
  60.                                 <goal>update</goal>  
  61.                             </goals>  
  62.                             <phase>validate</phase>  
  63.                             <configuration>  
  64.                                 <basedir>  
  65.                                     ${checkout.basedir}/core  
  66.                                 </basedir>  
  67.                                 <scmVersion>HEAD</scmVersion>  
  68.                                 <scmVersionType>revision</scmVersionType>  
  69.                                 <connectionUrl>  
  70.                                     ${core.connectionURL}  
  71.                                 </connectionUrl>  
  72.                                 <username>${checkout.username}</username>  
  73.                                 <password>${checkout.password}</password>  
  74.                             </configuration>  
  75.                         </execution>  
  76.                     </executions>  
  77.                 </plugin>  
  78.             </plugins>  
  79.         </build>  
  80.     </profile>  
  81. </profiles>  
  82. <scm>  
  83.     <connection>scm:svn:http://svnip:81/svn/repos/testProject</connection>  
  84.     <developerConnection>scm:svn:http://svnip:81/svn/repos/testProject</developerConnection>  
  85.     <url>http://svnip:81/svn/repos/testProject</url>  
  86. </scm>  
执行:

mvn scm:checkout

mvn scm:update

六、org.codehaus.mojo:properties-maven-plugin

介绍:mvn help:describe -Dplugin=properties

在java工程中,存在某个xxx.xml、xxx.properties。。。。资源文件。其中资源文件的参数针对不同的环境(开发、测试、生产)需要使用不同的参数值。

properties-maven-plugin插件实现该需求的方式是:

通过properties-maven-plugin插件读取动态参数的字典数据文件(dev.properties、prod.properties。。。),在maven构建的环节中进行参数的替换。

例:

(1)应用中需要被动态替换参数的资源文件:

src/resources/test.properties

[html]  view plain copy
  1. durl=${url}  

src/resources/test.xml

[html]  view plain copy
  1. <test>  
  2.     <url>${url}</url>  
  3. </test>  
(2)针对不同环境进行构建的参数字典文件

deploy_env/resources/dev.properties

[html]  view plain copy
  1. url=http://www.baidu.com  

deploy_env/resources/prod.properties

[html]  view plain copy
  1. url=http://www.qq.com  

(3)pom文件:properties-maven-plugin.xml

[html]  view plain copy
  1. <properties>  
  2.         <param>dev</param>  
  3.     </properties>  
  4.   
  5.     <build>  
  6.         <resources>  
  7.             <resource>  
  8.                 <directory>src/resources</directory>  
  9.                 <!-- 过滤 -->  
  10.                 <filtering>true</filtering>  
  11.             </resource>  
  12.         </resources>  
  13.         <plugins>  
  14.             <plugin>  
  15.                 <groupId>org.codehaus.mojo</groupId>  
  16.                 <artifactId>properties-maven-plugin</artifactId>  
  17.                 <version>1.0-alpha-1</version>  
  18.                 <executions>  
  19.                     <execution>  
  20.                         <phase>initialize</phase>  
  21.                         <goals>  
  22.                             <goal>read-project-properties</goal>  
  23.                         </goals>  
  24.                         <configuration>  
  25.                             <files>  
  26.                                 <file>deploy_env/resources/${param}.properties</file>  
  27.                             </files>  
  28.                         </configuration>  
  29.                     </execution>  
  30.                 </executions>  
  31.             </plugin>  
  32.         </plugins>  
  33.     </build>  
执行构建命令:

mvn -f properties-maven-plugin.xml install  (默认使用dev.properties文件)

mvn -f properties-maven-plugin.xml install -Dparam=prod

构建后,在工程target文件夹下的test.properties、test.xml文件中的动态参数已被替换

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值