【问题标题】:Spring Boot: passing system properties to mavenSpring Boot:将系统属性传递给 Maven
【发布时间】:2015-03-18 21:35:32
【问题描述】:

我试过了:

mvn -Dspring.profiles.active=dev spring-boot:run

但它不会影响我的默认配置。我用谷歌搜索了一下,发现:

mvn -DargLine="-Dspring.profiles.active=dev" spring-boot:run

但如果也失败了。

当我跑步时:

mvn package

然后:

java -Dspring.profiles.active=test -jar target/app-1.0.0.jar

它按预期工作(配置文件已更改),但无法从以这种方式加载的资源目录(FileNotFound 异常)中找到文件:

new File(getClass().getClassLoader().getResource("data.yml").getFile())

使用maven运行app时这个文件没有问题。

有什么建议吗?

【问题讨论】:

    标签: spring maven spring-boot


    【解决方案1】:

    使用当前版本(>= 2.0.0.RELEASE),参数为-Dspring-boot.run.jvmArguments

    mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Dspring.profiles.active=dev"

    来源:Running your Application with Maven

    要激活特定的配置文件,有一个可用的快捷方式:

    mvn spring-boot:run -Dspring-boot.run.profiles=dev

    来源:Specify Active Profiles

    【讨论】:

      【解决方案2】:

      尝试运行您的应用程序:

      mvn spring-boot:run -Drun.jvmArguments="-Dspring.profiles.active=dev"

      我不知道您使用的是哪个版本,但也请查看issue

      【讨论】:

      • run.jvmArguments 不明显!属性应该像环境变量一样传递给子进程。
      • 这不再适用于 Spring Boot >= 2.0.0.RELEASE。使用 spring-boot.run.jvmArguments 使用 Benoit 的答案
      【解决方案3】:

      我也尝试了很多方法。但最后,对我有用的是将它们设置在 pom.xml 中,如documentation 中所述。

      <project>
        ...
        <build>
          ...
          <plugins>
            ...
            <plugin>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-maven-plugin</artifactId>
              <version>2.2.2.RELEASE</version>
              <configuration>
                  <environmentVariables>
                      <ENV1>5000</ENV1>
                      <ENV2>Some Text</ENV2>
                      <ENV3/>
                      <ENV4></ENV4>
                  </environmentVariables>
              </configuration>
              ...
            </plugin>
            ...
          </plugins>
          ...
        </build>
        ...
      </project>
      

      【讨论】:

        猜你喜欢
        • 2017-02-10
        • 2010-10-23
        • 1970-01-01
        • 2016-11-08
        • 1970-01-01
        • 2019-06-22
        • 2017-03-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多