12

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:

  1. mvn spring-boot:run
  2. 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>
JimHawkins
  • 4,843
  • 8
  • 35
  • 55
adenix
  • 420
  • 1
  • 4
  • 10
  • did you find the solution for this? – Dilanka Rathnayake Feb 08 '17 at 12:49
  • Updating my spring boot version fixed this for me. They changed the way you configure the application for external servlet deployments and it fixed this issue. – adenix Aug 25 '17 at 22:20
  • It seems the problem is back - tried on Spring Boot 2.18 and 2.1.9. The only option I found is to set up a profile again during the external servlet deployment using `-Dspring.profile.actives` property – Krzysztof Skrzynecki Oct 17 '19 at 07:47
  • Do any one have idea about changing `.war` name dynamically according to environment like `app-dev.war` or `app-test.war` or `app-prod.war` – DeC Oct 17 '19 at 10:47

6 Answers6

11

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>"
Rae Burawes
  • 892
  • 7
  • 18
6

To package the project according to a pre defined profile, you need to do this :

  1. In your application.properties, add

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

  2. In your POM, add this :

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
  1. and define your profiles (just like what you did)

  2. run the command mvn package -P QA

You will see that in the generated WAR, in the application.properties, spring.profiles.active=@spring.profiles.active@ will be replaced with spring.profiles.active=QA

ihebiheb
  • 3,673
  • 3
  • 46
  • 55
1

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:

<profiles>
  <profile>
    <id>QA</id>
    <properties>
      <spring.profiles.active>qa</spring.profiles.active>
    </properties>
  </profile>
</profiles>
krmanish007
  • 6,749
  • 16
  • 58
  • 100
1

Only use this when you want to deploy.

java -jar -Dspring.profiles.active=<active profile name> <your war file name>.war
saeed eivazi
  • 818
  • 1
  • 7
  • 14
0

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.

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 ) :

<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.profiles.active="nameOfProfileYouWantToActive"

With this you just need to run : mvn clean package and you will obtein your deploy.

Nani
  • 1
0

You can Use

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

only in your application.properties file

other configuration you have to do in your application-dev.properties , appliation-qa.properties And application-prod.properties files

That's Works for me

DeC
  • 2,226
  • 22
  • 42