【问题标题】:Set VM option from maven config从 Maven 配置中设置 VM 选项
【发布时间】:2021-11-02 00:46:27
【问题描述】:

有没有办法从 Maven 配置中为命令行设置参数?也许是一些插件?

我需要使用以下 VM 选项在 Spring Boot 上运行我的服务:

-Dspring.config.additional-location=classpath:/config/business-config.yml

我尝试过使用maven-surefire-plugin:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
      <configuration>
        <argLine>
          -Dspring.config.additional-location=classpath:/config/business-config.yml
        </argLine>
      </configuration>
</plugin>

spring-boot-maven-plugin:

<plugin>
  <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
      <configuration>
        <jvmArguments>
          -Dspring.config.additional-location=classpath:/config/business-config.yml
        </jvmArguments>
      </configuration>
</plugin>

但这对我不起作用。我通过从 IDEA Intellij 本地运行我的 maven 项目作为 Spring Boot 应用程序进行测试。如果我在 IDEA 配置中设置了这个选项,那么它可以正常工作。

有什么想法吗?

【问题讨论】:

  • 当你构建 jar 并运行它时会发生什么?因为那是你应该只关心的
  • 没什么。构建成功,但是我通过IDEA启动服务时没有安装VM选项。没有任何异常或错误。但是,如果我在 IDEA 的编辑配置中设置了 VM 选项,那么它就会正确应用
  • “但是当我通过IDEA启动服务时没有安装VM选项” - 我说的是构建可执行jar。它不像您将在您的 IDE 中运行 prod。你知道spring的可执行jar是什么吧?

标签: java spring spring-boot maven cmd


【解决方案1】:

Maven 是构建工具,而不是运行时环境。

最简单的见The java Command:

java ... -Dproperty=value ...

设置系统属性值。 property 变量是一个不带空格的字符串,表示属性的名称。 value 变量是表示属性值的字符串。如果 value 是带空格的字符串,则将其括在引号中(例如 -Dfoo="foo bar")。

否则,您的应用程序运行所在的容器必须能够配置java 命令行参数。

【讨论】:

  • Maven 可以使用配置的构建路径执行类。由于某种原因,这还没有被利用到部署工具中。
  • @ThorbjørnRavnAndersen 是的,它默认使用测试代码(通过 Surefire/Failsafe 插件),还有其他插件可以做到这一点:exec-maven-pluginjetty:starthpi:run 等。您所说的“没有被用于部署工具”到底是什么意思?除了通过插件之外,它应该在哪里以及如何以其他方式集成到 Maven 中?
猜你喜欢
  • 2010-10-28
  • 2021-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-03
  • 2019-07-01
  • 1970-01-01
  • 2019-01-03
相关资源
最近更新 更多