91

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

Ali Dehghani
  • 46,221
  • 15
  • 164
  • 151
DarVar
  • 16,882
  • 29
  • 97
  • 146

16 Answers16

116

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
Ali Dehghani
  • 46,221
  • 15
  • 164
  • 151
PaulNUK
  • 4,774
  • 2
  • 30
  • 58
  • 26
    That worked and argument `-Dspring.profiles.active=development` overrides it, which is what I wanted – DarVar Jun 08 '16 at 20:57
  • @PaulNUK what is mean by 'production' here?? – KJEjava48 Jul 18 '19 at 06:30
  • 2
    production is just the name of a profile (it could be any valid profile). As the original question asked how to set the active profile to production, that's what I put in the answer. – PaulNUK Jul 18 '19 at 11:08
29

add --spring.profiles.active=production

Example:

java -jar file.jar --spring.profiles.active=production
Draken
  • 3,134
  • 13
  • 34
  • 54
Jaya Naresh
  • 391
  • 3
  • 3
  • 2
    This is the solution for me. Previously, I tried `java -jar file.jar -Dspring.profiles.active=production`, but that wouldn't work for me. – Valentin Grégoire Feb 10 '19 at 13:46
  • 8
    if you are intrested with -D args style it should preceed as below java -Dspring.profiles.active=production -jar file.jar – Jaya Naresh Aug 07 '19 at 09:42
  • Indeed, using -D didn't work for me either, using --spring.profiles.active for command line arguments did. – Raid Aug 30 '20 at 10:59
24

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:

spring.profiles.active=@spring.profiles.active@

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
13

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.

mchawre
  • 10,744
  • 4
  • 35
  • 57
  • Thank you. For me on java 11 is working just the one you mentioned: 'java -Dspring.profiles.active=dev -jar my-service.jar' – sasynkamil May 11 '21 at 16:41
11

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.

alxpez
  • 151
  • 1
  • 2
  • 14
  • 1
    Note - to use profile use `maven package -P{profilename}` - e.g. `maven package -Pproduction` for production based profile. – jnemecz Feb 18 '17 at 07:47
  • Does this automatically works without specify the profile at run time as mention Artegon just above. Does this really works to set activeByDefault to true without passing profile from commandline? for me using activeByDefault is not working.. – Indrajeet Gour Dec 06 '19 at 10:57
  • Any idea how to do this with yml? Doesn't seem to work – Jackie Nov 10 '20 at 17:30
  • @Jackie - see https://stackoverflow.com/a/66631458/3059685 – Joman68 Mar 15 '21 at 01:59
  • How can we achieve that only the active properties file will be part of the final jar? In our case there is sensible credential information (such as client IDs and client secrets) inside the dev profile that I don't want to expose to customers. Is there a way to exclude properties files based on the active maven profile? – Walnussbär Jun 27 '23 at 13:12
  • Found the solution to my problem: https://stackoverflow.com/questions/55286988/spring-boot-maven-plugin-exclude-properties-file-from-final-jar – Walnussbär Jun 27 '23 at 13:35
10

I do it this way

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

in the very beginning of main(...)

scorpp
  • 635
  • 10
  • 16
  • 3
    Personally, I prefer to reference constant `org.springframework.core.env.AbstractEnvironment.DEFAULT_PROFILES_PROPERTY_NAME` instead of it's value. – Ilya Serbis Aug 08 '20 at 12:54
9

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

Tom Van Rossom
  • 1,440
  • 10
  • 18
2

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

RajashekharC
  • 164
  • 1
  • 8
2

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

fcdt
  • 2,371
  • 5
  • 14
  • 26
2

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

spring:
  profiles:
    active: production
Joman68
  • 2,248
  • 3
  • 34
  • 36
1

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:

How do you get current active/default Environment profile programmatically in Spring?

user1653042
  • 355
  • 3
  • 7
1

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

0

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

Jay Ehsaniara
  • 1,421
  • 17
  • 24
0

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.

Santhoshm
  • 64
  • 3
  • The beauty of the profiles for application properties is that you can have multiple mutually exclusive sets of properties. So you don't need to define two beans. Just define one without `@Profile` and it'll get properties corresponding to the active profile. – Ilya Serbis Aug 08 '20 at 12:44
0

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

logbasex
  • 1,688
  • 1
  • 16
  • 22
0

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;
}
}
Aadam
  • 1,521
  • 9
  • 30
  • 60