java - 如何将 groovy (gradle) 内部的 vm 参数设置为 Tomcat?

标签 java spring tomcat groovy build.gradle

我想从 gradle 设置 vm 参数:

例如,要启动 Java 应用程序,我会这样做:

-Dspring.profiles.active=dev

我想做同样的事情,但不是来自命令行,而是来自 gradle

我尝试了不同的变体,但它不起作用:

System.properties['spring.profiles.active'] = "dev"
System.setProperty('spring.profiles.active',"dev")

如何从 Gradle 设置 System.properties?我需要在 tomcat 上作为 .war 文件运行 spring 应用程序,我不能将它作为系统变量传递

我需要它因为 https://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html

69.6 Set the active Spring profiles

The Spring Environment has an API for this, but normally you would set a System property (spring.profiles.active) or an OS environment variable (SPRING_PROFILES_ACTIVE). E.g. launch your application with a -D argument (remember to put it before the main class or jar archive):

$ java -jar -Dspring.profiles.active=production demo-0.0.1-SNAPSHOT.jar

In Spring Boot you can also set the active profile in application.properties, e.g.

spring.profiles.active=production

A value set this way is replaced by the System property or environment variable setting, but not by the SpringApplicationBuilder.profiles() method. Thus the latter Java API can be used to augment the profiles without changing the defaults.

最佳答案

这个有效:

bootRun {
    systemProperty 'spring.profiles.active', 'dev'
}

我在这里找到了答案: https://github.com/spring-projects/spring-boot/pull/592

关于java - 如何将 groovy (gradle) 内部的 vm 参数设置为 Tomcat?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41610100/

相关文章:

java - 如何仅针对给定的单个端点忽略 JsonProperty 并让它适用于其他端点

java - Spring Boot ,gradle,angularJS和一个 fat jar

java - 在哪里可以找到 org.springframework.cassandra.config jar。我看到的只是 org.springframework.data.cassandra

tomcat - 将非 SSL 流量正确重定向到 SSL 流量

java - eclipse rap下载服务servlet报错

java - 代码在 IDE 内部运行但在外部失败

java - 如何找到接口(interface)方法的一个实现的用法

java - Spring Boot 和配置文件

spring - @Autowired 在 Scala 类的构造函数上

java - MBean 可以在 Tomcat 下运行吗?