Spring 引导:将系统属性传递给 Maven

Spring Boot: passing system properties to maven

我试过了:

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使用运行这个文件是没有问题的。

有什么建议吗?

尝试 运行 您的应用程序:

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

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

当前版本(>=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

我也尝试了很多方法。但最后,对我有用的是将它们设置在 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>