码迷,mamicode.com
首页 > 编程语言 > 详细

集成maven和Spring boot的profile功能

时间:2018-01-03 13:59:13      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:directory   post   ack   cto   body   结合   div   ica   filter   

思路:maven支持profile功能,当使用maven profile打包时,可以打包指定目录和指定文件,且可以修改文件中的变量。spring boot也支持profile功能,只要在application.properties文件中指定spring.profiles.active=xxx 即可,其中xxx是一个变量,当maven打包时,修改这个变量即可。

1。配置maven 的profile

    <!-- 不同环境查找不同配置文件 -->
    <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <profiles.active>dev</profiles.active>
                <maven.test.skip>true</maven.test.skip>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>sit</id>
            <properties>
                <profiles.active>sit</profiles.active>
                <maven.test.skip>true</maven.test.skip>
            </properties>
        </profile>
        <profile>
            <id>prd1</id>
            <properties>
                <profiles.active>prd1</profiles.active>
                <maven.test.skip>true</maven.test.skip>
                <scope.jar>provided</scope.jar>
            </properties>
        </profile>
        <profile>
            <id>prd2</id>
            <properties>
                <profiles.active>prd2</profiles.active>
                <maven.test.skip>true</maven.test.skip>
                <scope.jar>provided</scope.jar>
            </properties>
        </profile>
    </profiles>

其中profiles.active是我们定义的一个变量,可通过mvn命令指定,别人也能访问

在build中配置可修改可访问资源文件

        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <excludes>
                    <exclude>application-dev.properties</exclude>
                    <exclude>application-sit.properties</exclude>
                    <exclude>application-prd1.properties</exclude>
                    <exclude>application-prd2.properties</exclude>
                </excludes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>application-${profiles.active}.properties</include>
                    <include>application.properties</include>
                </includes>
            </resource>
        </resources>

其中  ${profiles.active}是上面profile中指定的变量

 

2.配置springboot的profile

  这是通过spring.profiles.active指定

  在application.properties中指定spring.profiles.active=@profiles.active@,即可这样就将maven与springboot的profile结合了

 

3.打包命令:mvn clean package -Dmaven.test.skip=true -P prod -e

集成maven和Spring boot的profile功能

标签:directory   post   ack   cto   body   结合   div   ica   filter   

原文地址:https://www.cnblogs.com/xiaoping1993/p/8183206.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!