天天看點

使用maven-war-plugin 打包時排除不需要的檔案

1、過濾整個測試代碼,可以直接在指令行上指定

mvn clean install -Dmaven.test.skip=true      

提示:以上為舉例,具體的建構階段可以自定義,其中maven.test.skip為是否進行測試。

或者

mvn clean install -DskipTests      

還可以直接在pom.xml檔案上指定,比如使用maven-surefire-plugin時的配置

<plugin>  
    <groupId>org.apache.maven.plugins</groupId>  
    <artifactId>maven-surefire-plugin</artifactId>  
    <version>2.20</version>  
    <configuration>  
        <skipTests>true</skipTests>  
    </configuration>  
</plugin>        

提示:skipTests當為true為測試,反之同理。如果是使用插件,那麼要把依賴的jar包去除。

通過<properties>節點配置屬性

<properties>  
    <skipTests>true</skipTests>  
</properties>      

或者

<properties>  
    <maven.test.skip>true</maven.test.skip>  
</properties>        

2、如果是指定特定的特定的測試類時,此時需要使用maven-surefire-plugin這個插件,因為預設測試使用的就是這個插件進行關聯。

官網:http://maven.apache.org/components/surefire/maven-surefire-plugin/

如下pom.xml,指定了測試類及排除某些類

...
<build>
   <plugins>
     <plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-surefire-plugin</artifactId>
       <version>2.20</version>
       <configuration>
         <!-- 包含 -->
         <includes>
           <include>**/*Tests.java</include>
         </includes>
         <!-- 排除 -->
         <excludes>
           <exclude>**/Abstract*.java</exclude>
         </excludes>
       </configuration>
     </plugin>
   </plugins>
</build>
...      

同樣,如果不想指定以上的寫法,可以直接在指令行上指定測試類

mvn test -Dtest=[ClassName]      

提示:通過指令行就不需要配置pom.xml。

還可以直接指定某個測試類的指定方法(注意:插件要2.8以上,是以還必須指定pom.xml的版本)

mvn test -Dtest=[ClassName]#[MethodName]
[MethodName]為要運作的方法名,支援*通配符,範例:
mvn test -Dtest=MyClassTest#test1
mvn test -Dtest=MyClassTest#*test*      

maven-war-plugin插件的warSourceExcludes和packagingExcludes參數的差別

項目中在打包的時候時常要忽略一些隻在本地使用的檔案,比如一些test檔案夾或者本地配置,剛剛開始使用maven-war-plugin的warSourceExcludes和packagingExcludes這兩個參數還真是搞得有點暈,多試驗了幾次明白了,現在分享一下我的了解。 

引用我負責的一個項目對maven-war-plugin的配置:

聲明:packagingExcludes中的*.properties檔案均位于src/main/resources目錄中 warSourceExcludes中的?test/*,venue/**位于src/main/webapp目錄中
           

運作mvn package指令後結果是: 

target/mywebapp-1.0.4 (檔案夾)下面原碼中存在的test和venue目錄沒有複制過來(warSourceExcludes忽略成功),其他的檔案和目錄沒有變化

對于packagingExcludes的配置意思是從mywebapp-1.0.4檔案夾中複制檔案時忽略的檔案清單,是以最後打包的war裡面不包含test、venue檔案夾和packagingExcludes中指定的檔案 

簡單一句話說明:

warSourceExcludes是在編譯周期進行完成後從src/main/webapp目錄複制檔案時忽略,而packagingExcludes是在複制webapp目錄完成後打包時忽略target/mywebapp-1.0.4 檔案夾的檔案

說明: 

這裡使用了warSourceExcludes和packagingExcludes兩個參數為的就是示範一下含義,比如在打包産品的時候完全可以使用warSourceExcludes這一個參數來忽略檔案,這樣就可以省略packagingExcludes這個參數了