Search code examples
mavenmaven-war-plugin

Maven WAR overlay - merge resources into a different path


I'm using maven 4.0.0. I'm using an overlay in the maven-war-plugin to merge app1 into app2. I want to merge some of the directories in app1 into a different location in app2. Essentially move the content inside the SomeContent directory up one level. Is this even possible? Do I need to use the maven-resources-plugin somehow?

Here are the files in question in the war of app1:

SomeContent/admin/index.jsp
SomeContent/images/header.png

I want to end up with the files like this in app2's war after the overlay:

admin/index.jsp    <-- from app1
images/header.png  <-- from app1
app2.jsp           <-- from app2
WEB-INF/...        <-- from app2
...                <-- from app2

app1's pom:

<groupId>com.testing</groupId>
<artifactId>app1</artifactId>
<packaging>war</packaging>
...
<plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>3.2.3</version>
    <configuration>
        <warSourceDirectory>WebContent</warSourceDirectory>
    </configuration>
</plugin>

app2's pom:

<groupId>com.testing</groupId>
<artifactId>app2</artifactId>
<packaging>war</packaging>
...
<plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>3.2.3</version>
    <configuration>
        <warSourceDirectory>WebContent</warSourceDirectory>
        <overlays>
            <overlay>
                <groupId>com.testing</groupId>
                <artifactId>app1</artifactId>
            </overlay>
        </overlays>
    </configuration>
</plugin>

Solution

  • I figured it out. The overlay war is unpacked to the (default) target/war/work/<groupId>/<artifactId> directory. So I excluded in the overlay the directory I want at a different path, and make sure that overlay is executed before the current build. Then I added a resource to copy the directory in question into the build directory, which then gets added to the war at the path I specify.

    <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.2.3</version>
        <configuration>
            <overlays>
                <overlay>
                    <groupId>com.testing</groupId>
                    <artifactId>app1</artifactId>
                    <excludes>
                        <exclude>SomeContent/**</exclude>
                    </excludes>
                </overlay>
                <overlay><!-- empty groupId/artifactId represents the current build --></overlay>
            </overlays>
            <webResources>
                ...
                <resource>
                    <directory>target/war/work/com.testing/app1/SomeContent</directory>
                </resource>
            </webResources>
        </configuration>
    </plugin>