一、基础知识
用户可以通过两种方式调用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
- <project>
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-antrun-plugin</artifactId>
- <version>1.7</version>
- <executions>
- <execution>
- <!-- 默认执行的ID -->
- <id>default-cli</id>
- <configuration>
- <target>
- <property name="compile_classpath" refid="maven.compile.classpath" />
- <echo message="compile classpath: ${compile_classpath}" />
- <echo message="run SecureCRT.exe" />
- </target>
- </configuration>
- <goals>
- <goal>run</goal>
- </goals>
- </execution>
- <execution>
- <id>clean</id>
- <phase>clean</phase>
- <configuration>
- <target>
- <echo message="delete file;delete dir" />
- <!-- 删除文件 -->
- <delete file="d:\aa.txt"></delete>
- <!-- 删除文件夹 -->
- <delete dir="d:\test"></delete>
- </target>
- </configuration>
- <goals>
- <goal>run</goal>
- </goals>
- </execution>
- <execution>
- <id>copy</id>
- <phase>generate-resources</phase>
- <configuration>
- <target>
- <echo message="corp file;copy dir" />
- <!-- 拷贝文件,选择是否overwrite -->
- <copy tofile="D:\aa.txt" file="D:\bb.txt" overwrite="true"></copy>
- <!-- 拷贝文件夹,将test1、test2文件夹中的内容拷贝到test文件夹中 -->
- <copy todir="d:\test">
- <fileset dir="d:\test1"></fileset>
- <fileset dir="d:\test2"></fileset>
- </copy>
- </target>
- </configuration>
- <goals>
- <goal>run</goal>
- </goals>
- </execution>
- <execution>
- <id>execute</id>
- <phase>package</phase>
- <configuration>
- <target>
- <echo message="run SecureCRT.exe" />
- <!-- 执行exe -->
- <exec executable="H:\develop\soft\SecureCRT\SecureCRT.exe"/>
- <echo message="run ipconfig"/>
- <!-- 执行cmd命令 -->
- <exec executable="cmd">
- <arg value="/c ipconfig"></arg>
- </exec>
- <!-- 运行bat脚本,并传递参数 -->
- <exec executable="cmd" dir="xxx">
- <arg value="/C"></arg>
- <arg value="d:/signtool.bat"></arg>
- <arg value="d:/aa.txt"></arg>
- </exec>
- </target>
- </configuration>
- <goals>
- <goal>run</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </build>
- </project>
(1)mvn -f maven-antrun-plugin_pom.xml antrun:run
- [INFO] --- maven-antrun-plugin:1.7:run (default-cli) @ maven-antrun-plugin ---
- [INFO] Executing tasks
- main:
- [echo] compile classpath: H:\develop\workspace\learn_workspace\maven-plugin
- \target\classes
- [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
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <serverId>linuxServer</serverId>
- <serverUrl>xx.xx.xx.xx</serverUrl>
- <serverToTempDir>/usr/local/test</serverToTempDir>
- </properties>
- <build>
- <plugins>
- <plugin>
- <groupId>org.codehaus.mojo</groupId>
- <artifactId>wagon-maven-plugin</artifactId>
- <version>1.0-beta-4</version>
- <executions>
- <execution>
- <id>default-cli</id>
- <phase>install</phase>
- <goals>
- <goal>upload</goal>
- </goals>
- <configuration>
- <serverId>${serverId}</serverId>
- <fromDir>${basedir}/src</fromDir>
- <url>scp://${serverUrl}</url>
- <toDir>${serverToTempDir}</toDir>
- <!-- 在mavend的settings.xml中进行配置对应serverId的用户名和密码 -->
- <!-- Points to your settings.xml where the connection settings are
- stored as shown below
- <server>
- <id>p2Repo</id>
- <username>tomcat</username>
- <password>u1m9s0k8@gfb</password>
- </server> -->
- </configuration>
- </execution>
- </executions>
- <dependencies>
- <dependency>
- <!-- 如果使用sshd服务,需要依赖wagon-ssh插件 -->
- <groupId>org.apache.maven.wagon</groupId>
- <artifactId>wagon-ssh</artifactId>
- <version>1.0-beta-6</version>
- </dependency>
- </dependencies>
- </plugin>
- </plugins>
- </build>
settings.xml
- <server>
- <id>linuxServer</id>
- <username>root</username>
- <password>xxxxxx</password>
- </server>
运行:mvn -f wagon-maven-plugin_pom.xml wagon:upload
第一次运行时,会提示:
- The authenticity of host '42.96.187.171' can't be established.
- RSA key fingerprint is 6b:d6:f0:c5:df:4c:c1:ec:ac:76:71:a9:0f:f0:86:4b.
- Are you sure you want to continue connecting? (yes/no): yes
没有找到是否可以设置默认选择yes的方式。如果需要在hudson环境执行,需要先手工在命令行执行一次。
四、org.apache.maven.plugins:maven-jarsigner-plugin
查看介绍:
mvn help:describe -Dplugin=jarsigner -Ddetail
mvn help:describe -Dplugin=jarsigner -Dgoal=sign -Ddetail
例子:
- <project>
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-jarsigner-plugin</artifactId>
- <version>1.2</version>
- <executions>
- <execution>
- <!-- 使用default-cli为ID,通过goal的方式才可以执行 -->
- <id>default-cli</id>
- <goals>
- <goal>sign</goal>
- </goals>
- <configuration>
- <keystore>%JAVA_HOME%/jre/lib/security/cacerts</keystore>
- <alias>xxxx</alias>
- <storepass>changeit</storepass>
- <keypass>xxxx</keypass>
- <!-- 方式(1):指定要进行签名jar文件名称 -->
- <!-- <archive>d:/org.eclipse.epp.mpc.ui_1.1.1.5.jar</archive> -->
- <!-- 方式(2):指定对某个文件夹中的jar文件进行签名 -->
- <archiveDirectory>d:/test/</archiveDirectory>
- <!-- 指定文件夹的同时,可进行过滤,包含哪些jar,也可使用正则表达式过滤 -->
- <includes>
- <include>org.eclipse.epp.mpc.ui_1.1.1.5.jar</include>
- <include>*nl_zh*.jar</include>
- </includes>
- </configuration>
- </execution>
- <execution>
- <!-- 验签 -->
- <id>verifyTest</id>
- <phase>clean</phase>
- <goals>
- <goal>verify</goal>
- </goals>
- <configuration>
- <keystore>%JAVA_HOME%/jre/lib/security/cacerts</keystore>
- <alias>xxxx</alias>
- <storepass>changeit</storepass>
- <keypass>xxxx</keypass>
- <archive>d:/test/org.eclipse.help.ui_3.5.2.r36_v20100702.jar</archive>
- </configuration>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </build>
- </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
例子:
- <properties>
- <checkout.basedir>${basedir}/checkout</checkout.basedir>
- <checkout.username>svnusername</checkout.username>
- <checkout.password>svnpassword</checkout.username>
- <base.connectionURL>scm:svn:http://svnip:81/svn/repos/testProject</base.connectionURL>
- </properties>
- <profiles>
- <profile>
- <!-- checkout -->
- <id>checkout</id>
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-scm-plugin</artifactId>
- <inherited>false</inherited>
- <executions>
- <execution>
- <id>core-checkout</id>
- <goals>
- <goal>checkout</goal>
- </goals>
- <phase>validate</phase>
- <configuration>
- <scmVersion>HEAD</scmVersion>
- <scmVersionType>revision</scmVersionType>
- <skipCheckoutIfExists>true</skipCheckoutIfExists>
- <checkoutDirectory>
- ${checkout.basedir}/
- </checkoutDirectory>
- <connectionUrl>
- ${base.connectionURL}/
- </connectionUrl>
- <username>${checkout.username}</username>
- <password>${checkout.password}</password>
- <includes>testProject.im/*,testProject.server/*,eclipse_ext/epp1/*,eclipse_ext/epp2/*</includes>
- </configuration>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </build>
- </profile>
- <profile>
- <!-- update -->
- <id>update</id>
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-scm-plugin</artifactId>
- <inherited>false</inherited>
- <executions>
- <execution>
- <id>core-update</id>
- <goals>
- <goal>update</goal>
- </goals>
- <phase>validate</phase>
- <configuration>
- <basedir>
- ${checkout.basedir}/core
- </basedir>
- <scmVersion>HEAD</scmVersion>
- <scmVersionType>revision</scmVersionType>
- <connectionUrl>
- ${core.connectionURL}
- </connectionUrl>
- <username>${checkout.username}</username>
- <password>${checkout.password}</password>
- </configuration>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </build>
- </profile>
- </profiles>
- <scm>
- <connection>scm:svn:http://svnip:81/svn/repos/testProject</connection>
- <developerConnection>scm:svn:http://svnip:81/svn/repos/testProject</developerConnection>
- <url>http://svnip:81/svn/repos/testProject</url>
- </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
- durl=${url}
src/resources/test.xml
- <test>
- <url>${url}</url>
- </test>
deploy_env/resources/dev.properties
- url=http://www.baidu.com
deploy_env/resources/prod.properties
- url=http://www.qq.com
(3)pom文件:properties-maven-plugin.xml
- <properties>
- <param>dev</param>
- </properties>
- <build>
- <resources>
- <resource>
- <directory>src/resources</directory>
- <!-- 过滤 -->
- <filtering>true</filtering>
- </resource>
- </resources>
- <plugins>
- <plugin>
- <groupId>org.codehaus.mojo</groupId>
- <artifactId>properties-maven-plugin</artifactId>
- <version>1.0-alpha-1</version>
- <executions>
- <execution>
- <phase>initialize</phase>
- <goals>
- <goal>read-project-properties</goal>
- </goals>
- <configuration>
- <files>
- <file>deploy_env/resources/${param}.properties</file>
- </files>
- </configuration>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </build>
mvn -f properties-maven-plugin.xml install (默认使用dev.properties文件)
mvn -f properties-maven-plugin.xml install -Dparam=prod
构建后,在工程target文件夹下的test.properties、test.xml文件中的动态参数已被替换