Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I avoid build failing when Maven war plugin can't find webResource directory?

I am working with a module that produces a WAR artifact through Maven. The module has a typical src/main/webapp directory. I'm using Maven's war plugin to supplement the webapp directory with another resource directory that can be programmatically provided at build time. So my plugin is configured in the POM like this:

<plugin>
    <artifactId>maven-war-plugin</artifactId>
    <configuration>
        <webResources>
            <resource>
                <directory>src/main/webapp.${flavor}</directory>
            </resource>
        </webResources>
    </configuration>
</plugin>

Where ${flavor} defaults to a value in the root POM, or can optionally be overridden at build time with something like this:

mvn clean install -Dflavor=customer

The problem is the war plugin dies a fiery death during the build if it cannot find the directory provided for the resource (i.e. src/main/webapp.customer doesn't exist). My project can have many flavors, and I'd rather not have to create an empty directory for each possible flavor, just to appease the plugin.

Question(s): Is there a way to configure the war plugin to not fail the build when the directory does not exist? And if not, what is the best way to create this directory at build time when it does not already exist?

Thank you.

UPDATE

After additional thought and research, I decided to pursue a conditional creation of the "flavored" directory at build time, using the maven-antrun-plugin. After all, if the build needs the directory to be there - and will die without it - then the build should ensure the directory is there. So here's my antrun plugin execution:

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
        <execution>
            <id>created-flavored-resources-directory</id>
            <phase>process-resources</phase>
            <configuration>
                <target>
                    <mkdir dir="src/main/webapp.${flavor}"/>
                </target>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>

The execution will create the directory only when the directory does not already exist. This allows us to have a directory in place... or not. Everything just works.

like image 308
MegaMatt Avatar asked Jan 18 '18 18:01

MegaMatt


1 Answers

I had similar problem (actually found your question while googling for solution).

My observation was same as yours: directory must exist. However, it does not apply to <include>s. So I hope this would pass even if webapp.${flavor} subdir didn't exist:

<resource>
    <directory>src/main</directory>
    <includes>
        <include>webapp.${flavor}</include>
    </includes>
</resource>

The disadvantage is you cannot then use <targetPath> to rename dir.

My background: I had optional dir with built documentation which had to be bundled into war. The combination directory=${project.basedir}/../docs-build + include=** didn't work when docs-build directory didn't exist. In contrast, directory=${project.basedir}/.. + include=docs-build worked. (Finally I dropped this approach since I needed to rename dir but that's a different story.)

like image 53
Tomáš Záluský Avatar answered Sep 30 '22 17:09

Tomáš Záluský