java 带有war文件的Spring应用程序属性配置文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38509511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring application properties profile with war file
提问by adenix
I am trying to package my project in a .war
for a tomcat server deployment. I need the ability to use my application.properties
ORapplication-dev.properties
ORappliation-qa.properties
ORapplication-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
.
我正在尝试将我的项目打包.war
到 Tomcat 服务器部署中。我需要能够使用我的application.properties
OR application-dev.properties
OR appliation-qa.properties
ORapplication-prod.properties
。使用嵌入式 servlet 运行项目我可以通过命令行指定我想使用哪个,但是,application.properties
当我将它打包为.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"
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
mvn package -Dspring.profiles.active=qa
Application.java
应用程序.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
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
编辑 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
.
我已经添加了以下我的pom.xml,并试图用包装mvn package -P PROD
然而,当我打/about
我还看到我使用appliation.properties
的替代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>
回答by Rae Burawes
You can define the spring profiles through the following:
您可以通过以下方式定义弹簧轮廓:
web.xml
网页.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:
在 Tomcat 的 bin 文件夹下创建 setenv.sh 文件,内容如下:
JAVA_OPTS="$JAVA_OPTS -Dspring.profiles.active=<your target profile here>"
回答by ihebiheb
To package the project according to a pre defined profile, you need to do this :
要根据预定义的配置文件打包项目,您需要执行以下操作:
In your
application.properties
, addIn your POM, add this :
在您的
application.properties
, 添加在您的 POM 中,添加以下内容:
<build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources>
<build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources>
and define your profiles (just like what you did)
run the command
mvn package -P QA
并定义您的个人资料(就像您所做的一样)
运行命令
mvn package -P QA
You will see that in the generated WAR, in the application.properties
, [email protected]@
will be replaced with spring.profiles.active=QA
您会看到在生成的 WAR 中,application.properties
,[email protected]@
将被替换为spring.profiles.active=QA
回答by DeC
You can Use
您可以使用
spring.profiles.active=<Your Active application(dev/test/prod)>
only in your application.properties file
仅在您的 application.properties 文件中
other configuration you have to do in your application-dev.properties
, appliation-qa.properties
And application-prod.properties
files
您必须在application-dev.properties
,appliation-qa.properties
和application-prod.properties
文件中进行的其他配置
That's Works for me
这对我有用
回答by krmanish007
Have you tried this?
你试过这个吗?
java -jar -Dspring.profiles.active=qa yourwarfilename.war
For packaging, you can create profiles like this and call this profile while building it via $mvn package -P QA:
对于打包,您可以创建这样的配置文件,并在通过 $mvn package -P QA 构建它时调用此配置文件:
<profiles>
<profile>
<id>QA</id>
<properties>
<spring.profiles.active>qa</spring.profiles.active>
</properties>
</profile>
</profiles>
回答by Nani
i hope this solve your issue. (first i'm going to ask for appology because my inglish it's very poor, but I want to help you)
我希望这能解决您的问题。(首先我会要求道歉,因为我的英语很差,但我想帮助你)
Now, my comment :
现在,我的评论:
I'm coding with maven and spring-boot.
我正在使用 maven 和 spring-boot 进行编码。
you must have all yours profiles with their respective properties in the pom.xml ( in my case I have loggers per profile, you will add the properties you need ) :
您必须在 pom.xml 中拥有所有配置文件及其各自的属性(在我的情况下,每个配置文件都有记录器,您将添加所需的属性):
<profile>
<id>test</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<FILE_LOG_LEVEL>INFO</FILE_LOG_LEVEL>
</properties>
</profile>
then you can select the profile to tell spring with wich one work on the file "aplication.properties":
然后你可以选择配置文件来告诉 spring 对文件“aplication.properties”的一项工作:
spring.profiles.active="nameOfProfileYouWantToActive"
spring.profiles.active="nameOfProfileYouWantToActive"
With this you just need to run : mvn clean package and you will obtein your deploy.
有了这个,你只需要运行:mvn clean package,你就会得到你的部署。