tags:

views:

989

answers:

1

I've got an existing pom file that includes a maven-jar-plugin section. It runs for the test-jar goal and is currently excluding a few directories:

<excludes>
     <exclude>...</exclude>
     <exclude>...</exclude>
     <exclude>somedir/**</exclude>
 </excludes>

I need to include a file in the somedir directory but leave out the rest of the files in the somedir directory. I've read that includes have precedence over excludes so I added something like the following (there was no includes section before):

<includes>
    <include>somedir/somefile.xml</include>
</includes>

This ends up creating a jar file for test with only a few files in it (just the stuff in META-INF). The file that I included is not in the jar either. What I'd expect is a jar that is identical to the jar that was created before my includes change with the one additional file.

What am I missing here?

+3  A: 

First, if you don't specify any includes, then the maven jar plugin will use **/** as default pattern (see o.a.m.p.j.AbstractJarMojo) i.e. it will include everything. If you override this default pattern, the plugin will obviously only include what you tell him to include.

Second, the directory scanning is done at the end by the o.c.p.u.DirectoryScanner and this is what the javadoc of the class says:

 * The idea is simple. A given directory is recursively scanned for all files
 * and directories. Each file/directory is matched against a set of selectors,
 * including special support for matching against filenames with include and
 * and exclude patterns. Only files/directories which match at least one
 * pattern of the include pattern list or other file selector, and don't match
 * any pattern of the exclude pattern list or fail to match against a required
 * selector will be placed in the list of files/directories found.

So, with your current set of includes and excludes patterns, only ONE file will match the inclusion pattern but also match an exclusion pattern and will thus not be selected and you get an almost empty archive (with just the default manifest, see o.a.m.p.j.AbstractJarMojo#createArchive()).

I can't give you the exact solution here but you clearly need to rethink the way you include/exclude files (e.g. add more includes patterns, remove <exclude>somedir/**</exclude>, or use includes only, or use excludes only).

Pascal Thivent
This makes sense based on what I was seeing (includes not overriding excludes). I ended up defining a testResource that was included in the resulting jar file:` <testResources>`` <testResource>`` <filtering>true</filtering>`` <directory>somedir/somefile.xml</directory>`` </testResource>`` </testResources>`
Chris Williams