3

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.
Sreenivas M
  • 157
  • 3
  • 13
  • I don't quite understand... are you asking hot to activate a specific maven profile using cmd arguments (just like you do for spring profiles with -Dspring.profiles.active=prod) ? – Martín Zaragoza Aug 17 '18 at 13:03
  • 2
    The whole purpose of Spring profiles is to have single war / jar file for all environments. I think it will defeat the purpose if you are creating different war / jar files for each environment. Can you elaborate why do you need different war / jar per environment ? – Kedar Joshi Aug 17 '18 at 13:05
  • actually i need to dynamically pickup the environment specific file which doing maven install. I don't want diff war files. The required profile should not be configured in pom.xml. I need to avoid making changes in pom.xml for diff env. – Sreenivas M Aug 17 '18 at 14:32
  • I don't need multiple war files. Here the requirement is once the code is ready for deployment, i need load the configuration using command line arguments . Or any other way to avoid alerting the configuration in property file. – Sreenivas M Aug 24 '18 at 10:46

2 Answers2

7

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

  • Yes this configuration works. My requirement is diff.I should not touch the pom file again to set to diff profile. I want to dynamically pass the profile while building war file using maven. Like while running a spring boot app, I am specifying the profile type dynamically as part of VM args. – Sreenivas M Aug 17 '18 at 14:35
  • 1
    Oh yes. You won't need to touch the pom.xml anymore :-). I updated my answer to show how you can pre-define all the profiles once in the pom.xml. Then you can execute your maven goal like this `mvn install -DmyActiveProfile=dev` or `mvn install -DmyActiveProfile=prod` – Bankole Hussein Salako Aug 17 '18 at 16:42
  • 1
    You don't need to add any listeners AFAIK – Bankole Hussein Salako Aug 20 '18 at 15:09
  • My application is already being developed. Need to do minimal Java Coding. – Sreenivas M Aug 21 '18 at 15:58
0

Here, first of all I would suggest to rename your properties files to application-prod.properties, application-dev.properties and application-test.properties.

Second, maven install goal is to compile and build your project.

If you also want to run your application, when you do install I suggest to use spring-boot-maven-plugin.

And you can use a maven command something like below

mvn clean install spring-boot:run

Some links below for you information

https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-running-your-application.html

https://docs.spring.io/spring-boot/docs/current/maven-plugin/run-mojo.html

Anil Bachola
  • 289
  • 2
  • 5