spoulson
spoulson

Reputation: 21591

Maven building for different environments

I have a Java project that currently builds nicely with Maven. But, I'm using an Ant script to do other tasks that I think Maven could also do. To simplify the build workflow, I'd like to get Maven doing everything I need. All this is automated with Jenkins.

The challenge is that I want Maven to build a single artifact and deploy it to multiple environments on-demand via command line. Currently, I do this by building with Maven, then using an Ant script that will create a copy of the WAR artifact with some files changed depending on the environment I chose. This adapts for things like JDBC connection strings, properties files, etc. that are different in each environment.

I understand Maven can use profiles, as explained in this answer. However, I'm stuck on figuring out how to make a secondary WAR file for the environment. e.g. I start with "myproject.war" created from the build step, and I want to create "myproject-dev.war", then deploy it to the dev Tomcat server. I figure I can use the antrun maven plugin to make the file changes, but I don't know how to hook it into the lifecycle. Then, I'd need to use the tomcat plugin to deploy.

Any suggestions on how this could be configured?

Upvotes: 0

Views: 3530

Answers (1)

khmarbaise
khmarbaise

Reputation: 97399

Let us assume you have a structure like the following (just as an example)

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

You can define a assembly descriptor like the following:

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

Furthermore you need a maven-assembly-plugin configuration like the following:

<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>
  </executions>
</project>

If you add a separate assembly-descriptor for each environment and add a line with the descriptor you can create with a single call the packages for each environment. One drawback of this you have several assembly-descriptors which are aside from a few lines identical.

So going more you can use the iterator-maven-plugin to reduce the configuration and mainenance hassle like this:

<plugin>
  <groupId>com.soebes.maven.plugins</groupId>
  <artifactId>iterator-maven-plugin</artifactId>
  <version>0.3</version>
  <executions>
    <execution>
      <phase>package</phase>
      <goals>
        <goal>iterator</goal>
      </goals>
      <configuration>
        <items>
          <item>test</item>
          <item>prod</item>
          <item>dev</item>
        </items>

        <pluginExecutors>
          <pluginExecutor>
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-assembly-plugin</artifactId>
              <version>2.4</version>
            </plugin>
            <goal>single</goal>
            <configuration>
              <descriptors>
                <descriptor>src/assembly/iterator.xml</descriptor>
              </descriptors>
            </configuration>
          </pluginExecutor>
        </pluginExecutors>
      </configuration>
    </execution>
  </executions>
</plugin>

So furthermore you need an appropriate assembly descriptor like this:

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

  <id>${item}</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/${item}/</directory>
      <includes>
        <include>**</include>
      </includes>
    </fileSet>
    <fileSet>
      <outputDirectory>WEB-INF</outputDirectory>
      <directory>${project.build.directory}/environment/${item}/</directory>
      <includes>
        <include>**</include>
      </includes>
    </fileSet>
  </fileSets>
</assembly>

This makes it simple and convenient to maintain such a build. A full working example can looked at the integration tests of iterator-maven-plugin.

Upvotes: 1

Related Questions