用Maven解决一个罐子地狱? [英] Solving a jar hell with maven?

查看:87
本文介绍了用Maven解决一个罐子地狱?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用两个jars A和B.B是一个库,而A具有使用库B中一些旧类的类.现在,当我在项目类路径中同时包含两个jars时,这给我带来了一个问题两个类的名称相同,但其中一个类比另一个类老,并且行为也有所不同.

I'm using two jars A and B. B is a library and A has classes that uses some old classes from library B. Now this is causing me a problem when I include both jars in my project classpath as there are the same names of two classes but one of them is older than the other and behave differently.

我找到的解决此问题的方法是,首先将库B导入Eclipse,然后单击确定",然后构建项目.然后,我添加了jarA.这样,我所有现有的代码都将使用B的较新版本,而A的类将保持不变.

One solution to this problem I found is by first importing library B into Eclipse and then I click OK and the project builds. Then I add the jar A. This way all my existing code will use the newer versions of B and the classes of A will be untouched.

但是,现在我想在我的项目中使用Maven,但是我不知道如何再次使用Maven进行此技巧.请帮忙.

However now I want to use Maven for my projects but I'm unable to know how to make this trick again using Maven. Please help.

推荐答案

也许您可以通过重命名不需要的类的包来解决您的问题.

Maybe you can solve your problem by renaming the package of the class that you don't want.

您可以使用 Maven Shade插件

此插件允许在编译时重命名程序包名称.

This plugin allows to rename package names at compilation.

用法:

   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <executions>
        <execution>
        <phase>package</phase>
        <goals>
            <goal>shade</goal>
            </goals>
        <configuration>
                <relocations>
                    <relocation>
                            <pattern>com.example.package.name.YourClass</pattern>
                               <shadedPattern>com.example.rename.package.name.YourClass</shadedPattern>
                    </relocation>
                </relocations>
                    <promoteTransitiveDependencies>true</promoteTransitiveDependencies>
        </configuration>
        </execution>
    </executions>
</plugin>

这篇关于用Maven解决一个罐子地狱?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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