129

I have the application.yml, application-dev.yml and application-dev.yml files

  1. I'm using the maven command mvn spring-boot:run -Dspring.profiles.active=dev it doesn't work and I can not choose the dev profile using mvn spring-boot:run. How do I choose it?
  2. The documentation says java -jar XXX.jar --spring.profiles.active=dev works, and I tried -Dspring.profiles.active=dev but it does not work. And in my project, I use java -jar XXX.jar it runs, but if I use java -jar XXX.jar --spring.profiles.active=dev to choose dev profile, console print so many logs and warns that I never see used java -jar XXX.jar,and tell me APPLICATION FAILED TO START

So, how to solve these two problems?

Thanks.

informatik01
  • 16,038
  • 10
  • 74
  • 104
twogoods
  • 1,646
  • 2
  • 14
  • 21

13 Answers13

204

I'm not sure I fully understand the question but I'll attempt to answer by providing a few details about profiles in Spring Boot.

For your #1 example, according to the docs you can select the profile using the Spring Boot Maven Plugin using -Drun.profiles.

Edit: For Spring Boot 2.0+ run has been renamed to spring-boot.run and run.profiles has been renamed to spring-boot.run.profiles

mvn spring-boot:run -Dspring-boot.run.profiles=dev

For more details look at: Spring Boot Maven Plugin

From your #2 example, you are defining the active profile after the name of the jar. You need to provide the JVM argument before the name of the jar you are running.

java -jar -Dspring.profiles.active=dev XXX.jar

General info:

You mention that you have both an application.yml and a application-dev.yml. Running with the dev profile will actually load both config files. Values from application-dev.yml will override the same values provided by application.yml but values from both yml files will be loaded.

There are also multiple ways to define the active profile.

You can define them as you did, using -Dspring.profiles.active when running your jar. You can also set the profile using a SPRING_PROFILES_ACTIVE environment variable or a spring.profiles.active system property.

More info can be found here: 2.6. Set the Active Spring Profiles

Manuel Jordan
  • 15,253
  • 21
  • 95
  • 158
francis
  • 5,889
  • 3
  • 27
  • 51
  • thanks,the way you gived should be ok,but as i say I use `java -jar XXX.jar --spring.profiles.active=dev` to choose dev profile, console print so many logs and warns that i never see used ` java -jar XXX.jar`, and tell me `APPLICATION FAILED TO START` but build success. `mvn spring-boot:run -Drun.profiles=dev` and `java -jar -Dspring.profiles.active=dev XXX.jar` is same,`mvn spring-boot:run` and `java -jar ` can run well.i open debug mode accidentally??? – twogoods Oct 16 '16 at 02:06
  • The order is wrong, `java -jar XXX.jar --spring.profiles.active=dev` does **not** set the profile to dev. You need to use `java -jar -Dspring.profiles.active=dev XXX.jar` – francis Oct 16 '16 at 03:25
  • 1
    i have use `java -jar -Dspring.profiles.active=dev XXX.jar` console print many logs, warns and debugs, and failed to start. `java -jar XXX.jar --spring.profiles.active=dev` this order is wrong but also console print many logs, warns and debugs,the same as use `java -jar -Dspring.profiles.active=dev XXX.jar`.and i have try the quickstart in documents, both order is ok can choose the profile .springboot version is 1.4.1 – twogoods Oct 16 '16 at 06:04
  • 1
    In that case you most likely have a different problem not related to your profile – francis Oct 16 '16 at 14:24
  • What is nice about @twogoods answer above (`java -jar XXX.jar -Dspring.profiles.active=dev`), is that you do not need Maven installed to start the spring boot app. For example, Maven may not be available on production server. – John DeRegnaucourt Oct 20 '19 at 00:52
52

If you are using the Spring Boot Maven Plugin, run:

mvn spring-boot:run -Dspring-boot.run.profiles=foo,bar

