【问题标题】:Maven spring boot run debug with argumentsMaven spring boot 带参数运行调试
【发布时间】:2016-07-13 02:12:28
【问题描述】:

通常我使用命令运行我的 Spring Boot 应用程序:

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

我想设置自定义端口进行调试,所以我可以从 Eclipse 连接。当我从示例中添加参数时 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"

它可以工作,但其他参数(如 server.portpath.to.config.dir)不再被识别,我得到如下异常:

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"

问题:如何在所有参数的情况下运行?

【问题讨论】:

  • 就我而言,确切的解决方案是致电: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"
  • 链接失效...
  • @powder366 已修复。

标签: java spring maven spring-boot maven-3


【解决方案1】:

参数名称必须以spring-boot.为前缀,如-Dspring-boot.run.jvmArgument

Spring Boot documentation 在我运行 Spring Boot 2.0.3 时为我提供了解决方案

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

【讨论】:

  • 这应该是公认的答案,其他答案都不起作用
  • 这个答案适用于大多数人,但它在 PowerShell 中对我不起作用,我得到的消息是:No plugin found for prefix '.run.jvmArguments=-Xdebug -Xrunjdwp'
  • 谢谢。它工作mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Dserver.port=8090 -Dspring.profiles.active=local" spring-boot-maven-plugin=2.2.5.RELEASE
  • 我在 Windows 11 上运行时遇到了上面 Alex 提到的相同错误
【解决方案2】:

您注意到的行为和变化正在发生,因为您开始使用jvmArguments 选项:

应该与用于运行应用程序的分叉进程相关联的 JVM 参数。在命令行中,请确保在引号之间包含多个值。

默认情况下,Spring Boot Maven 插件在使用时也会分叉执行,如fork 选项所述:

标志以指示是否应派生运行进程。默认情况下,仅在指定代理或jvmArguments 时才使用进程分叉。

因此,jvmArguments 的使用也激活了插件执行的 fork 模式。通过分叉,您实际上并没有获取从命令行传递的其他 -D 参数。

解决方案:如果您想使用jvmArguments,则将所有必需的参数传递给它。

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"

-- 编辑 22/09/2020 还要检查@Stephane 的其他答案以完善此答案(参数前缀)

【讨论】:

  • 注意:这行得通,但我发现 -Drun.profiles 选项(我猜其他以 'run' 开头的选项)需要保留在 外部 jvmArguments 位。例如: mvn spring-boot:run -Drun.jvmArguments="-Xdebug -Dspring.foo=bar" -Drun.profiles=test
  • 你能指导我深入了解这些东西是如何工作的吗?
  • @powder366 您是否已经尝试过官方文档,上面的答案已经指出了?有目标/选项的示例和描述。您是参考 Spring Boot 特定文档还是更通用的 Maven 级别文档?
  • 我可以调试启动 Spring 应用程序,使用 mvn spring boot 插件运行它我不能断点。我想了解调试在后台是如何工作的,这样我才能理解这一切……因此寻找一些关于它的好文章。
  • 超级有帮助。谢谢。
【解决方案3】:

请注意,从 spring-boot 2.0 开始,名称已更改。更多详情请查看:

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

【讨论】:

    【解决方案4】:

    使用 Powershell 从 Maven 命令行覆盖 spring-boot 属性:

    • Spring Boot 2.4.4
    • Maven 3.6.3
    • Powershell Windows 10

    这对我有用:

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

    【讨论】:

      【解决方案5】:

      还有一个问题。如果 pom.xml 定义了 jvmArguments,那么命令行参数将被忽略

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

      如果我想向其中一个启动器添加调试属性

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

      这是行不通的。

      【讨论】:

        【解决方案6】:

        关注redhat documentation ..

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

        【讨论】:

          猜你喜欢
          • 2019-03-07
          • 2015-10-19
          • 2019-04-09
          • 2018-05-06
          • 1970-01-01
          • 2017-04-06
          • 2017-08-02
          • 2021-08-27
          • 2018-12-18
          相关资源
          最近更新 更多