291

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 ?

dleerob
  • 4,951
  • 4
  • 24
  • 36
  • 1
    Why are you running the application via gradle there? Wouldn't it be 10 times more convenient to use the run configuration? There's a field where you can set the profiles to enable... – Stephane Nicoll Sep 28 '16 at 06:40
  • 1
    I am using the run configuration in IntelliJ, as explained above. It's not working. – dleerob Sep 28 '16 at 06:47
  • 2
    No you're not. What I am talking about is the "Spring Boot run configuration" Run -> Edit configuration > New > Spring Boot. – Stephane Nicoll Sep 28 '16 at 06:50
  • Aah yes, I moved away from Spring Boot run configuration as I needed to expand project.properties into application.properties in my build.gradle and if I used the Spring Boot run configuration, it didn't seem to work. I'll look into resolving that issue and then perhaps I can simply use the active profile field as you suggested – dleerob Sep 28 '16 at 06:57
  • 1
    Using the Spring Boot configuration seems more trouble than its worth. The 'Make' simply copies across the resources and doesn't filter/alter them as per by build script. Then telling it to run the 'build' from gradle instead of 'make' simply causes the run to freeze. If I use bootRun instead, along with my environment entry as per below answer, all works fine. – dleerob Sep 28 '16 at 07:08
  • Done, although when I originally tried that in 2016 as mentioned in my original post, it wasn't working. Whatever the issue was, it must have been fixed since. – dleerob Jun 14 '18 at 12:35

18 Answers18

428

I added -Dspring.profiles.active=test to VM Options and then re-ran that configuration. It worked perfectly.

This can be set by

  • Choosing Run | Edit Configurations...
  • Go to the Configuration tab
  • Expand the Environment section to reveal VM options
Brad Parks
  • 66,836
  • 64
  • 257
  • 336
Thom
  • 14,013
  • 25
  • 105
  • 185
  • 1
    I was stuck in the same situation but for a Maven project. Thanks for the help. We have set the VM option for each and every test file. – Sri9911 May 07 '18 at 08:21
  • 2
    It doesn't work for me too. I use gradle and two modules (api and ui) – Virkom Apr 02 '20 at 07:19
  • 2
    Doesn't work for me either, also using gradle here. – IARI Apr 23 '20 at 11:49
  • 1
    HI, I have the same issue but this fix does not fix the issue. what is meant here by reran the configuration? is it as just as rerunning the task? – user1456110 Jul 06 '20 at 15:29
  • I ran that particular test after editing it as shown. – Thom Jul 06 '20 at 16:15
  • any other ideas why its not picking up the profile in my case. In edit config, under VM options I set "-Dspring.profiles.active=dev". I have two file application-dev.properties and application-prod.properties under resources folder. But its not picking up. I am using 2019 intellij version. – user1456110 Jul 14 '20 at 09:34
  • This works only for configurations of type "Application" (running the public static main method). It will not work if your configuration is of type maven or gradle (mvn spring-boot:run) – maddob Jul 16 '20 at 10:40
  • 2
    Usually, spring.profiles.include is preferrable, because spring.profiles.active also disables other profiles. Deactivating default profiles might be undesired. – Christian May 14 '21 at 11:34
  • Now you have to go to modify options. Then environment variables are available for change – Jorge Tovar Apr 11 '22 at 16:43
  • for me, I found it like so: Run -> Edit configurations -> Modify options -> check the 'add VM options' to reveal it then you can add -Dspring.profiles.active=dev in the space provided for VM options – HibaHasan Jun 24 '22 at 13:15
132

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

informatik01
  • 16,038
  • 10
  • 74
  • 104
Daniel Bubenheim
  • 4,043
  • 3
  • 14
  • 20
  • 2
    Thanks, I'm sure this would work, however I am not using the Spring Boot run configuration, I am using the Gradle run configuration which doesn't have the "Active Profiles" field. My gradle build filters and modifies some of the properties files it copies to the build directory, so I am using the Grade run configuration for this. – dleerob Sep 30 '16 at 12:28
  • As an above answer pointed out: if this isn't working for you, ensure that you're passing in the `args` in your `Application.main()` eg `SpringApplication.run( Application.class, args );` – Xaero Degreaz Dec 01 '16 at 21:13
  • 4
    This feature is supported in the Ultimate edition only. – Julien Malige Jul 17 '17 at 09:27
  • Thanks @JulienMalige for pointing this out. I will edit my answer. – Daniel Bubenheim Jul 17 '17 at 09:33
  • You can use a comma separate list of them as you would in the cli so for example `default, staging`. The far right takes highest precedence so for example `foo=bar` (default) and `foo=baz` (staging), will result in `foo=baz` – 8bitme Jun 14 '22 at 11:19
62

Tested with IntelliJ Community edition 2021.x

You can create Multiple configurations, one each for a specific profile, In my case below, I have created a dev config with dev profile environment variable.

  1. Goto Run > Edit Configuration
  2. Choose the configuration you want to edit, in the left under Application.
  3. On the right side > Under Environment Variable, update spring.profiles.active=<your profile name> example spring.profiles.active=dev (observer:- the variable should be without -D flag)
  4. Save the changes and Run the Spring boot app with the same configuration.

