Setting the default active profile in Spring-boot

JavaSpringSpring Boot

Java Problem Overview


I want my default active profile to be production if -Dspring.profiles.active is not set.

I tried the following in my application.properties but it did't work:

spring.profiles.default=production

Spring-boot version = 1.3.5.RELEASE

Java Solutions


Solution 1 - Java

What you are doing here is setting the default default profile (the profile that is used on any bean if you don't specify the @Profile annotation) to be production.

What you actually need to do is set the default active profile, which is done like this:

spring.profiles.active=production

Solution 2 - Java

add --spring.profiles.active=production

Example:

java -jar file.jar --spring.profiles.active=production

Solution 3 - Java

First of all, with the solution below, is necessary to understand that always the spring boot will read the application.properties file. So the other's profile files only will complement and replace the properties defined before.

Considering the follow files:

application.properties
application-qa.properties
application-prod.properties
  1. Very important. The application.properties, and just this file, must have the follow line:

    [email protected]@

  2. Change what you want in the QA and PROD configuration files to see the difference between the environments.

  3. By command line, start the spring boot app with any of this options:

It will start the app with the default application.properties file:

mvn spring-boot:run

It will load the default application.properties file and after the application-qa.properties file, replacing and/or complementing the default configuration:

mvn spring-boot:run -Dspring.profiles.active=qa

The same here but with the production environment instead of QA:

mvn spring-boot:run -Dspring.profiles.active=prod

Solution 4 - Java

We to faced similar issue while setting spring.profiles.active in java.

This is what we figured out in the end, after trying four different ways of providing spring.profiles.active.

In java-8

$ java --spring.profiles.active=dev -jar my-service.jar
Gives unrecognized --spring.profiles.active option.
$ java -jar my-service.jar --spring.profiles.active=dev
# This works fine
$ java -Dspring.profiles.active=dev -jar my-service.jar
# This works fine
$ java -jar my-service.jar -Dspring.profiles.active=dev
# This doesn't works

In java-11

$ java --spring.profiles.active=dev -jar my-service.jar
Gives unrecognized --spring.profiles.active option.
$ java -jar my-service.jar --spring.profiles.active=dev
# This doesn't works
$ java -Dspring.profiles.active=dev -jar my-service.jar
# This works fine
$ java -jar my-service.jar -Dspring.profiles.active=dev
# This doesn't works

NOTE: If you're specifying spring.profiles.active in your application.properties file then make sure you provide spring.config.location or spring.config.additional-location option to java accordingly as mentioned above.

Solution 5 - Java

If you're using maven I would do something like this:

Being production your default profile:

<properties>
    <activeProfile>production</activeProfile>
</properties>

And as an example of other profiles:

<profiles>
    <!--Your default profile... selected if none specified-->
    <profile>
        <id>production</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <activeProfile>production</activeProfile>
        </properties>
    </profile>

    <!--Profile 2-->
    <profile>
        <id>development</id>
        <properties>
            <activeProfile>development</activeProfile>
        </properties>
    </profile>

    <!--Profile 3-->
    <profile>
        <id>otherprofile</id>
        <properties>
            <activeProfile>otherprofile</activeProfile>
        </properties>
    </profile>
<profiles>

In your application.properties you'll have to set:

spring.profiles.active=@activeProfile@

This works for me every time, hope it solves your problem.

Solution 6 - Java

I do it this way

    System.setProperty("spring.profiles.default", "dev");

in the very beginning of main(...)

Solution 7 - Java

Put this in the App.java:

public static void main(String[] args) throws UnknownHostException {
    SpringApplication app = new SpringApplication(App.class);
    SimpleCommandLinePropertySource source = new SimpleCommandLinePropertySource(args);
    if (!source.containsProperty("spring.profiles.active") &&
            !System.getenv().containsKey("SPRING_PROFILES_ACTIVE")) {

        app.setAdditionalProfiles("production");
    }
    ...
}

This is how it is done in JHipster

Solution 8 - Java

If you are using AWS Lambda with SprintBoot, then you must declare the following under environment variables:

key: JAVA_TOOL_OPTIONS & value: -Dspring.profiles.active=dev

Solution 9 - Java

Currently using Maven + Spring Boot. Our solution was the following:

application.properties

#spring.profiles.active= # comment_out or remove

securityConfig.java

@Value(${spring.profiles.active:[default_profile_name]}")
private String ACTIVE_PROFILE_NAME;

Credit starts with MartinMlima. Similar answer provided here:

https://stackoverflow.com/questions/9267799/how-do-you-get-current-active-default-environment-profile-programmatically-in-sp?answertab=active#tab-top

Solution 10 - Java

you can also have multiple listings in the @Profile annotation

@Profile({"dev","default"})

If you set "default" as an additional value, you don't have to specify spring.profiles.active

Solution 11 - Java

The neat way to do this without changing your source code each time is to use the OS environment variable SPRING_PROFILES_ACTIVE:

export SPRING_PROFILES_ACTIVE=production

how-to-set-active-spring-profiles

Solution 12 - Java

In AWS LAMBDA:

For $ sam local you add the following line in your sam template yml file:

Resources:
   FunctionName:
       Properties:
           Environment:
               Variables:
                  SPRING_PROFILES_ACTIVE: local

But in AWS Console: in your Lambda Environment variables just add:

KEY:JAVA_TOOL_OPTIONS VALUE:-Dspring.profiles.active=dev

enter image description here

Solution 13 - Java

One can have separate application properties files according to the environment, if Spring Boot application is being created. For example - properties file for dev environment, application-dev.properties:

spring.hivedatasource.url=<hive dev data source url>
spring.hivedatasource.username=dev
spring.hivedatasource.password=dev
spring.hivedatasource.driver-class-name=org.apache.hive.jdbc.HiveDriver

application-test.properties:

spring.hivedatasource.url=<hive dev data source url>
spring.hivedatasource.username=test
spring.hivedatasource.password=test
spring.hivedatasource.driver-class-name=org.apache.hive.jdbc.HiveDriver

And a primary application.properties file to select the profile:

application.properties:

spring.profiles.active=dev
server.tomcat.max-threads = 10
spring.application.name=sampleApp

Define the DB Configuration as below:

@Configuration
@ConfigurationProperties(prefix="spring.hivedatasource")
public class DBConfig {

    @Profile("dev")
    @Qualifier("hivedatasource")
    @Primary
    @Bean
    public DataSource devHiveDataSource() {
        System.out.println("DataSource bean created for Dev");
        return new BasicDataSource();
    }

    @Profile("test")
    @Qualifier("hivedatasource")
    @Primary
    @Bean
    public DataSource testHiveDataSource() {
        System.out.println("DataSource bean created for Test");
        return new BasicDataSource();
    }

This will automatically create the BasicDataSource bean according to the active profile set in application.properties file. Run the Spring-boot application and test.

Note that this will create an empty bean initially until getConnection() is called. Once the connection is available you can get the url, driver-class, etc. using that DataSource bean.

Solution 14 - Java

Try this: @PropertySource("classpath:${spring.profiles.active:production}_file.properties")

Solution 15 - Java

If you are using application.yml for your config then add this to it, to set the default active profile:

spring:
  profiles:
    active: production

Solution 16 - Java

If anyone out there trying to load profile-specific properties for spring web(not boot) Then do this, can add multiple profiles to pom. Then in config class we can read the build profile and load props accordingly

> build like mvn clean install -Pdev

pom.xml

<profiles>
<profile>
            <id>prod</id>
             <properties>
		        <activatedProperties>prod</activatedProperties>
		    </properties>
            <activation>
                <!-- activate if system properties 'env=prod' -->
            </activation>
            <build><finalName>Finger</finalName></build>
        </profile>
    </profiles>

class

    @Configuration
@PropertySource({"classpath:application.properties"})
public class MyApplicationConfiguration {
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
	
    PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
    Resource resource = new ClassPathResource("/application.properties");
    try {
		Properties props1 = PropertiesLoaderUtils.loadProperties(resource);
		resource = new ClassPathResource("/application-"+props1.getProperty("spring.profiles.active")+".properties");
		configurer.setProperties(props1);
		configurer.setLocation(resource);
	} catch (IOException e) {
		e.printStackTrace();
	}
    return configurer;
}
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDarVarView Question on Stackoverflow
Solution 1 - JavaPaulNUKView Answer on Stackoverflow
Solution 2 - JavaJaya NareshView Answer on Stackoverflow
Solution 3 - JavaRafael S. FijalkowskiView Answer on Stackoverflow
Solution 4 - JavamchawreView Answer on Stackoverflow
Solution 5 - JavaalxpezView Answer on Stackoverflow
Solution 6 - JavascorppView Answer on Stackoverflow
Solution 7 - JavaTom Van RossomView Answer on Stackoverflow
Solution 8 - JavaRajashekharCView Answer on Stackoverflow
Solution 9 - Javauser1653042View Answer on Stackoverflow
Solution 10 - JavaJulian SchroeterView Answer on Stackoverflow
Solution 11 - JavaAbdessamad ABOUKDIRView Answer on Stackoverflow
Solution 12 - JavaJayView Answer on Stackoverflow
Solution 13 - JavaSanthoshmView Answer on Stackoverflow
Solution 14 - JavalogbasexView Answer on Stackoverflow
Solution 15 - JavaJoman68View Answer on Stackoverflow
Solution 16 - JavaAadamView Answer on Stackoverflow