从依赖项 jar 中排除 Java 包 [英] Exclude Java package from dependency jar
问题描述
我想使用第三方供应商的 jar.但是在这个 jar 中,我有旧版本的 Java 包 org.osgi.framework
我需要找到一些方法将包从主项目中排除.像这样的:
I want to use jar from third party vendor. But in this jar I have old version of Java package org.osgi.framework
I need to find some way to exclude the package from the main project. Something like this:
<dependency>
<groupId>com.ibm</groupId>
<artifactId>com.ibm.ws.admin.client</artifactId>
<version>8.5.0</version>
<exclusions>
<exclusion>org.osgi.framework</exclusion>
</exclusions>
<type>jar</type>
</dependency>
你能推荐一些解决方案吗?
Can you recommend some solution?
推荐答案
虽然更好的解决方案是使用分类器(如 this answer)并将其发布到您的企业 Maven 存储库(或 将其安装到您的本地 Maven 缓存中),下面是一个不同的解决方案,它也应该满足您的需求.
Although a better solution would be to re-pack the dependency (without the unwanted package) with a classifier (as described in this answer) and publish it on your enterprise Maven repository (or install it into your local Maven cache, if it's a personal project), below is a different solution which should also suit your needs.
你可以有一个 多模块 Maven 项目,只有一个模块这个依赖,你可以在其中使用 Maven Shade Plugin 及其 filters
属性在其 official示例.
You could have a multi-module Maven project, having a module with just this dependency and in it you could use the Maven Shade Plugin and its filters
property as explained in its official example.
根据 文档,过滤器
元素:
要使用的存档过滤器.允许您以 artifactSet 使用的复合标识符的形式指定一个工件,以及一组包含/排除文件模式,用于过滤将归档的哪些内容添加到阴影 jar 中
Archive Filters to be used. Allows you to specify an artifact in the form of a composite identifier as used by artifactSet and a set of include/exclude file patterns for filtering which contents of the archive are added to the shaded jar
在您的情况下,以下配置应应用过滤器:
In your case, the following configuration should apply the filter:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>com.ibm:com.ibm.ws.admin.client</artifact>
<excludes>
<exclude>org/osgi/framework/**</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
package
阶段生成的 jar 不应再包含该包.作为 Maven 输出的一部分,您应该会看到:
The generated jar from the package
phase should not contain that package any longer. As part of the Maven output you should see:
[INFO] --- maven-shade-plugin:2.4.3:shade (默认) @ test-checksum ---
[INFO] 在阴影 jar 中包含 com.ibm:com.ibm.ws.admin.client:jar:8.5.0.
[INFO] 用阴影工件替换原始工件.
[INFO] --- maven-shade-plugin:2.4.3:shade (default) @ test-checksum ---
[INFO] Including com.ibm:com.ibm.ws.admin.client:jar:8.5.0 in the shaded jar.
[INFO] Replacing original artifact with shaded artifact.
可以验证生成的jar的内容,过滤后的包应该没有.
You can verify the content of the generated jar, the filtered package should not be there.
然后,此模块的输出将包含您正在寻找的新"/过滤的 jar.然后消费者模块只需要对该模块有依赖关系,因此应用了过滤器.
这种多模块项目的一个例子是:
Then, the output of this module will have the "new"/filtered jar you were looking for. Then the consumer module would just need to have a dependency on this module and as such have the filter applied.
An example of such a multimodule project would be:
+ aggregator/parent project
- filtered-dependency-module (applying the shade filter)
- consumer-module (having dependency on the filtered module)
更新
进一步注意:在应用过滤器的模块中,您应该将依赖项声明为 可选这样消费者模块就不会再次传递它.
Update
Further note: in the module which applies the filter, you should declare the dependency as optional so that the consumer module doesn't bring it in transitively again.
<dependencies>
<dependency>
<groupId>com.ibm</groupId>
<artifactId>com.ibm.ws.admin.client</artifactId>
<version>8.5.0</version>
<optional>true</optional>
</dependency>
</dependencies>
Optional 不会影响模块本身,只会影响消费者.并且 Shade 插件将继续工作(我重新测试了它,以防万一).
Optional doesn't affect the module itself, only the consumer one. And the Shade plugin will keep on working (I re-tested it, just in case).
这篇关于从依赖项 jar 中排除 Java 包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!