I have my application.properties file in src/main/resources and i have my different active profile (application-dev.properties and application-prod.properties) in differenent directory as src/main/resources/properties.
I'm using springboot version 2.7.5
I tried to configure it in application.properties file which is in directory-src/main/resources spring.profiles.active=dev
but it is not reading the application-dev.properties from directory src/main/resources/properties
For fixing this issue i created two class
@Configuration
public class ActiveProfileConfiguration extends AbstractSecurityWebApplicationInitializer{
private static final Logger log = LoggerFactory.getLogger(ApplicationJPAConfiguration.class);
private static final String SPRING_PROFILES_ACTIVE = "SPRING_PROFILES_ACTIVE";
String profile;
protected void setSpringProfile(ServletContext servletContext) {
if(null!= System.getenv(SPRING_PROFILES_ACTIVE)){
profile=System.getenv(SPRING_PROFILES_ACTIVE);
}else if(null!= System.getProperty(SPRING_PROFILES_ACTIVE)){
profile=System.getProperty(SPRING_PROFILES_ACTIVE);
}else{
profile="local";
}
log.info("***** Profile configured is ****** " profile);
servletContext.setInitParameter("spring.profiles.active", profile);
}
}
and
@Configuration
@Profile("local")
public class DevPropertyReader {
@Bean
public static PropertySourcesPlaceholderConfigurer properties() {
PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
Resource[] resources = new ClassPathResource[] { new ClassPathResource("application.properties"), new ClassPathResource("EnvironmentProfiles/application-local.properties") };
ppc.setLocations(resources);
ppc.setIgnoreUnresolvablePlaceholders(true);
System.out.println("env active profile");
return ppc;
}
}
But still not able to resolve this issue.
CodePudding user response:
Spring will not look for property files files in your custom /properties
folder :
21.2 Application property files SpringApplication will load properties from application.properties files in the following locations and add them to the Spring Environment:
- A /config subdir of the current directory.
- The current directory
- A config package
- The classpath root
The simplest solution would be to move it at the root of src/main/resources
folder.
CodePudding user response:
Place .properties in structure like below:
src/
├─ main/
│ ├─ resources/
│ │ ├─ application.properties
│ │ ├─ application-dev.properties
│ │ ├─ application-prod.properties
Below Dockerfile example sets the right Spring Profile when running your application:
ENTRYPOINT java -Dspring.profiles.active=prod -jar /app.jar
But there are many other options through WebApplicationInitializer, Beans XML, Maven profile, etc...