Search code examples
javamavenspring-bootdeploymentwar

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.


Solution

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