带有war文件的Spring应用程序属性配置文件 [英] Spring application properties profile with war file
问题描述
我正在尝试将我的项目打包在 .war
中以用于 Tomcat 服务器部署.我需要能够使用我的 application.properties
OR application-dev.properties
OR appliation-qa.properties
OR application-prod.properties
.使用嵌入式 servlet 运行项目我可以通过命令行指定我想使用哪个,但是,当我将它打包为 .war 时,该项目总是使用
application.properties
代码>.
我使用以下命令在本地运行我的项目:
mvn spring-boot:run
mvn spring-boot:run -Drun.arguments="--spring.profiles.active=dev"
这个命令通过竹子打包我的项目进行部署:
mvn package -Dspring.profiles.active=qa
应用程序.java
包 com.pandera.wilson;导入 org.springframework.boot.SpringApplication;导入 org.springframework.boot.autoconfigure.SpringBootApplication;导入 org.springframework.boot.builder.SpringApplicationBuilder;导入 org.springframework.boot.context.web.SpringBootServletInitializer;导入 org.springframework.context.annotation.ComponentScan;导入 org.springframework.core.env.AbstractEnvironment;导入 org.springframework.scheduling.annotation.EnableAsync;导入 org.springframework.scheduling.annotation.EnableScheduling;导入 javax.servlet.ServletContext;导入 javax.servlet.ServletException;导入 org.apache.log4j.Logger;/*** @author Gaurav Kataria* @author 奥斯汀尼古拉斯* @category 应用*/@SpringBootApplication@ComponentScan(basePackages = { "com.pandera.wilson" })@EnableAsync公共类应用程序扩展 SpringBootServletInitializer {静态最终记录器记录器 = Logger.getLogger(Application.class);public static void main(String[] args) 抛出异常 {logger.info("进入申请");SpringApplication.run(Application.class, args);}@覆盖受保护的 SpringApplicationBuilder 配置(SpringApplicationBuilder 应用程序){返回 application.sources(Application.class);}}
pom.xml
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.springframework</groupId><artifactId>wilson</artifactId><version>3.0.1</version><包装>战争</包装><属性><java.version>1.8</java.version><start-class>com.pandera.wilson.Application</start-class></属性><父母><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.3.6.RELEASE</version></父母><构建><finalName>wilson-services</finalName><插件><插件><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></插件></plugins></build><依赖项><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><排除事项><排除><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-logging</artifactId></排除><排除><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId></排除></排除项></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId><范围>提供</范围></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-log4j</artifactId></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><范围>测试</范围></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></依赖><依赖><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.5.2</version></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></依赖><依赖><groupId>org.springframework.security.oauth</groupId><artifactId>spring-security-oauth2</artifactId></依赖><依赖><groupId>com.microsoft.sqlserver</groupId><artifactId>sqljdbc4</artifactId><version>4.0</version></依赖><依赖><groupId>org.json</groupId><artifactId>json</artifactId><version>20160212</version></依赖><依赖><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.4</version></依赖><依赖><groupId>com.microsoft.azure</groupId><artifactId>天蓝色</artifactId><version>1.0.0-beta2</version></依赖></依赖项><存储库><存储库><id>spring-releases</id><url>https://repo.spring.io/libs-release</url></repository></repositories></项目>
编辑 7-21-16 下午 1:30
我已将以下内容添加到我的 pom.xml 并尝试使用 mvn package -P PROD
打包,但是,当我点击 /about
时,我仍然看到我正在使用 appliation.properties
而不是 application-prod.properties
.
<个人资料><id>QA</id><属性><spring.profiles.active>qa</spring.profiles.active></属性></个人资料><个人资料><id>DEV</id><属性><spring.profiles.active>dev</spring.profiles.active></属性></个人资料><个人资料><id>PROD</id><属性><spring.profiles.active>prod</spring.profiles.active></属性></个人资料></个人资料>
您可以通过以下方式定义弹簧配置文件:
<块引用>web.xml
<param-name>spring.profiles.active</param-name><param-value>你的目标配置文件在这里</param-value></context-param>
<块引用>
setenv.sh
在 Tomcat 的 bin 文件夹下创建 setenv.sh 文件,内容如下:
JAVA_OPTS="$JAVA_OPTS -Dspring.profiles.active=<您的目标配置文件>"
I am trying to package my project in a .war
for a tomcat server deployment. I need the ability to use my application.properties
OR application-dev.properties
OR appliation-qa.properties
OR application-prod.properties
. Running the project with the embeded servlet I am able to specify via the command line which one I want to use, but, the project always uses application.properties
when I package it as a .war
.
I use the following commands to run my project locally:
mvn spring-boot:run
mvn spring-boot:run -Drun.arguments="--spring.profiles.active=dev"
And this command to package my project though bamboo for deployment:
mvn package -Dspring.profiles.active=qa
Application.java
package com.pandera.wilson;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.env.AbstractEnvironment;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import org.apache.log4j.Logger;
/**
* @author Gaurav Kataria
* @author Austin Nicholas
* @category Application
*/
@SpringBootApplication
@ComponentScan(basePackages = { "com.pandera.wilson" })
@EnableAsync
public class Application extends SpringBootServletInitializer {
static final Logger logger = Logger.getLogger(Application.class);
public static void main(String[] args) throws Exception {
logger.info("Entering Application");
SpringApplication.run(Application.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework</groupId>
<artifactId>wilson</artifactId>
<version>3.0.1</version>
<packaging>war</packaging>
<properties>
<java.version>1.8</java.version>
<start-class>com.pandera.wilson.Application</start-class>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.6.RELEASE</version>
</parent>
<build>
<finalName>wilson-services</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>4.0</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20160212</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure</artifactId>
<version>1.0.0-beta2</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
</project>
EDIT 1:30PM 7-21-16
I've added the following to my pom.xml and tried packaging with mvn package -P PROD
, however, when I hit /about
I still see that I'm using appliation.properties
instead of application-prod.properties
.
<profiles>
<profile>
<id>QA</id>
<properties>
<spring.profiles.active>qa</spring.profiles.active>
</properties>
</profile>
<profile>
<id>DEV</id>
<properties>
<spring.profiles.active>dev</spring.profiles.active>
</properties>
</profile>
<profile>
<id>PROD</id>
<properties>
<spring.profiles.active>prod</spring.profiles.active>
</properties>
</profile>
</profiles>
You can define the spring profiles through the following:
web.xml
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>your target profile here</param-value>
</context-param>
setenv.sh
Under your Tomcat's bin folder create setenv.sh file with the following content:
JAVA_OPTS="$JAVA_OPTS -Dspring.profiles.active=<your target profile here>"
这篇关于带有war文件的Spring应用程序属性配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!