Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot profile based WAR using maven

I need to create WAR file based on specific environment properties file.

So I have created 2 properties files,

  • application.DEV.properties

  • application.PROD.properties

Now when I run the project using eclipse embedded tomcat, I pass on the -Dspring.profiles.active=DEV as VM argument. Then when I hit my endpoint, I can see the DEV related messages returned. Same is the case when I pass PROD as parameter.

Now, what I want to do is I want to create a WAR file with maven command and pass the parameter in such a way that my specific properties file gets loaded. So I have referred google as well as stackoverflow and found various options like below,

  1. mvn clean install -Drun.profiles=DEV
  2. mvn clean install -Drun.jvmArguments="-Dspring.profiles.active=DEV"
  3. mvn clean install -Dspring.profiles.active="DEV"
  4. mvn clean install -Dspring.profiles.active=DEV

I tried all above. When I hit the command, the WAR gets generated. but it doesn't get deployed on tomcat, because it cant read the properties file and gives error. It seems like the profile specific properties file does not get loaded in the WAR.

I want to know what is the alternative to -Dspring.profiles.active=DEV when I want to generate a WAR file using maven?

How to generate WAR to correctly include proper profile specific properties file?

I am using spring boot 1.5.14.RELEASE.

like image 340
WebNoob Avatar asked Dec 30 '25 01:12

WebNoob


2 Answers

As commented on this answer by Mickael, you can get help from the maven documentation on how to use profiles : https://maven.apache.org/guides/introduction/introduction-to-profiles.html

The usual way to choose a profile with maven is

mvn -Pprod package

Where prod is the name of your profile. If you want to build with the dev profile, it would be

mvn -Pdev package

Such profiles are defined in your file pom.xml under project>profiles>profile. And at that place, you can specify packaging options.

Here is such a profile:

<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>
like image 199
Adrien H Avatar answered Jan 01 '26 15:01

Adrien H


You have to pass spring.profiles.active to the runtime, not to the "build runtime". The only spring profiles, which are desirable at build time, would be for testing purposes.

In case of "war on Tomcat", you can set spring.profiles.active:

  1. (Globally) Create a file in <tomcat_home>/bin/ named setnev.sh (respectively .bat, when you are on a Windows machine) with:

    export JAVA_OPTS="-Dspring.profiles.active=PROFILE_OF_THIS_TOMCAT"
    

    respectively:

    set JAVA_OPTS="-Dspring.profiles.active=PROFILE_OF_THIS_TOMCAT"
    
  2. (Globally) Add a line to <tomcat_home>/conf/catalina.properties:

    spring.profiles.active=PROFILE_OF_THIS_TOMCAT
    
  3. (At Container level) Add a file named context.xml at $CATALINA_BASE/conf/[enginename]/[hostname]/ (, you could also put it into /<webapp_root>/META-INF/, but then you'd have to distinguish at build time) with the following content:

    <Context>
     <Environment name="spring.profiles.active" value="PROFILE_OF_THIS_TOMCAT" /> 
    </Context>
    
like image 35
xerx593 Avatar answered Jan 01 '26 16:01

xerx593



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!