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
Related
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.
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
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
I'm running an application (in GitBash on Windows) with:
mvn spring-boot:run
Except that the application is looking for a properties file, which I'd like to make available in the classpath. But I don't want to have to change any code in this application.
Is there a way to specify additional classpaths when running this command? I've tried various forms of:
mvn spring-boot:run -Dclasspath="C:\\path\\to\\config\\dir"
or
mvn spring-boot:run -Dclasspath=/C/path/to/config/dir
And I've tried setting $CLASSPATH.
None of this works, but I don't get a clear idea about where it is breaking because the only error is that my properties file cannot be found.
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