Craig Otis
Craig Otis

Reputation: 32054

Using Maven Profiles with a WAR-Packaged Product

We have a Maven build (version 2.2.1) that currently produces a WAR file. Our output directory is target/, so we end up with a build artifact target/MyWar.WAR.

I'm adding two profiles to our POM.xml file to facilitate specific build "flavors" that each require a specific version of an A.xml file. In the intermediate build directory target/MyWar/ there are 3 files:

A.xml
A_1.xml
A_2.xml

Building in Maven without a specified profile should use A.xml, and it does currently. I want to use maven-antrun-plugin to (for Profile 1) replace A.xml with A_1.xml, and for Profile 2 replace A.xml with A_2.xml. (Removing the _1 and _2 suffixes.)

This is an example of Profile 1's execution:

<profile>
<id>Profile1</id>
<build>
...
    <execution>
        <id>Profile1-Replace</id>
        <phase>package</phase>
        <goals>
            <goal>run</goal>
        </goals>
        <configuration>
            <tasks>
                <delete
                    file="${project.build.outputDirectory}/../MyWar/A.xml" />
                <copy
                    file="${project.build.outputDirectory}/../MyWar/A_1.xml"
                    tofile="${project.build.outputDirectory}/../MyWar/A.xml" />
            </tasks>
        </configuration>
    </execution>
...
</build>
</profile>

This correctly replaces the files in the intermediate target/MyWar/ directory, but for whatever reason the final WAR that's being produced in target does not reflect these changes.

It's as if running in the 'package' phase is too late, and the WAR has already been built. Changing the phase to 'compile' or 'test', the immediately-previous phases, complain because the A.xml file (and the intermediate build directory) have not even been created yet.

Upvotes: 0

Views: 847

Answers (1)

Zac Thompson
Zac Thompson

Reputation: 12675

I would suggest to use the process-resources phase instead, or even generate-resources, if you feel that is a better fit. As a last resort, use prepare-package. But the package phase is the wrong place to do this sort of thing. All such modifications might typically occur directly in the source tree.

However, if you do the file manipulation in a separate directory, then you can add it during the package phase using the maven-war-plugin as follows:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <configuration>
      <webResources>
        <resource>
          <directory>A_variant</directory>
        </resource>
      </webResources>
    </configuration>
  </plugin>

Of course if you need to go this route, it would be simpler to keep three directories and choose the appropriate one in your profile.

Upvotes: 2

Related Questions