mavenmaven-3maven-jar-plugin

Why is the Maven JAR plugin not including some resources?


I have an enterprise application which I am in the process of converting from an Ant build to Maven. It's almost completely converted; this is the very last thing I need to fix. The application is packaged as an EAR file which contains two WARs and has a JAR module which provides all of the core functionality of the application.

I'm using the Freemarker templating library to generate, among other things, message bodies for automatic emails sent by the application. Freemarker needs its *.ftl template files to be on the classpath, and since this is core application functionality not specific to one WAR or the other, it needs to be in the JAR.

The Maven module which defines the JAR has the following POM:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <relativePath>../../pom.xml</relativePath>
        <groupId>com.company.project</groupId>
        <artifactId>projectName</artifactId>
        <version>1.8.0</version>
    </parent>

    <artifactId>core</artifactId>
    <packaging>jar</packaging>
    <name>Core Application</name>

    <profiles>
       <!-- snip -->
    </profiles>

    <dependencies>
       <!-- snip -->
    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/*.ftl</include>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <!-- snip -->
        </plugins>
    </build>
</project>

The *.ftl files are located at src/main/resources/template/, with some in a subdirectory within template/. There are other files within src/main/resources -- some .properties and some .xml, some at the root and some under a directory structure.

When I run the package phase on this module (or on the parent), the target/classes directory created as part of the build process contains the template directory, which in turn contains all of the *.ftl, *.xml, and *.properties files with an appropriate directory structure. If I JAR this directory up manually, everything works perfectly.

Here's where this gets weird and I get lost: when maven-jar-plugin creates the JAR, it includes the XML and properties files, but the template directory is completely absent from the JAR and its contents are nowhere to be found.

As you can see above, I tried explicitly including **/*.ftl. It doesn't make a difference; I can exclude the entire "includes" tag and I get the exact same behavior.

I'm using Maven 3.0.5 and maven-jar-plugin 2.4.


Solution

  • I figured out the answer RIGHT after submitting this question.

    In a parent POM, I had the following:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.4</version>
        <configuration>
            <includes>
                <include>**/*.class</include>
                <include>**/*.jdo</include>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
        </configuration>
    </plugin>
    

    I added **/*.ftl to the list of includes and now it's working.

    EDIT: Better yet, I removed the configuration tag entirely, and it's still working. I think it was a remnant from before I figured out that the .properties files and other things I needed on the classpath needed to be in src/main/resources and not src/main/java.