maven常用插件: 打包源碼 / 跳過測試 / 單獨打包依賴項

一、指定編譯文件的編碼 maven-compile-plugin 

複製代碼
 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

複製代碼
 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

複製代碼
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後,單元測試就會被執行。另外,如果單元測試中有輸出中文,eclipse的控制檯裏中文可能會變成亂碼輸出,也可以通過這個插件解決,參考配置:

複製代碼
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

複製代碼
 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

複製代碼
 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的文件編碼

複製代碼
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/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章