Maven Profile 多环境构建

项目开发中,都需要按不同环境进行不同的构建打包,最基本的就是把项目配置文件按环境打包:

  • dev:开发环境
  • test:测试环境
  • preproduct:预发布环境(上线前的预发布服务器环境)
  • product:生产环境

好像也无需解释太多,需要用到 Maven Profile 的人,想必此功能的目的也是相当明确的。

一、Meven Profile 配置

主要是配置 pom.xml:

<project>
    ...
    <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <profiles.active>dev</profiles.active>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <profiles.active>test</profiles.active>
            </properties>
        </profile>
        <profile>
            <id>product</id>
            <properties>
                <profiles.active>product</profiles.active>
            </properties>
        </profile>
    </profiles>
    ...
</project>

其中 <activeByDefault>true</activeByDefault> 表示该 profile 默认激活。当然,<properties> 标签中还可以按环境按需定义更多的属性。

二、项目配置文件目录规划

我一般是:

src
....main
........java
........resource
............spring-context.xml
............other_common.properties
............profle
................dev
....................jdbc.properties
....................log.xml
................test
....................jdbc.properties
....................log.xml
................product
....................jdbc.properties
....................log.xml

上面这样表示看起来有点杂,中心思想就是把公共的配置放在 src/main/resource 根目录下,其他环境不同的配置,放在 src/main/resource/profile/${profiles.active}/目录下。

三、Maven 资源路径配置

<project>
    ...
    <build> 
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <excludes>
                    <exclude>profile/**</exclude>
                </excludes>
            </resource>
            <resource>
                <directory>src/main/resources/profile/${profiles.active}</directory>
            </resource>
        </resources>
    </build>    
    ...
</project>

说明:

  • 第一个 <resource> 标签,把 src/main/resources 目录下所有配置包括进来,但是排除所有 profile/ 下的配置。
  • 第二个 <resource> 标签,把步骤一种激活的 ${profiles.active} 值的路径下的配置文件包括进来,由此实现按环境构建。
  • 注意:第二个 <resource> 标签这样配置, src/main/resources/profile/${profiles.active} 目录下的配置文件会被构建打包到 src/main/resources 根目录下。

四、构建打包

Maven Profile 构建打包命令:例如构建测试环境包:

# mvn clean package -Ptest

只需加 mvn 命令参数 -P 并加上步骤一中定义的 profile id 即可。如果要跳过测试:

# mvn clean package -DskipTests -Ptest

五、插曲:-DskipTests 和 -Dmaven.test.skip=true 区别

区别是:

  • -DskipTests compiles the tests, but skips running them. 编译测试用例,但是不执行。
  • -Dmaven.test.skip=true skips compiling the tests and does not run them. 既不编译测试用例,也不执行测试用例。”maven.test.skip is honored by Surefire, Failsafe and the Compiler Plugin”, 是万无一失的、准不会有错的。
  • 参考:《What’s the difference between -DskipTests and -Dmaven.test.skip=true》&《Maven docs

六、参考

先写到这。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值