Note:- You can also create a new configuration or copy existing in step 2 above, using the option available in the same panel.

enter image description here

puneetShanbhag
  • 739
  • 6
  • 7
  • 2
    This answer worked for me, I'm using the gradle bootRun configuration on Spring Boot 2.4.2 and the other answers would not change my active profile. Thanks! – Daniel Black May 12 '22 at 18:19
  • 2
    Only this one works for non-springboot app – Stan Nov 08 '22 at 23:24
46

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
WesternGun
  • 11,303
  • 6
  • 88
  • 157
  • 3
    The env var should actually be `SPRING_PROFILES_ACTIVE`. – Josh M. May 31 '19 at 12:23
  • 1
    I added it in "Run configuration" of IDEA "VM options" like "spring.profiles.active" and it works. Maybe another form like urs is used system-wide in system properties? – WesternGun May 31 '19 at 12:54
  • If you're adding it to `~/.profile` or similar, you have to use `SPRING_PROFILES_ACTIVE` -- `spring.profiles.active` likely only works as an argument on the command line. – Josh M. Jun 01 '19 at 01:42
  • 1
    Ah.. so you mean the same as I suspected; real system env not just the application env. OK I make an edit. – WesternGun Jun 02 '19 at 15:46
  • This answer is very concise and complete – absentia Nov 10 '20 at 01:47
18

Try add this command in your build.gradle

enter image description here

So for running configure that shape:

enter image description here

emerson moura
  • 345
  • 2
  • 3
11

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

dleerob
  • 4,951
  • 4
  • 24
  • 36
11

For Spring Boot 2.1.0 and later you can use

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

QuickSilver
  • 3,915
  • 2
  • 13
  • 29
Mr. K.
  • 111
  • 1
  • 4
6

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);
}
Hubert Ströbitzer
  • 1,745
  • 2
  • 15
  • 19
  • Thanks for the reply and suggestion, however I am passing the arguments in. – dleerob Sep 30 '16 at 12:26
  • This actually fixed my problem. For some reason when I created the project using Gradle, it didn't auto-generate my `Application` class so did it by hand from memory. Missed the `args` step, and so the "Active Profiles" box in the run configuration didn't work -- I had to manually pass in `-Dspring.profiles.active` in the VM options box. – Xaero Degreaz Dec 01 '16 at 21:11
3

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

Ryan Wibawa
  • 300
  • 3
  • 4
3

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
Nikos Hidalgo
  • 3,666
  • 9
  • 25
  • 39
3

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
1

This is worked for me Intellij Ultimate version 2022.3.

===== Step 1 =====

go to Run > Edit Configurations enter image description here

===== Step 2 =====

first I created a dev profile using application-dev.properties file in resources directory. you can see below popup window. enter image description here

===== Step 3 =====

Now click click Modify Option section under Build and run section to enable Environmental variables.

OR

simple you can use ALT + E shortcut for that. enter image description here

===== Step 4 =====

Now you can see like below and you can enable spring profiles(in this dev profile) using SPRING_PROFILES_ACTIVE=dev. enter image description here

Click on apply and ok and run the application.

Supun Sandaruwan
  • 1,814
  • 19
  • 19
0

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

ext { profile = project.hasProperty('profile') ? project['profile'] : 'local' }
sudoz
  • 3,275
  • 1
  • 21
  • 19
0

Replace your profile name with BE

You can try the above way to activate a profile

Amrit Malla
  • 176
  • 8
0

Here are 2 ways

  1. Using gradle project property

In build.gradle, add

bootRun{
//https://github.com/spring-projects/spring-boot/pull/592#issuecomment-880263914
    if (project.hasProperty('profiles')) {
        environment SPRING_PROFILES_ACTIVE: profiles
    } else {
        def profiles = 'dev'
        environment SPRING_PROFILES_ACTIVE: profiles
    }
}

In intellij gradle configuration, change the value "test" in "-Pprofiles" as appropriate to environment you want to run

enter image description here

  1. Using environment property

Follow answer by @Hubert https://stackoverflow.com/a/39749545/3333878

And configure the run configuration as

enter image description here

abitcode
  • 1,420
  • 17
  • 24
0

Create files properties like these

application.properties
application-dev.properties
application-prod.properties

then run

enter image description here

Vy Do
  • 46,709
  • 59
  • 215
  • 313
  • How can I enable this 'Active profiles' option as I can't see it by default. – Gurpreet Kaur Dec 29 '22 at 14:13
  • When you put application / compiled source code to environment A, profile default is A. When you put application / compiled source code to environment B, profile default is B. – Vy Do Dec 29 '22 at 15:11
  • Functionality with 'active profiles' is enabled only on IDEA Ultimate version. On community version there is not possible to set like that. – Piotr Feb 13 '23 at 14:17
0

VM option is hidden by default. Here is the right way to do it

Run->Edit Configurations->Select the application on the left menu->Add VM Options enter image description here

and then add

-Dspring.profiles.active=<profile_name>

Replace the <profile_name> with the profile, say local

Click Apply & OK.

-5

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

slartidan
  • 20,403
  • 15
  • 83
  • 131
jwenting
  • 5,505
  • 2
  • 25
  • 30