[spring] How do I activate a Spring Boot profile when running from IntelliJ?

I have 5 environments:

 - local (my development machine)
 - dev
 - qc
 - uat
 - live
 - staging

I want different application properties to be used for each environment, so I have the following properties files each which have a different URL for the datasource:

 - application.properties  (containing common properties)
 - application-local.properties
 - application-dev.properties
 - application-qc.properties
 - application-uat.properties
 - application-live.properties

I am using IntelliJ and running my app using bootRun in the Gradle plugin on my local machine. I will be using deploying the same application war file on all other environments which run Tomcat.

I have tried adding:

--spring.profiles.active=local

to the run configuration under script parameters.

I have tried adding

-Dspring.profiles.active=local

to the run configuration under VM options.

Neither work. I keep seeing the INFO message on startup say: No active profile set, falling back to default profiles: default

If I run my app from the windows command line using

gradle bootRun

but I first set the environment variable

set SPRING_PROFILES_ACTIVE=local

Then everything works.

So my question is, how do I activate my local spring boot profile when running bootRun from IntelliJ ?

This question is related to spring intellij-idea gradle spring-boot

The answer is


For Spring Boot 2.1.0 and later you can use

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


Try add this command in your build.gradle

enter image description here

So for running configure that shape:

enter image description here


Set -Dspring.profiles.active=local under program arguments.


If you actually make use of spring boot run configurations (currently only supported in the Ultimate Edition) it's easy to pre-configure the profiles in "Active Profiles" setting.

enter image description here


In my case I used below configuration at VM options in IntelliJ , it was not picking the local configurations but after a restart of IntelliJ it picked configuration details from IntelliJ and service started running.

-Dspring.profiles.active=local

A probable cause could be that you do not pass the command line parameters into the applications main method. I made the same mistake some weeks ago.

public static final void main(String... args) {
    SpringApplication.run(Application.class, args);
}

Replace your profile name with BE

You can try the above way to activate a profile


Spring Boot seems had changed the way of reading the VM options as it evolves. Here's some way to try when you launch an application in Intellij and want to active some profile:

1. Change VM options

Open "Edit configuration" in "Run", and in "VM options", add: -Dspring.profiles.active=local

It actually works with one project of mine with Spring Boot v2.0.3.RELEASE and Spring v5.0.7.RELEASE, but not with another project with Spring Boot v2.1.1.RELEASE and Spring v5.1.3.RELEASE.

Also, when running with Maven or JAR, people mentioned this:

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

or

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

(See here: how to use Spring Boot profiles)

2. Passing JVM args

It is mentioned somewhere, that Spring changes the way of launching the process of applications if you specify some JVM options; it forks another process and will not pass the arg it received so this does not work. The only way to pass args to it, is:

mvn spring-boot:run -Dspring-boot.run.jvmArguments="..."

Again, this is for Maven. https://docs.spring.io/spring-boot/docs/current/maven-plugin/examples/run-debug.html

3. Setting (application) env var

What works for me for the second project, was setting the environment variable, as mentioned in some answer above: "Edit configuration" - "Environment variable", and:

SPRING_PROFILES_ACTIVE=local

I ended up adding the following to my build.gradle:

bootRun {
  environment SPRING_PROFILES_ACTIVE: environment.SPRING_PROFILES_ACTIVE ?: "local"
}

test {
  environment SPRING_PROFILES_ACTIVE: environment.SPRING_PROFILES_ACTIVE ?: "test"
}

So now when running bootRun from IntelliJ, it defaults to the "local" profile.

On our other environments, we will simply set the 'SPRING_PROFILES_ACTIVE' environment variable in Tomcat.

I got this from a comment found here: https://github.com/spring-projects/spring-boot/pull/592


I use the Intellij Community Edition. Go to the "Run/Debug Configurations" > Runner tab > Environment variables > click button "...". Add: SPRING_PROFILES_ACTIVE = local

spring.profiles.active


Try this. Edit your build.gradle file as followed.

ext { profile = project.hasProperty('profile') ? project['profile'] : 'local' }

So for resuming...

If you have the IntelliJ Ultimate the correct answer is the one provided by Daniel Bubenheim

But if you don't, create in Run->Edit Configurations and in Configuration tab add the next Environment variable:

SPRING_PROFILES_ACTIVE=profilename

And to execute the jar do:

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

Examples related to spring

Are all Spring Framework Java Configuration injection examples buggy? Two Page Login with Spring Security 3.2.x Access blocked by CORS policy: Response to preflight request doesn't pass access control check Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified Spring Data JPA findOne() change to Optional how to use this? After Spring Boot 2.0 migration: jdbcUrl is required with driverClassName The type WebMvcConfigurerAdapter is deprecated No converter found capable of converting from type to type

Examples related to intellij-idea

IntelliJ: Error:java: error: release version 5 not supported Has been compiled by a more recent version of the Java Runtime (class file version 57.0) Error: Java: invalid target release: 11 - IntelliJ IDEA IntelliJ can't recognize JavaFX 11 with OpenJDK 11 Error: JavaFX runtime components are missing, and are required to run this application with JDK 11 ERROR Source option 1.5 is no longer supported. Use 1.6 or later Cannot inline bytecode built with JVM target 1.8 into bytecode that is being built with JVM target 1.6 How to configure "Shorten command line" method for whole project in IntelliJ intellij idea - Error: java: invalid source release 1.9 Failed to resolve: com.google.android.gms:play-services in IntelliJ Idea with gradle

Examples related to gradle

Gradle - Move a folder from ABC to XYZ A failure occurred while executing com.android.build.gradle.internal.tasks Gradle: Could not determine java version from '11.0.2' Android Gradle 5.0 Update:Cause: org.jetbrains.plugins.gradle.tooling.util Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0 Failed to resolve: com.android.support:appcompat-v7:28.0 Failed to resolve: com.google.firebase:firebase-core:16.0.1 com.google.android.gms:play-services-measurement-base is being requested by various other libraries java.lang.NoClassDefFoundError:failed resolution of :Lorg/apache/http/ProtocolVersion Error - Android resource linking failed (AAPT2 27.0.3 Daemon #0)

Examples related to spring-boot

Access blocked by CORS policy: Response to preflight request doesn't pass access control check Why am I getting Unknown error in line 1 of pom.xml? Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured How to resolve Unable to load authentication plugin 'caching_sha2_password' issue ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified After Spring Boot 2.0 migration: jdbcUrl is required with driverClassName ERROR Source option 1.5 is no longer supported. Use 1.6 or later How to start up spring-boot application via command line? JSON parse error: Can not construct instance of java.time.LocalDate: no String-argument constructor/factory method to deserialize from String value