Why is run.profiles getting ignored with spring-boot:run? - maven

Using spring-boot-maven-plugin version 1.5.9.RELEASE. The documentation states using -Drun.profiles to activate the active profiles. But running on the command line the profile is not activated:
mvn spring-boot:run -Drun.profiles=development
Using the jvmArguments parameter works and the profile is active:
mvn spring-boot:run -Drun.jvmArguments="-Dspring.profiles.active=development"
Reference:
https://github.com/spring-projects/spring-boot/issues/1095
Update:
It turned out, that the profile had been loaded correctly. Only the logging was disabled due to a conditional configuration of Logback:
In the Logback configuration I used a statement like <if condition='property("spring.profiles.active").contains("development")'>. Using the Maven plugin with -Drun.profiles does not set the system property spring.profiles.active but sets the command-line argument (--spring.profiles.active).
So, when using conditional statements in Logback only system properties and environment variables can be used.

Ensure do you have application-development.properties file. Else create it and put your properties there. By default application.properties will be considered and executed.
Based on the profile, you have create corresponding application properties file.

Related

Support of different test profiles for quarkus

Is there any support for different test profiles? During my local tests I would like to use "mvn package" which uses the "test" profile. This points to my localhost db. For my devops toolchain I want to use a different test profile because we are using containers and cannot use localhost. Goal is to distinguish between local machine test and cloud env. test.
you mean you run 'mvn packge' which leads to the tests being run - like with 'mvn test' . In this case #QuarkusTest tests will run with the 'test' profile. The same goes for running tests in the ide.
There is a system property (used with '-D') 'quarkus.test.profile'. It leads to this profile being activated:
mvn test -Dquarkus.test.profile=foo
.....
2020-04-10 14:06:20,451 INFO [io.quarkus] (main) Quarkus 1.3.0.Final started in 17.408s. Listening on: http://0.0.0.0:8081
2020-04-10 14:06:20,451 INFO [io.quarkus] (main) Profile foo activated.
You may set this property on the surefire or failsafe plugin in your pom.xml (see 1).
You may also set this property within your IDE on a run/launch configuration to start the test (IntelliJ: use the vm options field and add '-Dquarkus.test.profile=integrate')
https://quarkus.io/guides/maven-tooling
Quarkus supports custom profiles. You have two ways to set a custom profile: via the quarkus-profile system property or the QUARKUS_PROFILE environment variable.
For your needs, you can for example, use a 'staging' profile with a different db address in application.properties in this way:
%staging.db.address=value
And set the QUARKUS_PROFILE environment variable to staging to activate the profile.
you can use quarkus.profile property so at runtime it will be:mvn package -Dquarkus.profile=your_custom_profile

Profile Specific application.properties Not Applied During Unit Test

I'm writing integration tests, and I'd like to use Spring profiles to configure properties for each environment. However I'm finding profile specific application properties (e.g. application-dev.yml, application-prod.yml) in my src/test/resources directory aren't activated the same way they are in src/main/resources. It appears running tests with -Dspring.profiles.active=prod has no effect. Is it possible to activate a profile from the command line for tests?
Note: #ActiveProfiles isn't sufficient because I want to run the same tests against multiple environments.
I don't know if you have copied the file names from your workspace but 'applicaiton-prod.yml' has a typo in it. That could be the reason.
I found a similar issue reported in GitHub and the solution was applicable to my problem. The JVM system property spring.profiles.active wasn't getting picked-up in Gradle. So I modified my task as follows
integrationTest {
systemProperties = System.properties
}

Spring Boot embedded jar and application.properties location

I thought I had this working because in my IDE the correct environment properties file was being used, and a local application.properties was being correctly used. I am trying to use profile specific properties to determine various values throughout my Spring-Boot 1.3.5 app.
My src/main/resources/application.properties is:
spring.profiles.active=test
server.port=8080
deploy.server=http://localhost
liquibase.change-log:classpath:/db/changelog/db.changelog-master.xml
liquibase.check-change-log-location=true
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
logging.file=myapp.log
spring.jackson.serialization.write_dates_as_timestamps=false
And I have a src/main/resources/application-test.properties:
server.port=8081
deploy.server=https://test.myapp.com
logging.level.liquibase:TRACE
logging.level.org.springframework.security:DEBUG
Now I apparently have two problems:
After I run "gradle clean build" I get my jar and trying to run via:
java -jar build/libs/app.jar -Dspring.profiles.active=local
I see the message:
The following profiles are active: test
So it picking that up from the application.properties file and I can't seem to override it, which I think is causing my second problem:
My Jenkins build fails when I try to use a parameterized build with the values:
spring.profiles.active set to "test".
The error I get is Tomcat in a failed state. If I take that parameter out of the build, it works. But that build will not run from the command line unless I copy the application-{env}.properties to the run directory (which might be best practice anyway?).
I am not sure if the problem is the location of the properties files or the way I am trying to build them.

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 ?
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
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.
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
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.
Goto Run > Edit Configuration
Choose the configuration you want to edit, in the left under Application.
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)
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.
Try add this command in your build.gradle
So for running configure that shape:
For Spring Boot 2.1.0 and later you can use
mvn spring-boot:run -Dspring-boot.run.profiles=foo,bar
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
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);
}
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
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
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
Try this. Edit your build.gradle file as followed.
ext { profile = project.hasProperty('profile') ? project['profile'] : 'local' }
You can try the above way to activate a profile
Here are 2 ways
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
Using environment property
Follow answer by #Hubert https://stackoverflow.com/a/39749545/3333878
And configure the run configuration as
Create files properties like these
application.properties
application-dev.properties
application-prod.properties
then run
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
and then add
-Dspring.profiles.active=<profile_name>
Replace the <profile_name> with the profile, say local
Click Apply & OK.
Set -Dspring.profiles.active=local under program arguments.

Is there a way to tell gradle which profiles should be used for the tests?

I use the yml configuration files pattern application-{default,dev,production}.yml.
To define which configuration application will use, I fix the environment SPRING_PROFILES_ACTIVE=dev so when the spring app run, it choose the correct configuration.
I have now theses two issues:
The ./gradlew build command also run the test command, test need to have the correct configuration as the application does when it start.
My jenkins does not have access to the database and the units tests keep failing.
Which make make ask:
Does the build command tries all the datasource in order ? Is there a way to specify the spring boot active profile ?
Is there another different approach for deploying spring-boots app in production from jenkins ?
Does anyone has a workaround except
./gradlew -x test build
This is not what I want.
Neither adding #ActiveProfile("dev") to my tests because this require source code modification.
Simply Create multiple property files.For Example:
application.properties
application-test.properties
application-production.properties
Provide different properties based on profile and
Below you can specify
which profile to load in you gradle.build file
def profile = "test"
bootRun {
args = ["--spring.profiles.active="+profile]
}
Put below code in the end of gradle file

Resources