Spring 基于 WAR 使用 maven 的引导配置文件
Spring boot profile based WAR using maven
我需要根据特定的环境属性文件创建 WAR 文件。
所以我创建了 2 个属性文件,
application.DEV.properties
application.PROD.properties
现在,当我 运行 使用 eclipse 嵌入的项目 tomcat 时,我将 -Dspring.profiles.active=DEV
作为 VM 参数传递。然后,当我到达端点时,我可以看到返回的 DEV 相关消息。当我将 PROD 作为参数传递时也是如此。
现在,我想做的是用 maven 命令创建一个 WAR 文件,并以加载我的特定属性文件的方式传递参数。所以我参考了 google 以及 Whosebug 并找到了如下各种选项,
- mvn clean install -Drun.profiles=DEV
- mvn clean install -Drun.jvmArguments="-Dspring.profiles.active=DEV"
- mvn clean install -Dspring.profiles.active="DEV"
- mvn clean install -Dspring.profiles.active=DEV
以上我都试过了。当我点击命令时,会生成 WAR 。但它没有在 tomcat 上部署,因为它无法读取属性文件并给出错误。 WAR.
中似乎没有加载特定于配置文件的属性文件
我想知道当我想使用 maven 生成 WAR 文件时 -Dspring.profiles.active=DEV
的替代方法是什么?
如何生成 WAR 以正确包含正确的配置文件特定属性文件?
我正在使用 spring boot 1.5.14.RELEASE.
正如 Mickael 对此回答的评论,您可以从 Maven 文档中获得有关如何使用配置文件的帮助:https://maven.apache.org/guides/introduction/introduction-to-profiles.html
通常使用 maven 选择配置文件的方法是
mvn -Pprod package
其中 prod 是您个人资料的名称。如果你想用开发配置文件构建,那就是
mvn -Pdev package
此类配置文件在 project>profiles>profile
下的文件 pom.xml
中定义。在那个地方,您可以指定包装选项。
这是这样的个人资料:
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<!-- log configuration -->
<logback.loglevel>DEBUG</logback.loglevel>
<!-- default Spring profiles -->
<spring.profiles.active>dev${profile.no-liquibase}</spring.profiles.active>
</properties>
</profile>
您必须将 spring.profiles.active
传递给运行时,而不是 "build runtime"。在构建时需要的唯一 spring 个配置文件将用于测试目的。
如果是"war on Tomcat",可以设置spring.profiles.active
:
(全局)在 <tomcat_home>/bin/
中创建一个名为 setnev.sh
的文件(当您在 Windows 机器上时,分别为 .bat
):
export JAVA_OPTS="-Dspring.profiles.active=PROFILE_OF_THIS_TOMCAT"
分别是:
set JAVA_OPTS="-Dspring.profiles.active=PROFILE_OF_THIS_TOMCAT"
(全局)在<tomcat_home>/conf/catalina.properties
中添加一行:
spring.profiles.active=PROFILE_OF_THIS_TOMCAT
(在容器级别)在 $CATALINA_BASE/conf/[enginename]/[hostname]/
处添加一个名为 context.xml
的文件(您也可以将其放入 /<webapp_root>/META-INF/
,但是您将拥有以在构建时区分)具有以下内容:
<Context>
<Environment name="spring.profiles.active" value="PROFILE_OF_THIS_TOMCAT" />
</Context>
我需要根据特定的环境属性文件创建 WAR 文件。
所以我创建了 2 个属性文件,
application.DEV.properties
application.PROD.properties
现在,当我 运行 使用 eclipse 嵌入的项目 tomcat 时,我将 -Dspring.profiles.active=DEV
作为 VM 参数传递。然后,当我到达端点时,我可以看到返回的 DEV 相关消息。当我将 PROD 作为参数传递时也是如此。
现在,我想做的是用 maven 命令创建一个 WAR 文件,并以加载我的特定属性文件的方式传递参数。所以我参考了 google 以及 Whosebug 并找到了如下各种选项,
- mvn clean install -Drun.profiles=DEV
- mvn clean install -Drun.jvmArguments="-Dspring.profiles.active=DEV"
- mvn clean install -Dspring.profiles.active="DEV"
- mvn clean install -Dspring.profiles.active=DEV
以上我都试过了。当我点击命令时,会生成 WAR 。但它没有在 tomcat 上部署,因为它无法读取属性文件并给出错误。 WAR.
中似乎没有加载特定于配置文件的属性文件我想知道当我想使用 maven 生成 WAR 文件时 -Dspring.profiles.active=DEV
的替代方法是什么?
如何生成 WAR 以正确包含正确的配置文件特定属性文件?
我正在使用 spring boot 1.5.14.RELEASE.
正如 Mickael 对此回答的评论,您可以从 Maven 文档中获得有关如何使用配置文件的帮助:https://maven.apache.org/guides/introduction/introduction-to-profiles.html
通常使用 maven 选择配置文件的方法是
mvn -Pprod package
其中 prod 是您个人资料的名称。如果你想用开发配置文件构建,那就是
mvn -Pdev package
此类配置文件在 project>profiles>profile
下的文件 pom.xml
中定义。在那个地方,您可以指定包装选项。
这是这样的个人资料:
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<!-- log configuration -->
<logback.loglevel>DEBUG</logback.loglevel>
<!-- default Spring profiles -->
<spring.profiles.active>dev${profile.no-liquibase}</spring.profiles.active>
</properties>
</profile>
您必须将 spring.profiles.active
传递给运行时,而不是 "build runtime"。在构建时需要的唯一 spring 个配置文件将用于测试目的。
如果是"war on Tomcat",可以设置spring.profiles.active
:
(全局)在
<tomcat_home>/bin/
中创建一个名为setnev.sh
的文件(当您在 Windows 机器上时,分别为.bat
):export JAVA_OPTS="-Dspring.profiles.active=PROFILE_OF_THIS_TOMCAT"
分别是:
set JAVA_OPTS="-Dspring.profiles.active=PROFILE_OF_THIS_TOMCAT"
(全局)在
<tomcat_home>/conf/catalina.properties
中添加一行:spring.profiles.active=PROFILE_OF_THIS_TOMCAT
(在容器级别)在
$CATALINA_BASE/conf/[enginename]/[hostname]/
处添加一个名为context.xml
的文件(您也可以将其放入/<webapp_root>/META-INF/
,但是您将拥有以在构建时区分)具有以下内容:<Context> <Environment name="spring.profiles.active" value="PROFILE_OF_THIS_TOMCAT" /> </Context>