(https://docs.spring.io/spring-boot/docs/current/maven-plugin/reference/htmlsingle/#using.overriding-command-line)

pedromorfeu
  • 1,739
  • 14
  • 18
36

Since Spring Boot v2+

Alternatives below works with Spring Boot v2.0.0 and newer.

With Spring Boot Maven Plugin

You can provide commandline argument like this:

mvn spring-boot:run -Dspring-boot.run.arguments="--spring.profiles.active=dev"

You can provide JVM argument like this:

mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Dspring.profiles.active=dev"

java -jar

java -Dspring.profiles.active=dev -jar app.jar (VM param)

or

java -jar app.jar --spring.profiles.active=dev (program param)

A note on Windows Powershell

Powershell will require you to wrap in double quotes like shown here since it otherwise will result in an error because of how it interpret a period: java -D"spring.profiles.active=dev" -jar app.jar

Avec
  • 1,626
  • 21
  • 31
  • 1
    ./mvnw spring-boot:run -Dspring-boot.run.arguments="--spring.profiles.active=prod" , this worked, thanks a lot ! – echoaman May 15 '21 at 21:59
7

You don't need three .yml files for this. You can have a single application.yml file and write profile specific properties in the same where each profile section is separated by 3 hyphen (---)

Next, for selecting the current active profile, you can specify that as well in your application.yml file, like this :

spring:
  profiles:
    active:
    - local

However, this configuration will be overriden if you set an Environment variable, eg : SPRING_PROFILES_ACTIVE = dev


Here is a sample file for you requirement:

# include common properties for every profile in this section

server.port: 5000 

spring:
  profiles:
    active:
    - local

---
# profile specific properties

spring:
  profiles: local

  datasource:
    url: jdbc:mysql://localhost:3306/
    username: root
    password: root

---
# profile specific properties

spring:
  profiles: dev

  datasource:
    url: jdbc:mysql://<dev db url>
    username: <username>
    password: <password>
sumit singh
  • 522
  • 4
  • 9
  • 1
    Why would you need to do that? Config files tend to grow over time. Having all of them in a single file is a VERY bad idea, except if you like zapping through lines of code. – hypercube Dec 19 '22 at 18:32
6

If you are using maven, define your profiles as shown below within your pom.xml

<profiles>
<profile>
    <id>local</id>
    <activation>
        <activeByDefault>true</activeByDefault>
    </activation>
    <properties>
        <jdbc.url>dbUrl</jdbc.url>
        <jdbc.username>dbuser</jdbc.username>
        <jdbc.password>dbPassword</jdbc.password>
        <jdbc.driver>dbDriver</jdbc.driver>
    </properties>
</profile>
<profile>
    <id>dev</id>
    <properties>
        <jdbc.url>dbUrl</jdbc.url>
        <jdbc.username>dbuser</jdbc.username>
        <jdbc.password>dbPassword</jdbc.password>
        <jdbc.driver>dbDriver</jdbc.driver>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
        </dependency>
    </dependencies>
</profile>
<profile>
    <id>prod</id>
    <properties>
        <jdbc.url>dbUrl</jdbc.url>
        <jdbc.username>dbuser</jdbc.username>
        <jdbc.password>dbPassword</jdbc.password>
        <jdbc.driver>dbDriver</jdbc.driver>
    </properties>
</profile>

By default, i.e if No profile is selected, the local profile will always be use.

To select a specific profile in Spring Boot 2.x.x, use the below command.

mvn spring-boot:run -Dspring-boot.run.profiles=dev

If you want want to build/compile using properties of a specific profile, use the below command.

mvn clean install -Pdev -DprofileIdEnabled=true
Amimo Benja
  • 505
  • 5
  • 8
  • is this still supposed to work for any arbitrary property? I am looking at my generated war file and it has the original property in there still. – SledgeHammer Jan 16 '20 at 00:03
5

mvn spring-boot:run -Dspring-boot.run.profiles=foo,bar

**Source- **https://docs.spring.io/spring-boot/docs/current/maven-plugin/examples/run-profiles.html

Basically it is need when you multiple application-{environment}.properties is present inside in your project. by default, if you passed -Drun.profiles on command line or activeByDefault true in

 <profile>
    <id>dev</id>
    <properties>
        <activatedProperties>dev</activatedProperties>
    </properties>
    <activation>
        <activeByDefault>true</activeByDefault>
    </activation>
</profile>

Nothing defined like above it will choose by default application.properties otherwise you need to select by appending -Drun.profiles={dev/stage/prod}.

TL;DR

mvn spring-boot:run -Drun.profiles=dev

kris
  • 979
  • 11
  • 15
4

You can specify properties according profiles in one application.properties(yml) like here. Then mvn clean spring-boot:run -Dspring.profiles.active=dev should run it correct. It works for me

Sergii Getman
  • 3,845
  • 5
  • 34
  • 50
  • 1
    Is -Dspring.profiles.active=dev equivalent of -Dspring-boot.run.profiles=dev? It's frustrating... brrrr. – Simon Logic Oct 26 '18 at 19:43
  • @SimonLogic, see Amimo Benja's answer. – vmaldosan Mar 28 '19 at 19:38
  • 1
    This answer does not work for me. Instead, see the answer from @francis, `mvn spring-boot:run -Dspring-boot.run.profiles=dev` works. – John DeRegnaucourt Oct 20 '19 at 00:43
  • 2
    I'm using [Spring Boot Maven Plugin](https://docs.spring.io/spring-boot/docs/current/maven-plugin/examples/run-profiles.html), so I do: `mvn spring-boot:run -Dspring-boot.run.profiles=foo,bar` (as stated in other answers way below). – Sumi Straessle Apr 23 '20 at 09:08
3

If your using maven,

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <profiles>
                        <profile>dev</profile>
                    </profiles>
                </configuration>
            </plugin>
        </plugins>
    </build>

this set dev as active profile

./mvnw spring-boot:run

will have dev as active profile.

Randika Hapugoda
  • 348
  • 3
  • 18
2

Create specific .yml files in the resources directory for each and every environment(Eg: dev,qa,stg etc.) that you need to run the application. image of .yml files in resources directory

If you are using spring-boot-maven-plugin 2.0.5.RELEASE in your pom.xml file you can add the profiles within the dependency tag as follows. image of pom.xml spring-boot-maven-plugin (you can configure multiple profiles using multiple profile tags)

Then you can use the following commands to build and run the project.

1) mvn clean install
2) mvn spring-boot:run -Dspring-boot.run.default==qa

