Bootstrap

利用MAVEN打包时,如何包含更多的资源文件

首先,来看下MAVENx项目标准的目录结构:


 

一般情况下,我们用到的资源文件(各种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的源代码路径)。网络上有很多方法,我大概试了下,几种方法都可以,可以任选一种即可。

 

方法1,其中**/*这样的写法,是为了保证各级子目录下的资源文件被打包。

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插件

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插件

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>  

 

;