Maven spring boot run debug with arguments

JavaSpringMavenSpring BootMaven 3

Java Problem Overview


Usually I'm running my Spring Boot application with command:

mvn spring-boot:run -Drun.arguments=--server.port=9090 \
   -Dpath.to.config.dir=/var/data/my/config/dir

I want to set custom port to debug, so I can connect from eclipse. When I add arguments from example https://docs.spring.io/spring-boot/docs/1.1.2.RELEASE/maven-plugin/examples/run-debug.html

mvn spring-boot:run -Drun.arguments=--server.port=9090 \
   -Dpath.to.config.dir=/var/data/my/config/dir \
   -Drun.jvmArguments="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,address=8787"

it works but other arguments like server.port or path.to.config.dir are no longer recognized and I get exception like:

org.springframework.beans.factory.BeanDefinitionStoreException: Failed
to parse configuration class [com.my.app.Controller]; nested exception
is java.lang.IllegalArgumentException: Could not resolve placeholder
'path.to.config.dir' in string value
file:///${path.to.config.dir}/some.properties"

Question: How I can run with all arguments?

Java Solutions


Solution 1 - Java

The parameter name has to be prefixed with spring-boot. as in -Dspring-boot.run.jvmArgument

The Spring Boot documentation provided me the solution as I'm running Spring Boot 2.0.3

mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"

Solution 2 - Java

The behavior and the change you notice is happening because you started using the jvmArguments option: > JVM arguments that should be associated with the forked process used to run the application. On command line, make sure to wrap multiple values between quotes.

By default, when using it, the Spring Boot Maven plugin will also fork its execution, as described by the fork option: > Flag to indicate if the run processes should be forked. By default process forking is only used if an agent or jvmArguments are specified.

Hence, the usage of jvmArguments also activated the fork mode of the plugin execution. By forking, you are actually not picking up the others -D arguments passed from command line.

Solution: if you want to use the jvmArguments, then pass all of the required arguments to it.

mvn spring-boot:run -Drun.jvmArguments="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,address=8787 -Dserver.port=9090 -Dpath.to.config.dir=/var/data/my/config/dir"

-- Edit 22/09/2020 Check also the other answer from @Stephane to complent this answer (prefix of parameters)

Solution 3 - Java

Notice that from spring-boot 2.0 names have changed. For more details check out:

https://docs.spring.io/spring-boot/docs/2.0.2.RELEASE/maven-plugin/run-mojo.html

  • run.jvmArguments -> spring-boot.run.jvmArguments
  • run.arguments -> spring-boot.run.arguments

Solution 4 - Java

Override spring-boot properties from Maven command line using Powershell:

  • Spring Boot 2.4.4
  • Maven 3.6.3
  • Powershell Windows 10

This worked for me:

mvn spring-boot:run  -D"spring-boot.run.jvmArguments"="-Dimport.dataset.list=importpb"

Solution 5 - Java

One more issue is there. If pom.xml defines jvmArguments then command line arguments are ignored

<plugin>
    <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <jvmArguments>-Xmx2048m -XX:NativeMemoryTracking=summary --enable-preview</jvmArguments>
        </configuration>
</plugin>

If I would like to add debug properties to one of launchers

-Dspring-boot.run.jvmArguments="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8282"

It is not gonna work.

Solution 6 - Java

Following redhat documentation ..

mvn spring-boot:run -Drun.jvmArguments="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=$PORT_NUMBER"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionwbkView Question on Stackoverflow
Solution 1 - JavaStephaneView Answer on Stackoverflow
Solution 2 - JavaA_Di-MatteoView Answer on Stackoverflow
Solution 3 - JavajalogarView Answer on Stackoverflow
Solution 4 - JavassimmView Answer on Stackoverflow
Solution 5 - JavasimarView Answer on Stackoverflow
Solution 6 - JavadannybastosView Answer on Stackoverflow