如何使maven-assembly-plugin遵守pom.xml中定义的排除项? [英] How to have maven-assembly-plugin respect the exclusions defined in pom.xml?

查看:177
本文介绍了如何使maven-assembly-plugin遵守pom.xml中定义的排除项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果在POM文件中排除了某些(特定的)传递依赖项,但是将程序集描述符设置为获取所有依赖项,则将包括排除的依赖项. 如何防止这种情况?

When some (specific) transitive dependencies are excluded in the POM file but the assembly descriptor is set to fetch all dependencies, the excluded dependencies will be included in assembly. How can I prevent this?

某些依赖项可能很难处理,因为它们的groupId和artifactIds在几乎每个版本(在我的情况下为bouncycastle)中都会发生变化.

Some dependencies may be tricky to handle because their groupIds and artifactIds change at almost each version (in my case, bouncycastle).

我正在检索bouncycastle的多个版本(138、1.38、1.45和1.50).我的目的是消除除1.50以外的所有版本.确切地说,我有一个依赖项(让我们称之为some.perfectly.done:job)可以导入1.50,另一个(how.many.castles:do-you-need)可以导入所有其他依赖.它们是公司的依存关系,因此为您提供真实的groupId:artifactId不会帮助您进行测试.

I am retrieving several versions of bouncycastle (138, 1.38, 1.45 and 1.50). My purpose is to eliminate all versions other than 1.50. To be precise, I have one dependency (let's call it some.perfectly.done:job) which imports 1.50 and one other (how.many.castles:do-you-need) which imports all others. They are corporate dependencies, so giving you real groupId:artifactId wouldn't help you for testing.

我的依赖项声明如下:

<dependency>
    <groupId>some.perfectly.done</groupId>
    <artifactId>job</artifactId>
</dependency>
<dependency>
    <groupId>how.many.castles</groupId>
    <artifactId>do-you-need</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.bouncycastle</groupId>
            <artifactId>*</artifactId>
        </exclusion>
        <exclusion>
            <groupId>bouncycastle</groupId>
            <artifactId>*</artifactId>
        </exclusion>
    </exclusions>
</dependency>

assembly.xml

<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">
    <id>bin</id>
    <formats>
        <format>zip</format>
    </formats>

    <!-- Adds dependencies to zip package under lib directory -->
    <dependencySets>
        <dependencySet>
            <useProjectArtifact>false</useProjectArtifact>
            <useTransitiveFiltering>true</useTransitiveFiltering>
            <outputDirectory>lib</outputDirectory>
                        <outputFileNameMapping>${artifact.groupId}.${artifact.artifactId}-${artifact.version}${dashClassifier?}.${artifact.extension}</outputFileNameMapping>
            <unpack>false</unpack>
        </dependencySet>
    </dependencySets>
    <!-- ... -->
</assembly>

我得到的东西

$ ls *bouncycastle*
bouncycastle.bcmail-jdk14-138.jar       org.bouncycastle.bcmail-jdk15on-1.50.jar  org.bouncycastle.bcprov-jdk15-1.45.jar
bouncycastle.bcprov-jdk14-138.jar       org.bouncycastle.bcpkix-jdk15on-1.50.jar  org.bouncycastle.bcprov-jdk15on-1.50.jar
org.bouncycastle.bcmail-jdk14-1.38.jar  org.bouncycastle.bcprov-jdk14-1.38.jar    org.bouncycastle.bctsp-jdk14-1.38.jar

我期望(和需要)

$ ls *bouncycastle*
org.bouncycastle.bcmail-jdk15on-1.50.jar  org.bouncycastle.bcpkix-jdk15on-1.50.jar  org.bouncycastle.bcprov-jdk15on-1.50.jar

一些可能的线索可以建立

这里答案的真正价值是找到一个 real generic 解决方案.我不想解决我的案件,我想为所有有类似案件的人找到解决方案.

Some possible leads to build upon

The real value in an answer here would be to find a real, generic solution. I am not looking to solve my case, I'd like to find a solution for all people with a similar case.

因此,我想避免一些可行的解决方案,但这些解决方案确实与特定情况有关,并且经常需要将POM的逻辑复制到汇编描述符中.

As such, I'd like to avoid some solutions which would work but are really related to a particular case and often requires to duplicate the POM's logic into the assembly descriptor.

但是,如果找不到更好的线索,这些线索可能会有所帮助.

These are however leads that may help if nothing better can be found.

很明显.在我自己的情况下,除了智能地使用includes/excludes之外,这是不切实际的.这不是实际的解决方案.

Obviously. In my own case though, quite impractical, except by the intelligent use of includes/excludes. This is not a practical solution.

请注意,我知道已经有人问过这个问题,但是唯一的答案对我来说并不令人满意情况:

Note that I am aware this question has already been asked, but the only attempt at an answer is unsatisfactory for my case:

如果您有这种事情,则需要定义两个dependencySet条目,其中一个包括logb​​ack [with useTransitiveDependencies=false]和另一个.

(由 khmarbaise )

与上述相同的问题上,提出了一种在不提出任何问题的情况下可以使用的方法:使用 dependency:copy-dependencies 复制正确的依赖项到一个临时目录,然后从该目录组装我的zip.

On the same question as above, an approach which I might use if no question is asked has been proposed: first use dependency:copy-dependencies to copy my correct dependencies to a temporary directory, then assemble my zip from this directory.

作为一种解决方法,这可能是最有效的解决方案,因为它是通用的,并且不会从程序集描述符中的POM复制逻辑,尽管这会使构建时间更长.

As a workaround, this is probably the most valid solution, as being generic and not duplicating the logic from the POM in the assembly descriptor, though this makes the build longer.

为什么maven-assembly-plugin这样表现?我没有在文档中找到对此的任何引用.是期望的行为,还是(已知/未知)错误?

Why does the maven-assembly-plugin behave this way? I did not find any reference to this in the documentation. Is it desired behavior, or is it a (known/unknown) bug?

推荐答案

编辑

maven-assembly-plugin的版本3.1.1

Edit

Version 3.1.1 of the maven-assembly-plugin have been released Jan 01, 2019. It solves the JIRA issues mentioned in my answer below (kept for history or for users that cannot upgrade the plugin). With this version, maven-assembly-plugin now honors wildcards in dependencies exclusions. Simply upgrade the plugin.

看来Maven程序集插件(版本< = 3.1.0 )不支持使用通配符(*)进行的排除,请参见 MASSEMBLY- 675 .从上一个JIRA问题开始,该问题将在插件的3.1.1版本中解决(请参见

It appears that Maven assembly plugin (version <= 3.1.0) does not honor exclusions using wildcards (*), see MASSEMBLY-762, MASSEMBLY-861 or MASSEMBLY-675. From the last JIRA issue, the problem will be solved in version 3.1.1 of the plugin (see commit). At the moment of writing, the 3.1.1 version is not released : 3.1.0 is the latest version.

希望该问题可以通过版本< = 3.1.0来解决.

Hopefully, the problem can be solved with version <= 3.1.0.

为此,只需声明正确的排除工件而不是*,它便可以正常工作.列出所有被排除的依赖关系可能会很痛苦,但是至少我认为这是一个比被接受的解决方案更好的解决方案(较少的Maven阶段调整),尤其是在具有交叉依赖关系的多模块项目中(我的情况).此外,这样,您就可以更好地控制依赖项.

To do so, just declare the right excluded artifacts instead of *, and it works properly. It can be painful to list all excluded dependencies, but at least I think it is a better solution than the accepted one (less tweaking of Maven phases), especially on multi-modules project with cross dependencies (my case). Moreover, by doing so, you have more control about the dependencies.

希望对您有所帮助,请等待3.1.1版本! :)

Hope that helps, waiting the 3.1.1 version! :)

这篇关于如何使maven-assembly-plugin遵守pom.xml中定义的排除项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