从依赖项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?
推荐答案
尽管更好的解决方案是使用分类器重新包装依赖项(不包含不需要的包)(如到本地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插件及其 filters
属性,如其官方例子.
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所使用的复合标识符的形式指定工件,并使用一组包含/排除文件模式来过滤将归档文件的哪些内容添加到有色罐子中
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>
可选不影响模块本身,仅影响使用方.并且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屋!