使用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)
[color=red]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).[/color]


设置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>


文件目录如图:

[img]http://dl.iteye.com/upload/attachment/0082/7766/c4de101f-e38e-3a1e-92f8-1a75416f7f81.bmp[/img]


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]

这样即可通过[color=red]$[][/color]占位符(在上面通过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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值