1

Taken by example the jhipster-sample-app how can I manage multiple profiles, given that the same application will be installed in several machines having different configurations?

Since the deployment will be made using the apache tomcat, and it could be running one or more jhipster based apps, I would like to avoid using

-Dspring.profiles.active=MY_PROFILE

in the JAVA_OPTS variable.

Also it could happen to run the same application with different profiles on the same tomcat instance.

1 Answer 1

4

Taking profiles for the configuration of an application to support different environments like dev, test, prod is a bad idea. Let us assume you have defined to make it simple three profiles like dev,test,prod. So now you build for dev environment like this:

mvn -Pdev clean package

Ok now you can take your artifact and deploy it. Next time you need for test environment you have to go like this:

mvn -Ptest clean package

You can take your artifact and deploy it. But what happens if you like to create for two environments or for three?

mvn -Pdev,test,prod clean package

This will usually fail, cause it's really tricky (and in same areas impossible) to handle different profiles to produce three different artifacts. So the best practice is to remove profiles and let your build produce just by:

mvn clean package

all the packages you need one for dev, one for test and one for prod.

One solution is to create a project structure like this:

.
|-- pom.xml
`-- src
    |-- main
    |   |-- java
    |   |-- resources
    |   |-- environment
    |   |   |-- test
    |   |   |   `-- database.properties
    |   |   |-- qa
    |   |   |   `-- database.properties
    |   |   `-- production
    |   |       `-- database.properties
    |   `-- webapp

The different folders and property files are place holders just to show the path. Next you need an assembly-descriptor one for each environment like this:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">

  <id>test</id>
  <formats>
    <format>war</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <dependencySets>
    <dependencySet>
      <unpack>true</unpack>
      <useProjectArtifact>true</useProjectArtifact>
    </dependencySet>
  </dependencySets>
  <fileSets>
    <fileSet>
      <outputDirectory>WEB-INF</outputDirectory>
      <directory>${basedir}/src/main/environment/test/</directory>
      <includes>
        <include>**</include>
      </includes>
    </fileSet>
  </fileSets>
</assembly>

And finally you need to configure the maven-assembly-plugin like this:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-assembly-plugin</artifactId>
  <executions>
    <execution>
      <id>test</id>
      <phase>package</phase>
      <goals>
        <goal>single</goal>
      </goals>
      <configuration>
        <descriptors>
          <descriptor>${project.basedir}/src/main/assembly/test.xml</descriptor>
        </descriptors>
      </configuration>
    </execution>
    <execution>
      <id>qa</id>
      <phase>package</phase>
      <goals>
        <goal>single</goal>
      </goals>
      <configuration>
        <descriptors>
          <descriptor>${project.basedir}/src/main/assembly/qa.xml</descriptor>
        </descriptors>
      </configuration>
    </execution>
    <execution>
      <id>production</id>
      <phase>package</phase>
      <goals>
        <goal>single</goal>
      </goals>
      <configuration>
        <descriptors>
          <descriptor>${project.basedir}/src/main/assembly/production.xml</descriptor>
        </descriptors>
      </configuration>
    </execution>
  </executions>
</plugin>

In the end this will produce the following artifacts: artifact-VERSION-dev.war, artifact-VERSION-test.war and artifact-VERSION-prod.war with a single call of Maven. If you take a deeper look into that blog article the above can made much more elegant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.