Active profile not getting set during mvn test - spring-boot

I am currently doing karate testing, and while running my tests with active profile set to as "local" the application tries to boot up with active profile: "prd".
mvn spring-boot:run -Dspring-boot.run.profiles=local
When I run my application with the above command the active profile is correctly set to local.
mvn test -Dspring-boot.run.profiles=local -Dkarate.env=local -Pkarate
When I run my karate test with the above command, the application tries to boot up with active profile set to as "prd", which is the last available profile in the yaml file. When I change the order of profiles in the yaml file, still the last available profile is fetched during boot up.

Related

Springboot ActiveProfile annotation is triggering test regardless of active profile being used

My Springboot REST API application uses RESTAssured integration tests with JUnit 5. I would like to be able to isolate these RESTAssured test so they only run against in memory H2 database and to do so, I use profiles like:
application.profile (which sets active profile to "development" or "production")
application-dev.profile (development profile using in memory H2 database for testing)
application-prod.profile (production profile using real database)
I am trying to isolate my RestAssured tests so that when I run maven > install, they will only run while I use "dev" profile.
If I use "production" profile, I would like not to run them as I dont want RestAssured tests to fire against real database.
I have my RestAssured test class annotated with #ActiveProfiles("dev") as:
#ActiveProfiles("dev")
class MyControllerRestAssuredTest { ... }
I would expect that if when my application.profile sets active profile like:
spring.profiles.active=dev, maven > install would run MyControlerRestAssuredTest
spring.profiles.active=prod, maven > install would NOT run MyControlerRestAssuredTest
However, regardless of how I set my active profile, maven > install will run the MyControlerRestAssuredTest tests.
What is that I am missing?
Try setting:
spring.profiles.active = dev
in application-test.properties

How to debug spring boot maven project in debug mode in Intellij?

I run my maven project with the following command for dev profile from my terminal
sudo mvn spring-boot:run -Dspring.profiles.active=dev
How can I run my project to run in debug mode with given profile. How to set configuration in intellij for this?
Running a Spring Boot project from Maven with a specific profile
mvn spring-boot:run -Dspring-boot.run.profiles=foo,bar
Or by using a shell variable
SPRING_PROFILES_ACTIVE=foo mvn spring-boot:run
Or by passing arguments to the JVM
mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Dspring.profiles.active=foo,bar"
These are the only methods that I know of that work for Spring Boot v2.0+.
The first option is recognized by the Spring Boot Maven plugin and passes it on to the application JVM.
Since version 2.0 of Spring Boot, the run goal forks the process by default. Since -Dspring.profiles.active is not recognized by the plugin directly, it's only seen by the Maven process and not passed on to the app itself. This is why it doesn't work in the form mvn spring-boot:run -Dspring.profiles.active=foo,bar.
In the second option, the shell variable should be visible to any subprocesses spawned from that shell.
Starting a Spring Boot project in debug mode from Maven
mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"
Putting it together
mvn spring-boot:run -Dspring-boot.run.profiles=foo,bar -Dspring-boot.run.jvmArguments="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"
Alternative, passing all arguments to JVM
mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005 -Dspring.profiles.active=foo,bar"
The Maven pom.xml should include the Spring Boot plugin
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.0.1.RELEASE</version>
</plugin>
In IntelliJ you should create a new "Remote" debug configuration from the "Run/Debug Configurations" tool window. You'll find it in the main menu - "Run / Edit Configurations..."
The default config will use the same 5005 port.
After that, launch that debug config. The console should display "Connected to the target VM...".
Sources:
https://docs.spring.io/spring-boot/docs/2.0.1.RELEASE/maven-plugin/examples/run-profiles.html
https://docs.spring.io/spring-boot/docs/2.0.1.RELEASE/maven-plugin/examples/run-debug.html
If you're running from maven, then add the following parameters:
mvn spring-boot:run -Dspring.profiles.active=dev -Drun.jvmArguments="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5555"
Here 5555 is a debug port number (you can use any other unoccupied port).
Then in IntelliJ you can use Remote Debug configuration and connect to that port.
If you open the pom.xml from intelliJ, you can create a Run Configuration with --spring.profiles.active=dev and main class that is a class with method main just like in a regular the most simple java application.
Just click run button (green triable button) then click Debug...
IntelliJ will run your spring-boot app in debugging mode
If you want to run with arguments just open edit configuration and put your args in VM Options/Program arguments like

Running Spring Boot Application.main() via IntelliJ doesn't find my local profile

I've been running my Spring Boot (2.1.0) app via Gradle bootRun but since adding in the Actuator, this causes the app to not actually shutdown when I tell IntelliJ to stop the app. For whatever reason, this is only an issue when running in Debug mode.
The solution I found online was to just run the Application.main() but now, my local profile isn't being picked up. So I have the typical application.yml along with various other profiles. All of the developers have an application-local.yml that is sitting at the root of the project and is ignored by git. Running Gradle's bootRun with local as the active profile works. But running Application.main() with the local active profile does not pick up the application-local.yml file's config.
As you can see in the image, I've not only specified the profile in Intellij's Active profiles section, but also as a VM option just like I do when running via Gradle.
One more thing which you can try
Set spring.profiles.active=local in Environment Variables below VM Options

how to run maven command line with specified spring profiles?

I'm running spring boot app with mvn command line. how can I active the specified spring profile? I tried this but with no luck:
mvn spring-boot:run -Drun.arguments="some parameters" -Dspring.profiles.active=schedule
Use run.profiles, for example:
mvn spring-boot:run -Drun.arguments="some parameters" -Drun.profiles=schedule
More details in the docs:
The active profiles to use for a particular application can be specified using the profiles argument
The profiles to enable can be specified on the command line as well

tomcat7-maven-plugin to deploy spring boot with appropriate spring profile selected

My goal is to be able to use the tomcat-maven plugin to deploy my spring boot application from the command line where an argument is supplied that tells spring which profile to use like this:
mvn tomcat7:deploy -Dspring.profiles.active="dev"
I've tried several different things such as the solution described here but the default application.properties is still always selected.
The only way that I've been able to get the application-dev.properties selected is by using
mvn spring-boot:run -Dspring.profiles.active="dev"
But we don't want to have tomcat packaged in our war
I'm new to maven and spring boot and I've been spinning my wheels for the better part of a a day now so any advice would be appreciated.
Consider using MAVEN_OPTS environment variable to set VM argument. (Linux/osx) example you would need to execute before your maven goal:
export MAVEN_OPTS="-Dspring.profiles.active=dev"
I found out the issue and I was able to get the correct profile selected using
export SPRING_PROFILES_ACTIVE=dev. The problem that I was having was when I was starting my local tomcat server through the eclipse UI my environment variables were being ignored. When starting tomcat through startup.bat the environment variable gets used and spring uses the correct profile.

Resources