如何通过annotations/pom.xml管理Maven配置文件? [英] How to manage maven profiles through annotations/pom.xml?
问题描述
Ref- Maven个人资料
我不想在运行时通过命令行参数指定配置文件.因为,如果我在本地进行更改,则必须在整个CI流中进行更改.
场景:
基本上有两个配置文件"PROD"
和"TEST"
.我已经注释了使用这些方法.基本上,我在不同的配置文件下连接不同的数据库.
虽然测试类使用@ActiveProfile("TEST")
我使用mvn clean test-compile failsafe:integraton-test
运行测试,并使用mvn springboot:run
问题:
案例1:
问题:PROD和TEST仅运行与PROD
相关的方法.
如果我不使用@Profile("PROD")
,而仅使用@Profile("TEST")
,并且使用参考中指定的@ActiveProfiles("TEST")
.上面两个未指定任何标志的mvn命令仅使用未用概要文件注释的Bean.
情况2:
问题:PROD
无法运行.
如果我同时使用PROD
和TEST
而没有任何标志,则mvn clean test-compile failsafe:integraton-test
命令运行完美(仅当指定了@ActiveProfile("TEST")
时,否则我必须发送标志-Dspring.profiles.active=TEST
),但springboot:run
不会无法工作,因为它无法获取任何数据库配置,也找不到活动的配置文件.
即使我同时使用PROD和@Primary
注释PROD配置文件,情况仍然如此
令人惊讶的是,即使我提供了命令行参数,结果也是一样的. mvn springboot:run -Dspring.profiles.active=PROD
如果我注释使用@ActiveProfiles("PROD")
运行这些配置文件的主类,则结果是相同的.
理想情况/预期:
最好使用不带标志的mvn test-compile failsafe:integration-test
运行测试. (已经实现,但是产品不起作用)
要使用mvn springboot:run
强调无标志的方式来运行产品.
首选更改:
Java注释:诸如@Profile
,@ActiveProfile
,@Primary
之类的注释,它们告诉Spring-boot或maven使用什么.
POM.xml:设置配置文件以指导maven使用什么.
简而言之,我认为您只需要将spring.profiles.active=PROD
添加到您的application.properties
我整理了一个示例,该示例打印出在prodant测试之间不同的bean内容,希望对您有所帮助.
运行以下代码时,我得到以下输出:
mvn spring-boot:run
:上下文中的字符串为:PROD
mvn clean test
:上下文中的字符串为:TEST
ProfilesApplication.java:
@SpringBootApplication
public class ProfilesApplication implements CommandLineRunner {
Logger logger = LoggerFactory.getLogger(ProfilesApplication.class);
@Autowired
private String profiledString;
public static void main(String[] args) {
SpringApplication.run(ProfilesApplication.class, args);
}
@Bean
@Profile("PROD")
public String prodString() {
return new String("PROD");
}
@Bean
@Profile("TEST")
public String testString() {
return new String("TEST");
}
@Override
public void run(String... args) throws Exception {
logger.info("The String in the context is: {}", profiledString);
}
}
application.properties:
spring.profiles.active=PROD
ProfilesApplicationTests.java:
@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles("TEST")
public class ProfilesApplicationTests {
@Test
public void contextLoads() {
}
}
Ref - Maven Profiles
I don't want to specify profile through command line arguments while running. Because, if I change it locally, then changes have to be made thoughout CI stream.
Scenario:
There are basically two profiles "PROD"
and "TEST"
. I have annotated methods using these. Basically, I connect different databases under different profiles.
While the test classes are annotated with @ActiveProfile("TEST")
I run tests using mvn clean test-compile failsafe:integraton-test
and run application using mvn springboot:run
Problem:
Case 1:
Issue: Only PROD
related methods run for PROD and TEST.
If I don't use @Profile("PROD")
and only use @Profile("TEST")
and I use @ActiveProfiles("TEST")
as specified in reference. Both mvn commands without any flag specified above only use the beans which are not annotated with profiles.
Case 2:
Issue: PROD
doesn't run.
If I use both PROD
and TEST
without any flag, mvn clean test-compile failsafe:integraton-test
command runs perfectly (only when @ActiveProfile("TEST")
is specified otherwise I'd have to send a flag -Dspring.profiles.active=TEST
) but springboot:run
doesn't work as it cannot pick up any database configuration and no active profile found.
This is the case even when I Annotate PROD profiles with both PROD and @Primary
Surprisingly even If I provide command line argument the result is the same. mvn springboot:run -Dspring.profiles.active=PROD
If I annotate the main class where these profiles are run with @ActiveProfiles("PROD")
, the result is the same.
Ideal Scenario/Expected:
To run tests using mvn test-compile failsafe:integration-test
preferably without flag. (Already acheived but prod isn't working)
To run prod using mvn springboot:run
emphasis of no flags.
Preferable changes:
Java annotations: Annotations such as @Profile
, @ActiveProfile
, @Primary
which tells Spring-boot or maven what to use.
POM.xml: setting profiles for directing maven what to use.
in short i think you just need to add spring.profiles.active=PROD
to your application.properties
I put together an example which prints the contents of a bean which differs between prod ant test, hope this helps.
When running the below code i get the following output:
mvn spring-boot:run
: The String in the context is: PROD
mvn clean test
: The String in the context is: TEST
ProfilesApplication.java:
@SpringBootApplication
public class ProfilesApplication implements CommandLineRunner {
Logger logger = LoggerFactory.getLogger(ProfilesApplication.class);
@Autowired
private String profiledString;
public static void main(String[] args) {
SpringApplication.run(ProfilesApplication.class, args);
}
@Bean
@Profile("PROD")
public String prodString() {
return new String("PROD");
}
@Bean
@Profile("TEST")
public String testString() {
return new String("TEST");
}
@Override
public void run(String... args) throws Exception {
logger.info("The String in the context is: {}", profiledString);
}
}
application.properties:
spring.profiles.active=PROD
ProfilesApplicationTests.java:
@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles("TEST")
public class ProfilesApplicationTests {
@Test
public void contextLoads() {
}
}
这篇关于如何通过annotations/pom.xml管理Maven配置文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!