Reputation: 1916
I have the application.yml
, application-dev.yml
and application-dev.yml
files.
The command mvn spring-boot:run -Dspring.profiles.active=dev
doesn't work, so I cannot choose the dev
profile using mvn spring-boot:run
that way. How do I choose it?
The documentation says java -jar XXX.jar --spring.profiles.active=dev
should work. When I use java -jar XXX.jar
it runs, but if I use java -jar XXX.jar --spring.profiles.active=dev
to choose dev
profile, many messages are logged to the console, including a warning that I should never use java -jar XXX.jar
, and ultimately the run fails with "APPLICATION FAILED TO START".
I also tried -Dspring.profiles.active=dev
, but it does not work.
How can I solve these problems?
Upvotes: 155
Views: 318146
Reputation: 6359
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
Upvotes: 249
Reputation: 1949
If you are using the Spring Boot Maven Plugin, run:
mvn spring-boot:run -Dspring-boot.run.profiles=foo,bar
Upvotes: 56
Reputation: 1771
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
Upvotes: 45
Reputation: 102
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.
Upvotes: 0
Reputation: 358
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.
Upvotes: 4
Reputation: 867
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"
Upvotes: 1
Reputation: 3715
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>
Upvotes: 3
Reputation: 1219
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
Upvotes: 5
Reputation: 569
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
Upvotes: 7
Reputation: 21
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
Upvotes: 2
Reputation: 1426
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");
}
}
Upvotes: 0
Reputation: 542
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>
Upvotes: 8
Reputation: 4381
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
Upvotes: 4