Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

maven-jar-plugin includes vs excludes

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?

like image 482
Chris Williams Avatar asked Mar 15 '10 18:03

Chris Williams


People also ask

How do you exclude a jar file while building a application?

How to include/exclude content from jar artifact. Specify a list of fileset patterns to be included or excluded by adding <includes> / <include> or <excludes> / <exclude> in your pom. xml .

Does Maven include test classes in jar?

You can produce a jar which will include your test classes and resources. To reuse this artifact in an other project, you must declare this dependency with type test-jar : <project>


1 Answers

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).

like image 74
Pascal Thivent Avatar answered Sep 24 '22 18:09

Pascal Thivent