maven在不同环境下打包

在开发项目时,项目的开发环境,测试环境,灰度环境和生产环境的各个配置项可能有所不同,如数据库连接相关信息,日志记录等,因此要使用不同的配置文件。然后通过maven打包时就应根据不同的环境打不同的包。具体做法如下(IDE为idea2017.1.2):
在resources/config/resource文件夹下新增dev,test,prd文件夹,对应的文件夹存放对应环境的配置文件,例如ly.properties(业务相关配置参数)和application.properties(应用相关配置参数),然后resource目录下还有个配置文件为common.properties(通用配置参数)。配置文件根据实际情况来划分,建议不要把所有的配置项都堆积到一个文件中,造成阅读性很差。
然后在pom文件中配置profiles:

<profiles>
    <profile>
        <id>dev</id>
        <properties>
            <profile.path>config/resource/dev</profile.path>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <profile>
        <id>test</id>
        <properties>
            <profile.path>config/resource/test</profile.path>
        </properties>
    </profile>
        <profile>
        <id>product</id>
        <properties>
            <profile.path>config/resource/prd</profile.path>
        </properties>
    </profile>
</profiles>

profile可以让我们定义一系列的配置信息,然后指定其激活条件。这样我们就可以定义多个profile,然后每个profile对应不同的激活条件和配置信息,从而达到不同环境使用不同配置信息的效果。
接着新增build节点,跟profiles同级。

<build>
    <resources>
        <resource>   
            <directory>src/main/resources/${profile.path}</directory>
            <filtering>true</filtering>
        </resource>
        <resource>
            <directory>src/main/resources/config/resource</directory>
            <includes>
                <include>common.properties</include>
            </includes>
            <filtering>true</filtering>
        </resource>

        <resource>
            <directory>src/main/resources</directory>
            <excludes>
                <exclude>**/dev/*.*</exclude>
                <exclude>**/test/*.*</exclude>
                <exclude>**/prd/*.*</exclude>
            </excludes>
            <filtering>true</filtering>
        </resource>

    </resources>

    <plugins>
        //相关插件信息
    </plugins>
</build>

Build中定义了3个resource节点,第一个节点表示将src/main/resources/${profile.path}下面的配置文件复制到classes目录下(这里${profile.path}就是激活的具体的环境),当然如果src/main/resources/${profile.path}目录下有不需要的文件,也可以通过exclude进行排除。第二个resource节点表示将src/main/resources/config/resource下的common.properties文件复制到classes目录下。第三个节点表示将src/main/resources目录下除了dev,test和prd之外的其他所有文件/文件夹复制到classes目录下。
然后就可以在propertyConfigurer这个bean中指定配置文件了。

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:application.properties</value>
            <value>classpath:ly.properties</value>
            <value>classpath:common.properties</value>
        </list>
    </property>
</bean>

在profile激活时可以通过打包参数clean package -Pxxx -Dmaven.test.skip=ture等来指定(如-Ptest指定激活id为test的profile)。这样在maven打包时就会将test文件夹下的配置文件打包进去。
本地通过<activeByDefault>true</activeByDefault>来设置默认激活的profile。在实际中发现似乎并无效果,而需要在idea的Maven Project视图中手动选择要使用的profile的id。如下图所示:
Profiles
|——□dev
|——□test
|——□product
选中对应的profile,然后通过maven进行compile或package时都会在target目录相应classes下生成对应properties文件。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值