ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Maven Profiles 多环境配置(开发、测试、生产)

2021-06-09 15:04:48  阅读:246  来源: 互联网

标签:profile activeByDefault 标签 Profiles Maven profiles 测试 true dev


profiles标签

在pom.xml中配置profiles标签

<profiles>
    <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        <activation>
        <properties>
            <build.profile>dev</build.profile>
            <packageing.type>jar</packageing.type>
        </properties>
    <profile/>

    <profile>
        <id>test</id>
        <activation>
            <activeByDefault>false</activeByDefault>
        <activation>
        <properties>
            <build.profile>test</build.profile>
            <packageing.type>war</packageing.type>
        </properties>
    <profile/>

    <profile>
        <id>prod</id>
        <activation>
            <activeByDefault>false</activeByDefault>
        <activation>
        <properties>
            <build.profile>prod</build.profile>
            <packageing.type>jar</packageing.type>
        </properties>
    <profile/>
</profiles>

activeByDefault标签

不显式指定环境时, 会取 activeByDefault true 的环境变量, 需要注意的是: 没指定 activeByDefault true 的其他相同 profile id 的 properties 是不会生效的

  • 当我们没有显式指定环境时, maven 会默认取activeByDefault 为 true 的 profile
  • 当父子 pom 都有相同的 profile id 时, 都需要显式指定 activeByDefault true, 如果不指定, 那么并不会加载没有指定 activeByDefault true 的 properties
  • 结论: 对于父子 pom 相同 id 的 profile, 每个 profile 节点都要指定 activeByDefault true

 

build标签

<build>
    <finalName>bdcs</finalName>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
        </resource>
        <resource>
            <directory>src/main/profiles/${build.profile}</directory>
        </resource>
    </resources>
</build>

使用filter

profiles标签

<profiles>
    <!-- 开发环境 -->
    <profile>
        <id>dev</id>
        <properties>
            <env>dev</env>
        </properties>
    </profile>
    <!-- 生产环境 -->
    <profile>
        <id>publish</id>
        <properties>
            <env>publish</env>
        </properties>
    </profile>
    <!-- 本地环境 -->
    <profile>
        <id>local</id>
        <properties>
            <env>local</env>
        </properties>
        <!--默认启用-->
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
</profiles>

build标签

 <build>
    <!-- 指定使用filter -->
    <filters>
        <filter>src/main/resources/profiles/${env}/env.properties</filter>
    </filters>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/**</include>
            </includes>
            <excludes>
                <exclude>profiles/**</exclude>
            </excludes>
            <filtering>true</filtering>
        </resource>
    </resources> 
</build>

切换配置环境

如果使用idea,直接打开右侧的maven在Profiels下勾选切换即可

 

如果使用命令行则通过添加后缀 -P

clean -U package -P dev -DskipTests=true -f pom.xml

 

标签:profile,activeByDefault,标签,Profiles,Maven,profiles,测试,true,dev
来源: https://www.cnblogs.com/YC-L/p/14865462.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有