如何在Maven程序集中排除资源文件? [英] How to exclude a resource file in a Maven assembly?
问题描述
我正在尝试从Maven构建中排除资源文件.我已经复制了my-jar-with-dependencies.xml并添加了一个部分.这样可以正确地将命名文件排除在复制到jar的/src/main/resources之外,但 仍然将文件与其他资源一起复制到jar的顶级目录中.
I'm trying to exclude a resource file from my Maven build. I've copied the my-jar-with-dependencies.xml and added a section. This correctly excludes the named file from being copied to the jar's /src/main/resources but the file still gets copied to the jar's top level directory along with the other resources.
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
<!-- TODO: a jarjar format would be better -->
<id>my-jar-with-dependencies</id>
<formats>
<format>jar</format>
</formats>
<fileSets>
<fileSet>
<directory>${project.basedir}</directory>
<outputDirectory></outputDirectory>
<useDefaultExcludes>true</useDefaultExcludes>
<excludes>
<exclude>**/lanchecker.properties</exclude> <!-- this removes it from jar/src/main/resources but *NOT* from jar/ -->
</excludes>
</fileSet>
</fileSets>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
</assembly>
我可以阻止maven-assembly-plugin复制该文件,但仍使该文件在开发中可访问吗?
Can I prevent the maven-assembly-plugin from doing that copy but still leave that file accessible in development?
更新:
依赖关系,根据要求:
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
<version>1.4.01</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.7</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>2.47.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.6.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.6.3</version>
</dependency>
</dependencies>
推荐答案
尝试一下:
...
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>runtime</scope>
<unpackOptions>
<excludes>
<exclude>**/lanchecker.properties</exclude>
</excludes>
</unpackOptions>
</dependencySet>
</dependencySets>
...
之所以可行,是因为不同的程序集选择器(<dependencySets>
,<moduleSets>
,<fileSets>
和<files>
)针对不同的对象,并且它们可以重叠.
The reason this works is that the different assembly selectors (<dependencySets>
, <moduleSets>
, <fileSets>
and <files>
target different things and they can overlap.
例如,<fileSets>
仅针对 this 模块(程序集所绑定的模块)中的文件.由于您指定了<useProjectArtifact>true</useProjectArtifact>
,因此依赖项集将包含 this 模块作为依赖项,因此这就是lanchecker.properties
进入程序集的原因,这就是为什么您必须将其从依赖项集中排除的原因也一样
For instance, <fileSets>
only targets files in this module (the one the assembly is tied to). Since you specified <useProjectArtifact>true</useProjectArtifact>
, the dependency set will include this module as a dependency, so that's why lanchecker.properties
found its way into your assembly, and that's why you have to exclude it from the dependency set as well.
在不知道您的POM是什么样子或您试图达到什么目的的情况下,指定<useProjectArtifact>false</useProjectArtifact>
并仅使用<fileSets>
来控制 this 模块的内容可能就足够了.
Without knowing what your POM looks like or what you are trying to achieve, it might be sufficient to specify <useProjectArtifact>false</useProjectArtifact>
and just use <fileSets>
to control the contents of this module.
另一方面,由于此模块已经包含在依赖项集中(通过<useProjectArtifact>true</useProjectArtifact>
的方式,您可能会完全删除<fileSets>
部分而逃脱了.
On the other hand, since this module is already included in the dependency set (by way of <useProjectArtifact>true</useProjectArtifact>
, you might get away with just removing the <fileSets>
section altogether.
组装通常是Maven用户学习掌握的最后一件事.它功能强大,灵活且复杂,尤其是当您将多个模块混合使用时,但它也可能有些古怪.
Assemblies are usually the last thing a Maven user learns to master. It's powerful, flexible and can be complex, especially if you throw in multi-modules in the mix, but it can also be a bit quirky.
这篇关于如何在Maven程序集中排除资源文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!