一、指定编译文件的编码 maven-compile-plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>utf-8</encoding>
</configuration>
</plugin>
如果maven编译时出现乱码,通常这样指定编码后,即可解决
二 、打包同时生成源码 maven-source-plugin
<plugin>
<artifactId>maven-source-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
三、 打包时跳过单元测试 maven-surefire-plugin
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.6</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
注:起作用的是<skip>true</skip>,改成false后,单元测试就会被执行。
当然,直接这样也行:
mvn package -Dmaven.test.skip=true
另外,如果单元测试中有输出中文,eclipse的控制台里中文可能会变成乱码输出,也可以通过这个插件解决,参考配置:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<configuration>
<forkMode>once</forkMode>
<argLine>-Dfile.encoding=UTF-8</argLine>
</configuration>
</plugin>
argLine这里指定了UTF-8编码,解决了中文乱码问题
四、 单独打包依赖项 maven-assembly-plugin
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<finalName>mylib</finalName>
<appendAssemblyId>false</appendAssemblyId>
<encoding>utf-8</encoding>
<descriptors>
<descriptor>src/main/assembly/src.xml</descriptor>
</descriptors>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
注:<descriptor>src/main/assembly/src.xml</descriptor> 这里需要在src/main/assembly下放一个src.xml
<?xml version="1.0" encoding="UTF-8"?>
<assembly xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/assembly-1.0.0.xsd">
<id>package</id>
<formats>
<format>war</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<!-- <fileSets>
<fileSet>
<directory>src/main/bin</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
<fileSet>
<directory>src/main/config</directory>
<outputDirectory>config</outputDirectory>
</fileSet>
</fileSets> -->
<dependencySets>
<dependencySet>
<outputDirectory>lib</outputDirectory>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
</assembly>
最终所有依赖项,会生成一个名为mylib.war的独立文件(文件名是由<finalName>...</finalName>节点决定的)
另:
<descriptors>
<descriptor>src/main/assembly/src.xml</descriptor>
</descriptors>
这里<descriptor>...</descriptor>可重复出现,即可出现多个. 这也意味着,你同时可以有多个打包配置规则,比如依赖项打包成文件A,所有配置打包成文件B...
五、打包时指定资源目录resouces的文件编码
<!-- 设置resouce目录下的所有文件编码,否则如果配置xml文件中有中文字符,部署后可能会引起运行失败 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
最后附加二个找jar\plugin的网址:
http://search.maven.org/
http://mvnrepository.com/