带有war文件的Spring应用程序属性配置文件



我正试图将我的项目打包在.war中,用于tomcat服务器部署。我需要能够使用我的application.propertiesapplication-dev.propertiesORappliation-qa.propertiesORapplication-prod.properties。使用嵌入式servlet运行项目,我可以通过命令行指定要使用哪个servlet,但是,当我将其打包为.war时,项目总是使用application.properties

我使用以下命令在本地运行我的项目:

  1. mvn spring-boot:run
  2. mvn spring-boot:run -Drun.arguments="--spring.profiles.active=dev"

这个命令将我的项目打包成竹状进行部署:

  • 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>


编辑下午1:30PM 7-21-16

我已经在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>

您可以通过以下方式定义弹簧轮廓:

web.xml

<context-param>
 <param-name>spring.profiles.active</param-name>
 <param-value>your target profile here</param-value>
</context-param>

setenv.sh

在Tomcat的bin文件夹下创建setenv.sh文件,其中包含以下内容:

JAVA_OPTS="$JAVA_OPTS -Dspring.profiles.active=<your target profile here>"

要根据预先定义的概要文件打包项目,需要执行以下操作:

  1. 在您的application.properties中,添加

    spring.profiles.active=@spring.profiles.active@

  2. 在您的POM中,添加以下内容:

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
  1. 并定义您的配置文件(就像您所做的那样)

  2. 运行命令mvn package -P QA

您将看到,在生成的WAR中,在application.properties中,spring.profiles.active=@spring.profiles.active@将替换为spring.profiles.active=QA

你试过这个吗?

java -jar -Dspring.profiles.active=qa yourwarfilename.war

对于打包,您可以创建这样的配置文件,并在通过$mvn包构建该配置文件时调用该配置文件-PQA:

<profiles>
  <profile>
    <id>QA</id>
    <properties>
      <spring.profiles.active>qa</spring.profiles.active>
    </properties>
  </profile>
</profiles>

仅在需要部署时使用此选项。

java -jar -Dspring.profiles.active=<active profile name> <your war file name>.war

我希望这能解决你的问题。(首先,我想问一下,因为我的英语很差,但我想帮助你)

现在,我的评论:

我使用maven和spring-boot进行编码。

您必须在pom.xml中拥有所有具有各自属性的配置文件(在我的情况下,每个配置文件都有记录器,您将添加所需的属性):

<profile>
<id>test</id>
    <activation>
        <activeByDefault>false</activeByDefault>
    </activation>
    <properties>
       <FILE_LOG_LEVEL>INFO</FILE_LOG_LEVEL>
    </properties>
</profile>

然后你可以选择一个文件"application.properties"上的工作来告诉spring的配置文件:

spring.profiles.active="nameOfProfileYouWantToActive"

有了这个,你只需要运行:mvn-clean包,你就会得到你的部署。

您可以使用

spring.profiles.active=<Your Active application(dev/test/prod)>

仅在application.properties文件中

您必须在application-dev.propertiesappliation-qa.propertiesapplication-prod.properties文件中进行的其他配置

这对我很有用

最新更新