常用Maven 插件总结
一. 打包类
1. Shade Plugin
用来打可执行的Jar包
示例:
<build> <finalName>storm-start-${project.version}</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.4.1</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <manifestEntries> <Main-Class>sun.storm.start.tutorial.ExclamationTopology</Main-Class> <X-Compile-Source-JDK>${jdk.version}</X-Compile-Source-JDK> <X-Compile-Target-JDK>${jdk.version}</X-Compile-Target-JDK> </manifestEntries> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.DontIncludeResourceTransformer"> <resource>.txt</resource> </transformer> </transformers> <minimizeJar>true</minimizeJar> <outputDirectory>${project.build.directory}</outputDirectory> <createDependencyReducedPom>true</createDependencyReducedPom> <createSourcesJar>false</createSourcesJar> <keepDependenciesWithProvidedScope>false</keepDependenciesWithProvidedScope> </configuration> </execution> </executions> </plugin> </plugins> </build>
shade:shade绑定在package阶段,详细说明参考 http://maven.apache.org/plugins/maven-shade-plugin/
2. Jar plugin
示例:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.6</version> <executions> <execution> <phase>package</phase> <goals> <goal>jar</goal> </goals> <configuration> <classifier>client</classifier> <includes> <include>**/service/*</include> </includes> </configuration> </execution> </executions> </plugin>
详细参考: http://maven.apache.org/plugins/maven-jar-plugin/
3. portable-config-maven-plugin
分不同环境进行打包,如测试环境、开发环境
将如有一个数据库配置文件 src/main/resource/db.properties 如下:
database.jdbc.username=dev database.jdbc.password=dev_pwd
那么在打包的过程中可以指定替换变量内容,如在测试环境中
src/main/portable/test.xml 中配置如下内容:
<portable-config> <config-file path="WEB-INF/classes/db.properties"> <replace key="database.jdbc.username">test</replace> <replace key="database.jdbc.password">test_pwd</replace> </config-file> </portable-config>
最后在maven中配置插件
<plugin> <groupId>com.juvenxu.portable-config-maven-plugin</groupId> <artifactId>portable-config-maven-plugin</artifactId> <version>1.1.5</version> <executions> <execution> <goals> <goal>replace-package</goal> </goals> </execution> </executions> <configuration> <portableConfig>src/main/portable/test.xml</portableConfig> </configuration> </plugin>
或者使用命令行
mvn clean package -DportableConfig="src/main/portable/test.xml"
二.核心插件
1. resources plugins
资源插件主要拷贝项目资源文件到输出目录,资源文件分为两种:main resources 和 test resources,从2.3版本 maven增加了资源过滤的特性( maven filtering)
(1) resources:resources
copy the resources for the main source code to the main output directory. this goal is bound by default to the process-resources life-cycle phase. It always uses the project.build.resources element to specify the resources, and by default uses the project.build.outputDirectory to specify the copy destination.
示例:
<build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> <resource> <directory>${project.basedir}/src/main/webapp</directory> <filtering>true</filtering> <includes> <include>**/*.*</include> </includes> <targetPath>${project.build.directory}/webapp</targetPath> </resource> </resources> </build>
其中filtering为true表示开启资源过滤,什么意思呢?
例如有一个资源文件 src/main/resources/hello.txt中有以下内容:
Hello ${name}
在pom中定义
<name>world</name>
如果没有开启filtering,拷贝后的文件内容为 Hello ${name},开启后侧为 Hello world
同时配置资源拷贝是的编码
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.7</version> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin>
执行资源拷贝命令:
mvn clean resources:resources
(2)resources:testResources
<testResources> <testResource> <directory>${project.basedir}/src/test/resources</directory> </testResource> </testResources>
2. Compliler Plugin
示例:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>${jdk.version}</source> <target>${jdk.version}</target> <encoding>UTF-8</encoding> <compilerVersion>${jdk.version}</compilerVersion> <verbose>true</verbose> </configuration> </plugin>
3. Deploy Plugin
这里介绍使用ftp方式部署
<project>... <distributionManagement> <repository> <id>ftp-repository</id> <url>ftp://repository.mycompany.com/repository</url> </repository> </distributionManagement> <build> <extensions> <!-- Enabling the use of FTP --> <extension> <groupId>org.apache.maven.wagon</groupId> <artifactId>wagon-ftp</artifactId> <version>1.0-beta-6</version> </extension> </extensions> </build> ... </project>
在settings.xml中配置
<settings>... <servers> <server> <id>ftp-repository</id> <username>user</username> <password>pass</password> </server> </servers> ... </settings>
执行 mvn deploy
来自:http://my.oschina.net/u/1387007/blog/519581