Then you will see that the default profile is set as qa while running the project. displaying the default profile when running the application

2

Working with Intellij, because I don't know how to set keyboard shortcut to mvn spring-boot:run -Dspring.profiles.active=dev, I have to do this:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <jvmArguments>
            -Dspring.profiles.active=dev
        </jvmArguments>
    </configuration>
</plugin>
Sida Zhou
  • 3,529
  • 2
  • 33
  • 48
1

Use "-Dspring-boot.run.profiles=foo,local" in Intellij IDEA. It's working. Its sets 2 profiles "foo and local".

Verified with boot version "2.3.2.RELEASE" & Intellij IDEA CE 2019.3.

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

Setting profile with "mvn spring-boot:run" enter image description here

Setting environment variable enter image description here

harshit2811
  • 827
  • 1
  • 7
  • 21
0

The @Profile annotation allows you to indicate that a component is eligible for registration when one or more specified profiles are active. Using our example above, we can rewrite the dataSource configuration as follows:

@Configuration
@Profile("dev")
public class StandaloneDataConfig {

    @Bean
    public DataSource dataSource() {
        return new EmbeddedDatabaseBuilder()
            .setType(EmbeddedDatabaseType.HSQL)
            .addScript("classpath:com/bank/config/sql/schema.sql")
            .addScript("classpath:com/bank/config/sql/test-data.sql")
            .build();
    }
}

And other one:

@Configuration
@Profile("production")
public class JndiDataConfig {

    @Bean(destroyMethod="")
    public DataSource dataSource() throws Exception {
        Context ctx = new InitialContext();
        return (DataSource) ctx.lookup("java:comp/env/jdbc/datasource");
    }
}
Adam Ostrožlík
  • 1,256
  • 1
  • 10
  • 16
0

Alternatively, the profile can be directly specified in the application.properties file by adding the line:

spring.profiles.active=prod

Profiles work in conjunction with Spring Boot properties files. By default, Spring Boot parses a file called application.properties – located in the src/main/resources directory – to identify configuration information.

Our first task will be to add a parameter in that file which will tell Spring to use a different environment-specific property file corresponding to the active profile (i.e. the profile that the app is currently being run with). We can do this by adding the following to the application.properties file:

spring.profiles.active=@activatedProperties@

Now we need to create the two new environment-specific property files (in the same path as the existing application.properties file), one to be used by the DEV profile and one to be used by the PROD profile. These files need to be named the following:

application-dev.properties

application-prod.properties

In each case, we specify prod as the active profile, which causes the application-prod.properties file to be chosen for configuration purposes.