i have application.yml
,application-dev.yml
andapplication-dev.yml
mvn spring-boot:run
Since Spring Boot v2+
I have verified with Spring Boot v2.3.5.RELEASE
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
(Note order)
or
java -jar app.jar --spring.profiles.active=dev
(Note order)
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");
}
}
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/examples/run-profiles.html)
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
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
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>