I am trying to run the Spring application from the server.
What is the difference between:
mvn spring-boot:run -Dspring.profiles.active=dev
and
mvn spring-boot:run -Dspring-boot.run.profiles=dev
Tnx
The difference is that the second property, spring-boot.run.profiles, comes from the Spring Boot Maven plugin which allows you to define profiles in your pom.xml, while spring.profiles.active comes from spring and can be used without any plugin, but in the end, they can do the same thing.
Related
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
In my team I frequently (>10) see Spring Boot projects with the following instructions for running locally:
mvn clean spring-boot:run -Dspring.profiles.active=dev,runtime
My understanding of Spring Boot profiles is that each one corresponds to a particular environment, eg dev, test - so you would never need more than one. Also that each profile name corresponds to a particular env file - eg src/main/java/resources/application-dev.yml application-test.yml etc.
I would have thought that you would run the app on your local machine with:
mvn clean spring-boot:run -Dspring.profiles.active=local
And have the local configuration in:
src/main/java/resources/application-local.yml
This is running:
Java 11
maven parent is org.springframework.boot spring-boot-starter-parent 2.2.6.RELEASE
maven version is 3.3.9
My question is: Is there any meaning to running -Dspring.profiles.active=dev,runtime when running Spring Boot locally?
I am getting confused about how to use profiles within a Spring Boot App.
Assuming that I defined profiles in pom.xml file,
Here is the different ways that I find.
mvnw -Pdev
mvnw spring-boot:run -Dspring-boot.run.profiles=dev
mvnw spring-boot:run -Dspring.profiles.active=dev
mvnw spring-boot:run --spring.profiles.active=dev
1/ Is there any difference between them ?
2/ What is the best approach to use ?
You should not confuse Maven profiles and Spring profiles - this is completely different things.
mvnw spring-boot:run -Dspring.profiles.active=dev - it is telling a Spring which profile to use at runtime.
Spring Profile - is how your Spring application will be configured at the runtime, based on the active profile.
You can read more about it here and here.
mvnw -P dev - it telling a Maven to use build Maven profile
Maven Profile - is how your Java application will be compiled/build/packed. It is related to the compiling/packaging, not runtime execution of your Spring application, and it doesn't related to Spring at all.
You can read more about it here and here.
This is answer to your first question.
Regarding your second question,
Maven profile are used when you need to build your application differently based on the profile settings. For example, pack a "fat jar" with all dependencies when you execute mvn -P fat-jar.
Spring profile is used when you want to configure your Spring application to work differently at runtime, for example - to use development and production database, etc.
mvnw -Pdev
This will consider maven profile
mvnw spring-boot:run -Dspring-boot.run.profiles=dev
This will consider maven profile
mvnw spring-boot:run -Dspring.profiles.active=dev
This will consider spring boot profile
mvnw spring-boot:run --spring.profiles.active=dev
This shouldn't work as maven don't understand --spring
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.
I have a spring boot application and I want to pass application.properties file in commandLine when I start-up.
i.e when I run mvn spring-boot:run --application.properties
I will have a default application.properties in src/main/resources. but that is only for testing purposes. In the production run, I would like to pass the property file in commandLine.
I am aware of passing single arguments such as
mvn spring-boot:run --server.port=9001.
But I have many such properties and would prefer to pass a property file if that is possible.
You can do that with spring.config.location property:
mvn spring-boot:run -Dspring.config.location=your.properties
In case if anyone finds it useful as it was for me. If you want to pass in individual application properties as parameters when using the maven spring boot run command you can use the argument spring-boot.run.jvmArguments.
Eg:
mvn spring-boot:run -Dspring-boot.run.jvmArguments='
-Dspring.datasource.url=jdbc:postgresql://localhost:5432/mydb
-Dspring.datasource.username=admin
-Dspring.datasource.password=admin'
With the above command I am setting (overriding) the following properties which were in the application.properties file.
spring.datasource.url=jdbc:postgresql://localhost:5432/mydb
spring.datasource.username=admin
spring.datasource.password=admin
mvn spring-boot:run -Dspring-boot.run.arguments=--spring.config.location=classpath:/application-local.properties