maven整合spring profile实现多环境自动切换

maven整合spring profile实现多环境自动切换
1、resources目录下添加不同环境的配置文件


2、applicationContext文件增加profile配置
<beans profile="dev">
    <import resource="classpath:dev/remote-sevlet.xml"/>
    <context:property-placeholder location="classpath:dev/*.properties"/>
</beans>
<beans profile="test">
    <import resource="classpath:test/remote-sevlet.xml"/>
    <context:property-placeholder location="classpath:test/*.properties"/>
</beans>
<beans profile="prod">
    <import resource="classpath:prod/remote-sevlet.xml"/>
    <context:property-placeholder location="classpath:prod/*.properties"/>
</beans>


3、web.xml增加激活profile的配置
<context-param>
    <param-name>spring.profiles.default</param-name>
    <param-value>${profile.active}</param-value>
</context-param>


4、pom.xml文件增加profile配置
<profiles>
    <profile>
        <id>dev</id>
        <properties>
            <profile.active>dev</profile.active>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <profile>
        <id>local</id>
        <properties>
            <profile.active>local</profile.active>
        </properties>
    </profile>
    <profile>
        <id>prod</id>
        <properties>
            <profile.active>prod</profile.active>
        </properties>
    </profile>
    <profile>
        <id>test</id>
        <properties>
            <profile.active>test</profile.active>
        </properties>
    </profile>
</profiles>




5、pom.xml增加打包插件
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <configuration>
        <warName>ROOT</warName>
        <!-- 激活spring profile -->
        <webResources>
            <resource>
                <filtering>true</filtering>
                <directory>src/main/webapp</directory>
                <includes>
                    <include>**/web.xml</include>
                </includes>
            </resource>
        </webResources>
        <warSourceDirectory>src/main/webapp</warSourceDirectory>
        <webXml>src/main/webapp/WEB-INF/web.xml</webXml>
    </configuration>
</plugin>


6:打包命令
如:mvn clean package -DskipTests=true -P prod -P参数为指定的打包环境


实现原理参考:http://zilongsky-gmail-com.iteye.com/blog/2032001
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值