maven常用插件: 打包源码 / 跳过测试 / 单独打包依赖项
一、指定编译文件的编码 maven-compile-plugin
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <plugin> 2 <groupId>org.apache.maven.plugins</groupId> 3 <artifactId>maven-compiler-plugin</artifactId> 4 <version>2.5.1</version> 5 <configuration> 6 <source>1.6</source> 7 <target>1.6</target> 8 <encoding>utf-8</encoding> 9 </configuration> 10 </plugin>
如果maven编译时出现乱码,通常这样指定编码后,即可解决
二 、打包同时生成源码 maven-source-plugin
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <plugin> 2 <artifactId>maven-source-plugin</artifactId> 3 <version>2.4</version> 4 <executions> 5 <execution> 6 <phase>package</phase> 7 <goals> 8 <goal>jar-no-fork</goal> 9 </goals> 10 </execution> 11 </executions> 12 </plugin>
三、 打包时跳过单元测试 maven-surefire-plugin
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <plugin> 2 <artifactId>maven-surefire-plugin</artifactId> 3 <version>2.6</version> 4 <configuration> 5 <skip>true</skip> 6 </configuration> 7 </plugin>
注:起作用的是<skip>true</skip>,改成false后,单元测试就会被执行。
当然,直接这样也行:
mvn package -Dmaven.test.skip=true
另外,如果单元测试中有输出中文,eclipse的控制台里中文可能会变成乱码输出,也可以通过这个插件解决,参考配置:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <plugin> 2 <groupId>org.apache.maven.plugins</groupId> 3 <artifactId>maven-surefire-plugin</artifactId> 4 <version>2.16</version> 5 <configuration> 6 <forkMode>once</forkMode> 7 <argLine>-Dfile.encoding=UTF-8</argLine> 8 </configuration> 9 </plugin>
argLine这里指定了UTF-8编码,解决了中文乱码问题
四、 单独打包依赖项 maven-assembly-plugin
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <plugin> 2 <artifactId>maven-assembly-plugin</artifactId> 3 <version>2.4.1</version> 4 <configuration> 5 <finalName>mylib</finalName> 6 <appendAssemblyId>false</appendAssemblyId> 7 <encoding>utf-8</encoding> 8 <descriptors> 9 <descriptor>src/main/assembly/src.xml</descriptor> 10 </descriptors> 11 <descriptorRefs> 12 <descriptorRef>jar-with-dependencies</descriptorRef> 13 </descriptorRefs> 14 </configuration> 15 <executions> 16 <execution> 17 <id>make-assembly</id> 18 <phase>package</phase> 19 <goals> 20 <goal>single</goal> 21 </goals> 22 </execution> 23 </executions> 24 </plugin>
注:<descriptor>src/main/assembly/src.xml</descriptor> 这里需要在src/main/assembly下放一个src.xml
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <assembly xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/assembly-1.0.0.xsd"> 4 <id>package</id> 5 <formats> 6 <format>war</format> 7 </formats> 8 <includeBaseDirectory>false</includeBaseDirectory> 9 <!-- <fileSets> 10 <fileSet> 11 <directory>src/main/bin</directory> 12 <outputDirectory>/</outputDirectory> 13 </fileSet> 14 <fileSet> 15 <directory>src/main/config</directory> 16 <outputDirectory>config</outputDirectory> 17 </fileSet> 18 </fileSets> --> 19 <dependencySets> 20 <dependencySet> 21 <outputDirectory>lib</outputDirectory> 22 <scope>runtime</scope> 23 </dependencySet> 24 </dependencySets> 25 </assembly>
最终所有依赖项,会生成一个名为mylib.war的独立文件(文件名是由<finalName>...</finalName>节点决定的)
另:
<descriptors>
<descriptor>src/main/assembly/src.xml</descriptor>
</descriptors>
这里<descriptor>...</descriptor>可重复出现,即可出现多个. 这也意味着,你同时可以有多个打包配置规则,比如依赖项打包成文件A,所有配置打包成文件B...
五、打包时指定资源目录resouces的文件编码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <!-- 设置resouce目录下的所有文件编码,否则如果配置xml文件中有中文字符,部署后可能会引起运行失败 --> 2 <plugin> 3 <groupId>org.apache.maven.plugins</groupId> 4 <artifactId>maven-resources-plugin</artifactId> 5 <version>2.6</version> 6 <configuration> 7 <encoding>UTF-8</encoding> 8 </configuration> 9 </plugin>
最后附加二个找jar\plugin的网址:
http://search.maven.org/
http://mvnrepository.com/
出处:http://yjmyzz.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。