Java – Spring Boot Profile-specific Properties build using Maven Configuration

javamavenspring-bootspring-profileswar

How to build the environment specific war file for a spring boot application using maven.I have created 3 profile configuration files placed in src/main/resource/ folder.

  1. application.prod.properties
  2. application.dev.properties
  3. application.test.properties

I am able to run application by specifying required profile type in the VM argument tab with the value "-Dspring.profiles.active=dev" while executing the project as spring boot application.

Here while running as spring boot application i am able to the specify the required profile. In the same way when I need to use for MAVEN install with different profile. Is it there any way to specify profile as part of VM argument list in Run Configuration for Maven Install goal.

I have limitation as not to touch the existing java code.

I am using STS IDE, Spring boot 1.5.2.RELEASE version, Java 1.8 and oracle db.

In the same way also help me in how to configure profiles in Jenkins.

My profile configuration has two requirements.

  1. Run the application in STS IDE as spring boot app with the VM args.
    Used the below VM ARGS
    -Dspring.profiles.active=dev

Blockquote

(Here I am getting below exception while starting SpringBootApp locally in Dev Environment).


APPLICATION FAILED TO START


Description:

Cannot determine embedded database driver class for database type NONE

Action:

If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (the profiles "dev" are currently active).

Blockquote

  1. How to do the same thing using maven install by specifying profiles dynamically to generate war file.I am unable to find the solution.

Best Answer

In your main application.properties file, set spring.profiles.active to @myActiveProfile@ (or any name you wish)

spring.profiles.active=@myActiveProfile@

Add your application.properties file as a filtered resource so that the placeholder myActiveProfile is replaced during build phase.

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
    …
</build>

Add a profiles section to your pom.xml

<profiles>
    <profile>
        <id>dev</id>
        <properties>
            <myActiveProfile>dev</myActiveProfile>
        </properties>
    </profile>
    <profile>
        <id>prod</id>
        <properties>
            <myActiveProfile>prod</myActiveProfile>
        </properties>
    </profile>
</profiles>

Basically, you are able to specify maven profiles when executing a particular goal. E.g mvn install -P profileName. Within a profile, you can create a property which can be passed from the command line when running a goal like mvn install -DmyActiveProfile=foo

Hope this helps.

Helpful Links

How to set spring active profiles with maven profiles https://maven.apache.org/guides/introduction/introduction-to-profiles.html