Maven_Build_Resources(转) Maven_Build_Resources

功能:主要用于打包资源文件,默认情况下maven只打包src/main/resource下的资源,通过

1、设置build_resources
2、使用build-helper-maven-plugin插件
3、使用maven-resources-plugin插件
都可以自定要打包的资源
 
 
 

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

 

一般情况下,我们用到的资源文件(各种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>  

 

 

 


关键字:利用maven的resources、filter和profile实现不同环境使用不同配置文件

基本概念说明(resources、filter和profile):
1.profiles定义了各个环境的变量id
2.filters中定义了变量配置文件的地址,其中地址中的环境变量就是上面profile中定义的值
3.resources中是定义哪些目录下的文件会被配置文件中定义的变量替换,一般我们会把项目的配置文件放在src/main/resources下,像db,bean等,里面用到的变量在打包时就会根据filter中的变量配置替换成固定值


在我们平常的java开发中,会经常使用到很多配制文件(xxx.properties,xxx.xml),而当我们在本地开发(dev),测试环境测试(test),线上生产使用(product)时,需要不停的去修改这些配制文件,次数一多,相当麻烦。现在,利用maven的filter和profile功能,我们可实现在编译阶段简单的指定一个参数就能切换配制,提高效率,还不容易出错,详解如下。

一,原理:

    利用filter实现对资源文件(resouces)过滤

maven filter可利用指定的xxx.properties中对应的key=value对资源文件中的${key}进行替换,最终把你的资源文件中的username=${key}替换成username=value

    利用profile来切换环境

maven profile可使用操作系统信息,jdk信息,文件是否存在,属性值等作为依据,来激活相应的profile,也可在编译阶段,通过mvn命令加参数 -PprofileId 来手工激活使用对应的profile
结合filter和profile,我们就可以方便的在不同环境下使用不同的配制

二,配制:
在工程根目录下添加3个配制文件:

    config-dev.properties  -- 开发时用
    config-test.properties  -- 测试时用
    config-product.properties -- 生产时用

工程根目录下的pom文件中添加下面的设置:

<build>
<resources>
<!-- 先指定 src/main/resources下所有文件及文件夹为资源文件 -->
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
<!-- 设置对auto-config.properties,jdbc.properties进行过虑,即这些文件中的${key}会被替换掉为真正的值 -->
<resource>
<directory>src/main/resources</directory>
<includes>
<include>auto-config.properties</include>
<include>jdbc.properties</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>

<profiles>
<profile>
<id>dev</id>

<!-- 默认激活开发配制,使用config-dev.properties来替换设置过虑的资源文件中的${key} -->
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<filters>
<filter>config-dev.properties</filter>
</filters>
</build>
</profile>
<profile>
<id>test</id>
<build>
<filters>
<filter>config-dev.properties</filter>
</filters>
</build>
</profile>
<profile>
<id>product</id>
<build>
<filters>
<filter>config-product.properties</filter>
</filters>
</build>
</profile>
</profiles>


三,使用:

    开发环境:

filter是在maven的compile阶段执行过虑替换的,所以只要触发了编译动作即可,如果像以前一样正常使用发现没有替换,则手工clean一下工程(eclipse -> Project -> Clean)【这里你应该要安装上maven插件,因为替换是maven做的,不是eclipse做的,所以这里的clean应当是触发了maven的compile】,然后在Tomcat上也右键 -> Clean一下即可,然后你去tomcat目录下会发现你的工程的资源文件里面的${key}被替换为对应的config-xx的值了
如果上面还不行,那么就使用maven插件或者手工控制台下打maven编译命令吧
因为pom.xml中设置了dev为默认激活的,所以默认会把config-dev拿来进行替换${key}

    测试环境

手工编译,打包:maven clean install -Ptest -- 激活的profile

    生产环境

手工编译,打包:maven clean install -Pproduct -- 激活的profile