Maven打包时自动选择不同的配置(利用profile和filter插件)

来源:互联网 发布:淘宝头层牛皮是真的吗 编辑:程序博客网 时间:2024/06/05 08:17

项目开发时,一般都会部署到两套以上不同的环境中(比如开发环境和生产环境)。打包的时候需要修改配置文件中的很多信息、或替换不同的配置文件,很容易出错而且不方便。下面举例介绍如何利用Maven的profile和filter插件来解决这个问题——为不同环境打包前自动修改配置文件中的内容:


假设有两个环境:
  • 开发环境
  • 生产环境
配置文件目录为src/main/resources


第一步为两个不同的环境建立两个配置文件,作为其他配置文件实际内容的来源
  • development.properties,内容为:
db.url=development-url
  • production.properties,内容为:
db.url=production-url

第二步在其他公用配置文件(.properties, .xml, etc.)中,使用${}来引用上面定义的值,如:
db.url=${db.url}<bean id="testService" class="com.xjj.service.impl.TestServiceImpl">    <property name="url" value="${db.url}"></property></bean>

第三步在POM文件中定义两个profile

<project>... <!-- 选择不同的属性为resource中的变量赋值。development:开发环境,production:生产环境 --> <profiles>      <profile>           <id>development</id>           <activation>                <activeByDefault>true</activeByDefault>           </activation>           <build>                <filters>                     <filter>src/main/resources/profile/development.properties</filter>                </filters>           </build>      </profile>      <profile>           <id>production</id>           <build>                <filters>                     <filter>src/main/resources/profile/production.properties</filter>                </filters>           </build>      </profile> </profiles>...</project>

第四步在POM文件中定义需要被替换的文件,并使用filter

<project>...    <build>    ...        <!-- 启用filtering功能为resources中的变量赋值 -->          <resources>               <resource>                    <directory>src/main/resources</directory>                    <filtering>true</filtering>               </resource>          </resources>    </build></project>

第五步:在MyEclipse中添加两个Maven Build快捷方式
Run -> Run Configurations... -> Maven Build
Base directory: Browse Workspace...选择当前项目
Goals中填入:
  • 开发环境:package -Pdevelopment
  • 生产环境:package -Pproduction
如果要先clean并且跳过测试
  • 开发环境:clean package -Pdevelopment -Dmaven.test.skip=ture
  • 生产环境:clean package -Pproduction -Dmaven.test.skip=ture

第六步:打包
分别运行这两个Maven Build,查看生成的jar或war包,就可以看到第二步配置文件中的变量已经被替换成development.properties或production.properties中的内容了。

注1:如果其他目录中有需要打包的xml文件(比如MyBatis的mapper.xml文件),需要另外指定,不然Maven默认不会打包:
<resources>
    ……    <resource>        <directory>src/main/java</directory>        <includes>            <include>**/*.properties</include>            <include>**/*.xml</include>        </includes>        <filtering>false</filtering>    </resource></resources>

注2:如果resources文件夹中有二进制文件,需要排除在filter之外(先exclude,后面再单独在一个resource中声明),不然可能会搞坏它:
<resources>    <resource>        <directory>src/main/resources</directory>        <filtering>true</filtering>        <excludes>            <exclude>**/*.p12</exclude>        </excludes>   </resource>   <resource>       <directory>src/main/resources</directory>        <filtering>false</filtering>        <includes>             <include>**/*.p12</include>        </includes>    </resource></resources>

具体的全部代码请参考:https://github.com/xujijun/MyJavaStudio


(原创文章,转载请注明转自Clement-Xu的博客:http://blog.csdn.net/clementad/article/details/43603441)






0 0