Apache Maven 使用 profile 和 filtering 实现多种环境下的资源配置管理



http://archboy.org/2012/05/21/apache-maven-profile-filtering-multiple-build-environments/

构建项目时可能会遇到在测试(如单元测试)、开发、模拟、生产等不同环境下需要不同配置(properties、xml)或资源(jpg、png、mp3)的情况。比如常见的数据库连接(即 jdbc url)的值,在不同的环境下可能有如下几种值:

  • 测试环境:jdbc:mysql://localhost:3306/foobar_test
  • 开发环境:jdbc:mysql://localhost:3306/foobar_dev
  • 模拟环境:jdbc:mysql://192.168.1.11:3306/foobar
  • 生产环境:jdbc:mysql://192.168.1.10:3306/foobar

或者同样是生产环境,针对(产品)交付给A公司客户的与交付给B公司客户的需要不同配置或者资源,比如产品界面中的公司名称、公司LOGO等。

又或者针对不同的操作系统(如 Windows,Linux)需要为某个配置设定不同的文件路径。

可见,在不同的软件开发生命周期阶段、不同的最终客户(用户)环境、不同的运行平台都有可能需要不同配置或资源的情况。假如各个环境下的差别很小的话,我们可以在项目编译之后手工修改或者写个 shell script 自动修改,但如果需要修改的项目很多而且复杂的话,则应该使用 Apache Maven 的 Profile 和 Filtering 功能来解决。(当然前提是你的项目必须是用 Maven 构建的啦,哈哈,还有测试阶段所使用到的资源文件实际上 Maven 默认已经划分出来,所以并不需要本文所说的方法)

Filtering 功能

Filtering 是 Maven Resources Plugin 的一个功能,它会使用系统属性或者项目属性的值替换资源文件(*.properties,*.xml)当中 ${…} 符号的值。比如你系统属性有一项 “user.name=foobar”,那么资源文件当中的 ${user.name} 符号会在 Maven 编译时自动被替换为 “foobar”。

举个例子:

默认的项目资源文件位于 “src/main/resources” 目录,在该目录下创建一个文件 “test.properties”,里面写上一行:

Hello ${user.name}

然后修改项目文件(pom.xml)启用 filtering 功能,如:

<project>
	...
	<build>
	  ...
	    <resources>
	      <resource>
	        <directory>src/main/resources</directory>
	        <filtering>true</filtering>
	      </resource>
	      ...
	    </resources>
	    ...
	</build>
	...
</project>

然后编译项目:

$mvn clean compile -Duser.name=foobar

查看输出文件 target/classes/test.properties 的内容,可见原先的 “Hello {user.name}” 已经变成 “Hello foobar”。

我们也可以把 filtering 用到的变量写在项目属性段里,比如:

<project>
  ...
  <properties>
    <user.name>foobar</user.name>
    <user.email>foobar@some.com</user.email>
  </properties>
  ...
</project>

如果属性项比较多的话,最佳实践是把他们抽离出来独立一个属性文件,比如在你项目目录(即 pom.xml 文件所在的目录)新建一个属性文件 project.properties:

user.name=foobar
user.email=foobar@some.com

然后在 build/filters/filter 里指明使用这个属性文件作为 filtering 属性值的来源:

<project>
  ...
  <build>
    ...
    <filters>
      <filter>project.properties</filter>
    </filters>
    ...
  </build>
  ...
</project>

Profile 功能

Profile 的作用是允许你在项目文件(pom.xml)里定义若干个 profile 段,然后在编译时选择其中的一个用于覆盖项目文件原先的定义。接着上一个例子,如果我们需要为开发环境和生产环境定义不同的 user.name 属性值,则我们在项目目录里创建两个属性文件:

profile-development.properties,内容

user.name=foobar

profile-production.properties,内容

user.name=tom

然后在项目文件(pom.xml)里增加 profile 段,如下:

<project>
  ...
  <profiles>
    <profile>
      <id>development</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <build>
        <filters>
          <filter>profile-development.properties</filter>
        </filters>
      </build>
    </profile>
    <profile>
      <id>production</id>
      <build>
        <filters>
          <filter>profile-production.properties</filter>
        </filters>
      </build>
    </profile>
  </profiles>
</project>

在编译项目时,可以使用 -P 参数指定需要使用的 profile 的 id,比如下面命令将会使用 development profile:

$mvn clean compile -Pdevelopment

如果想使用 production profile 则执行如下命令:

$mvn clean compile -Pproduction

假如不指定 -P 参数的话,则会使用 activeByDefault=true 的一项(即 development)。

至此,通过 filtering 和 profile 功能实现了为开发环境和生产环境使用不同配置值的目的。当然 profile 还可以允许你添加更多的定义,比如为某一个 profile 添加不同的资源文件。在一些大中型项目里,不同的环境可能仅仅修改配置值并不足够,可能还需要某个配置文件整个替换,那么就应该在 profiles/profile/build/resources 段里指定了。详细的可以参阅附录链接。

附录:


================================

http://piotrnowicki.com/2012/10/filtered-resources-in-maven/

Piotr Nowicki

About

Filtered Resources in Maven

Maven has some really great features. One of them is filtered resources.

Basically, if you use Maven for some time, I’m sure you know what by default everything you put in src/main/resources will be added to the classpath in the resulting artifact (e.g. in the WEB-INF/classes for a *.war archive.) Putting resources in this location is a common way of adding Java properties file to your project.

Now what is interesting: you can use Maven variables in those resource files and they can be swapped into final value during the process-resource phase. You just need to tell Maven that it is not a “regular” resource and that it should be filtered.

As an example: assume you have a following properties file:

src/main/resources/application.properties

app.version = ${version}

And you add the following section to your pom.xml file:

pom.xml

<project>
    <build>
        ...
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>application.properties</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>
<project>

During the prepare-resources phase the application.properties file will be filtered by Maven. The ${...} placeholders will be filled with proper values and the resulting application.properties will be produced and placed somewhere in the target directory. Its content will be something like:

app.version = 0.1.1-SNAPSHOT

You can use variables available out-of-the-box in Maven or some additional ones coming e.g. from the Maven plugins or your own defined in the <properties> pom.xml element. It’s pretty neat if you combine it with Maven Build Number plugin which can add your revision or some custom made build number and save it to your runtime accessible property file.

As a side note, if you define the exact files / directories you want to be filtered and want to treat the rest of the files as regular ones (not filtered) you should instruct Maven to do so. It seems that if you define at least one <resource> element, the default values doesn’t apply anymore. Something like this should do the work:

<project>
    <build>
        ...
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>application.properties</include>
                </includes>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
    </build>
<project>

You can see an example of how I used it with Build Number plugin here.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值