Maven_Build_Resources

功能:主要用於打包資源文件,默認狀況下maven只打包src/main/resource下的資源,經過
一、設置build_resources
二、使用build-helper-maven-plugin插件
三、使用maven-resources-plugin插件
均可以自定要打包的資源



首先,來看下MAVEN項目標準的目錄結構:java

 

通常狀況下,咱們用到的資源文件(各類xml,properites,xsd文件等)都放在src/main/resources下面,利用maven打包時,maven能把這些資源文件打包到相應的jar或者war裏。網絡

 

有時候,好比mybatis的mapper.xml文件,咱們習慣把它和Mapper.java放一塊兒,都在src/main/java下面,這樣利用maven打包時,就須要修改pom.xml文件,來把mapper.xml文件一塊兒打包進jar或者war裏了,不然,這些文件不會被打包的。(maven認爲src/main/java只是java的源代碼路徑)。網絡上有不少方法,我大概試了下,幾種方法均可以,能夠任選一種便可。mybatis

 

方法1,其中**/*這樣的寫法,是爲了保證各級子目錄下的資源文件被打包。app

Xml代碼  
  1. <build>  
  2.     <finalName>test</finalName>  
  3.     <!--  
  4.     這樣也能夠把全部的xml文件,打包到相應位置。  
  5.     <resources>  
  6.         <resource>  
  7.             <directory>src/main/resources</directory>  
  8.             <includes>  
  9.                 <include>**/*.properties</include>  
  10.                 <include>**/*.xml</include>  
  11.                 <include>**/*.tld</include>  
  12.             </includes>  
  13.             <filtering>false</filtering>  
  14.         </resource>  
  15.         <resource>  
  16.             <directory>src/main/java</directory>  
  17.             <includes>  
  18.                 <include>**/*.properties</include>  
  19.                 <include>**/*.xml</include>  
  20.                 <include>**/*.tld</include>  
  21.             </includes>  
  22.             <filtering>false</filtering>  
  23.         </resource>  
  24.     </resources>  
  25. </build>  

 

方法2,利用build-helper-maven-plugin插件maven

Xml代碼  
  1. <build>  
  2.     ...  
  3.     </plugins>  
  4.         ...  
  5.         <!--  
  6.         此plugin能夠用  
  7.         利用此plugin,把源代碼中的xml文件,  
  8.         打包到相應位置,這裏主要是爲了打包Mybatis的mapper.xml文件   
  9.         -->  
  10.         <plugin>  
  11.             <groupId>org.codehaus.mojo</groupId>  
  12.             <artifactId>build-helper-maven-plugin</artifactId>  
  13.             <version>1.8</version>  
  14.             <executions>  
  15.                 <execution>  
  16.                     <id>add-resource</id>  
  17.                     <phase>generate-resources</phase>  
  18.                     <goals>  
  19.                         <goal>add-resource</goal>  
  20.                     </goals>  
  21.                     <configuration>  
  22.                         <resources>  
  23.                             <resource>  
  24.                                 <directory>src/main/java</directory>  
  25.                                 <includes>  
  26.                                     <include>**/*.xml</include>  
  27.                                 </includes>  
  28.                             </resource>  
  29.                         </resources>  
  30.                     </configuration>  
  31.                 </execution>  
  32.             </executions>  
  33.         </plugin>     
  34.         ...  
  35.     </plugins>       
  36.     ...  
  37. </build>  

 

方法3,利用maven-resources-plugin插件ui

Xml代碼  
  1. <build>  
  2.     ...  
  3.     </plugins>  
  4.         ...  
  5.         <!--  
  6.         此plugin能夠用  
  7.         利用此plugin,把源代碼中的xml文件,打包到相應位置,  
  8.         這裏主要是爲了打包Mybatis的mapper.xml文件   
  9.         -->  
  10.         <plugin>  
  11.             <artifactId>maven-resources-plugin</artifactId>  
  12.             <version>2.5</version>  
  13.             <executions>  
  14.                 <execution>  
  15.                     <id>copy-xmls</id>  
  16.                     <phase>process-sources</phase>  
  17.                     <goals>  
  18.                         <goal>copy-resources</goal>  
  19.                     </goals>  
  20.                     <configuration>  
  21.                         <outputDirectory>${basedir}/target/classes</outputDirectory>  
  22.                         <resources>  
  23.                             <resource>  
  24.                                 <directory>${basedir}/src/main/java</directory>  
  25.                                 <includes>  
  26.                                     <include>**/*.xml</include>  
  27.                                 </includes>  
  28.                             </resource>  
  29.                         </resources>  
  30.                     </configuration>  
  31.                 </execution>  
  32.             </executions>  
  33.         </plugin>     
  34.         ...  
  35.     </plugins>       
  36.     ...  
  37. </build>  

 




相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息