maven-jar-plugin包含vs排除

时间:2023-01-14 08:58:23

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:

我有一个现有的pom文件,其中包含一个maven-jar-plugin部分。它运行测试jar目标,目前不包括几个目录:

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

我需要在somedir目录中包含一个文件,但遗漏somedir目录中的其余文件。我已经读过,包括优先于排除,所以我添加了类似下面的内容(之前没有包含部分):

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

这最终会创建一个jar文件进行测试,其中只有几个文件(只是META-INF中的东西)。我包含的文件也不在jar中。我期望的是一个jar,它与我在包含一个额外文件的更改之前创建的jar相同。

What am I missing here?

我在这里想念的是什么?

1 个解决方案

#1


11  

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.

首先,如果你没有指定任何包含,那么maven jar插件将使用** / **作为默认模式(参见o.a.m.p.j.AbstractJarMojo),即它将包含所有内容。如果您覆盖此默认模式,插件显然只会包含您告诉他包含的内容。

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:

其次,目录扫描最后由o.c.p.u.DirectoryScanner完成,这就是该类的javadoc所说的内容:

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

因此,使用当前包含和排除模式的集合,只有一个文件将匹配包含模式,但也匹配排除模式,因此不会被选中,您将得到一个几乎为空的存档(只有默认清单,请参阅oampjAbstractJarMojo# 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).

我不能在这里给你确切的解决方案,但你显然需要重新考虑包含/排除文件的方式(例如添加更多包含模式,删除 somedir / ** ,或仅使用包含,或使用仅排除)。

#1


11  

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.

首先,如果你没有指定任何包含,那么maven jar插件将使用** / **作为默认模式(参见o.a.m.p.j.AbstractJarMojo),即它将包含所有内容。如果您覆盖此默认模式,插件显然只会包含您告诉他包含的内容。

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:

其次,目录扫描最后由o.c.p.u.DirectoryScanner完成,这就是该类的javadoc所说的内容:

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

因此,使用当前包含和排除模式的集合,只有一个文件将匹配包含模式,但也匹配排除模式,因此不会被选中,您将得到一个几乎为空的存档(只有默认清单,请参阅oampjAbstractJarMojo# 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).

我不能在这里给你确切的解决方案,但你显然需要重新考虑包含/排除文件的方式(例如添加更多包含模式,删除 somedir / ** ,或仅使用包含,或使用仅排除)。