首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件开发 >

应用maven为不同环境的工程打包

2013-04-22 
使用maven为不同环境的工程打包pom.xml里加入下面内容:buildpluginManagementplugins plugingrou

使用maven为不同环境的工程打包
pom.xml里加入下面内容:


<build><pluginManagement>   <plugins> <plugin>                    <groupId>org.apache.maven.plugins</groupId>        <artifactId>maven-resources-plugin</artifactId>                          <version>2.5</version>                 <configuration>                          <useDefaultDelimiters>false</useDefaultDelimiters>                       <delimiters>    <delimiter>$[*]</delimiter>              </delimiters>                  <encoding>UTF-8</encoding>                    </configuration>           </plugin>                   </plugins></pluginManagement><resources><resource><directory>${basedir}/src/main/resources</directory><filtering>true</filtering></resource></resources><testResources><testResource><directory>${basedir}/src/test/resources</directory></testResource></testResources></build>



maven-resources-plugin,官网介绍地址http://maven.apache.org/plugins/maven-resources-plugin/,从2.3版本开始引入过滤机制。
http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html 这个网址具体介绍Filtering功能。
其中本例子中useDefaultDelimiters设置为false,不使用默认配置。当然也可以使用,默认的如下:(详情请看这个网址:http://maven.apache.org/plugins/maven-resources-plugin/resources-mojo.html)
Set of delimiters for expressions to filter within the resources.
These delimiters are specified in the form 'beginToken*endToken'. If no '*' is given, the delimiter is assumed to be the same for start and end.
So, the default filtering delimiters might be specified as:

<delimiters>
  <delimiter>${*}</delimiter>
  <delimiter>@</delimiter>
</delimiters>

Since the '@' delimiter is the same on both ends, we don't need to specify '@*@' (though we can).



设置prorfiles,四个profile,默认激活dev。

<profiles><profile><id>dev</id><activation><activeByDefault>true</activeByDefault></activation><build><filters><filter>../${project.parent.artifactId}/vars/vars.dev.properties</filter></filters></build></profile><profile><id>sit</id><build><filters><filter>../${project.parent.artifactId}/vars/vars.sit.properties</filter></filters></build></profile><profile><id>pre</id><build><filters><filter>../${project.parent.artifactId}/vars/vars.pre.properties</filter></filters></build></profile><profile><id>prod</id><build>   <filters>       <filter>../${project.parent.artifactId}/vars/vars.prod.properties</filter></filters></build></profile></profiles>


文件目录如图:





var.dev.properties文件为过滤文件,为普通的Property文件,里面是一些key、value值。下面是个demo

envName=dev
hostName=192.168.0.1
port=80
url=http://www.test.com

如项目中还有其他的一些配置文件,可以把变量的赋值放在var.dev.properties等文件中。比如项目中还有其他的xml文件和properties文件,只需在这些文件中作如下引用。

demo.properties

hostName=$[hostName]
testUrl=$[url]
env=$[envName]

这样即可通过$[]占位符(在上面通过pom文件配置)引用到var.dev.properties等过滤文件的内容。xml中引用过滤文件的属性,亦是如此。

最后就是通过maven打包了,激活对应的profile。如下:

dev打包命令:  mvn clean install -Pdev  或  clean install (默认是dev)
sit打包命令:  mvn clean install -Psit
pre打包命令:  mvn clean install -Ppre
prod打包命令: mvn clean install -Pprod








热点排行