maven多重环境配置


maven多重环境配置

profiles 标签

作用

profiles 标签可以配置不同的环境,编译打包时可以选择激活

<profiles>
	<profile>
    	...
    </profile>
    <profile>
    	...
    </profile>
</profiles>

profile 的激活方式

  1. IDEA 的 maven 插件

    image-20221110092604432

  2. 配置默认激活

    <profile>
        <id>dev</id>
        <properties>
            <env>dev</env>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>

    需要注意的是,有任意一个 profile 被其他方式激活后,默认激活的就会自动失效

filters 标签

作用

filters 标签属于 build 标签,给出对资源文件进行过滤的属性文件的路径,属性文件中定义若干键值对。在构建过程中,对于资源文件中出现的变量(键),将使用属性文件中该键对应的值替换。

示例

目录结构

src
└ main
  └ resources
    ├ filters
    │ ├ filter-dev.properties
    │ └ filter-prod.properties
    └ application.properties

文件内容

application.properties

server.port=8080
word=@test.word@

filter-dev.properties

test.word=hello

filter-prod.properties

test.word=hi

maven 配置

<build>
	<filters>
    	<filter>src/main/resources/filters/filter-${env}.properties</filter>
    </filters>
</build>

${env} 是 pom.xml 中的 properties,结合上面的 profiles 标签,可以指定激活相应的环境

效果

测试方法

@Value("${word}")
private String word;

@Test
void contextLoads() {
    log.info(word);
}

输出结果

hello


文章作者: ❤纱雾
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 ❤纱雾 !
评论
  目录