gpt4 book ai didi

java - 将 "mvnw clean package"的属性添加到 application.properties ?

转载 作者:太空宇宙 更新时间:2023-11-04 09:10:01 24 4
gpt4 key购买 nike

在 STS 中调试时,我需要属性 xxx.yyy.zzz 存在/或根本不存在。但是,当通过 mvnw clean 包部署到 Tomcat 时,我需要添加 xxx.yyy.zzz=/myApp

我看到一个 SO 溢出帖子,说你只需要这样做:

<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<xxx.yyy.zzz>/myApp</xxx.yyy.zzz>
</systemPropertyVariables>
</configuration>
</plugin>

但这行不通。我一直在阅读配置文件,但我认为这对于我的需求来说太过分了,我不想维护属性的多个副本,我只想覆盖该属性。有没有办法在您只需要覆盖值的情况下执行配置文件?

这是一个 Spring Boot 应用程序。

编辑:

根据 Khalil 的建议,我正在尝试配置文件。

1)我添加了application-tomcat.properties

2)在pom.xml中,我添加了:

<profiles>
<profile>
<id>tomcat</id>
<properties>
<spring.properties.active>tomcat</spring.properties.active>
</properties>
</profile>
</profiles>

3)我做了 mvnw clean package -P tomcat

没有错误,部署顺利,但未读取属性。

最佳答案

这个解决方案有什么问题?

application-root.properties

xxx.yyy.zzz=/

application-not-root.properties

bla bla

@Profile("root")
public String xxYY(){
return yourVar;
}

@Profile("Notroot")
public String xxYY(){
return "";
}

if (your condition){
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.getEnvironment().setActiveProfiles("root or no root");
}

如果您谈论的是 Tomcat,甚至可以这样:

if (your condition)
servletContext.setInitParameter("spring.profiles.active", "root or no root");

关于java - 将 "mvnw clean package"的属性添加到 application.properties ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59760728/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com