管理软件包依赖关系

问题描述:

自从我编写了很多代码之后,已经有一段时间了,因此请耐心等待一些基本问题!管理软件包依赖关系

我正在尝试为Atlassian Confluence编写一个与另一个第三方平台集成的插件。该第三方平台提供了一个SDK库,以用于其REST API。这是一个方便的使用方法,因为我只是试图快速构建一个概念的原型。我使用Java 1.8,IntelliJ和Maven和Confluence 5.8。

这是我的第一个问题 - 我想包含作为依赖项的第三方SDK库具有它自己的依赖关系。其中,这是一个例子:

<!-- HTTP client: jersey-client --> 
    <dependency> 
     <groupId>com.sun.jersey</groupId> 
     <artifactId>jersey-client</artifactId> 
     <version>${jersey-version}</version> 
    </dependency> 
    <dependency> 
     <groupId>com.sun.jersey.contribs</groupId> 
     <artifactId>jersey-multipart</artifactId> 
     <version>${jersey-version}</version> 
    </dependency> 

    <!-- JSON processing: jackson --> 
    <dependency> 
     <groupId>com.fasterxml.jackson.core</groupId> 
     <artifactId>jackson-core</artifactId> 
     <version>${jackson-version}</version> 
    </dependency> 
    <dependency> 
     <groupId>com.fasterxml.jackson.core</groupId> 
     <artifactId>jackson-annotations</artifactId> 
     <version>${jackson-version}</version> 
    </dependency> 

它继续,但你明白了。我的第一个问题是,我无法成功添加第三方SDK库,并且无需将所有SDK的依赖项添加到我自己的项目中即可进行编译。 这是预期的吗?它大大膨胀了我的罐子的大小。

这会导致下一个问题。 Confluence已经使用了Jersey库(1.8-atlassian-16)的老版本和Atlassian定制版本,我猜想它与SDK库使用的新泽西库(1.19.1)发生冲突。如果我在我的项目中指定了Atlassian Jersey版本,它会编译并安装,但在尝试运行时会失败。如果我指定SDK库使用的Jersey版本,它会编译但不能正确安装。 有什么我可以做的关于泽西图书馆版本差异,以便我可以在我的项目中使用SDK库?

如果有要求,我会提供错误的详细信息,但我很确定它与泽西岛版本冲突有关,因为这两种情况下的错误都是“NoSuchMethod”和类别强制转换泽西类的异常。

2个不同SDK使用不同版本的库的情况有点难以解决。例如在你的情况下,你将不得不找到可以解决这两个问题的Jersey依赖关系的可传递版本。您将必须找出外部SDK(使用树)的依赖关系,并选择排除它引用的特定版本(使用排除项),捆绑一个可以解决依赖关系并运行maven的传递版本目标。您也可以从这些线索enter link description hereenter link description here中获得一个好主意。希望它有帮助

您可以使用一些工具重新打包第三方SDK及其依赖关系,重新命名包名以避免与confluence自带的jar文件发生冲突。例如,jarjar

或者更好,使用the maven shade plugin

<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> 
      <relocations> 
       <relocation> 
       <pattern>org.codehaus.plexus.util</pattern> 
       <shadedPattern>org.shaded.plexus.util</shadedPattern> 
       <excludes> 
        <exclude>org.codehaus.plexus.util.xml.Xpp3Dom</exclude> 
        <exclude>org.codehaus.plexus.util.xml.pull.*</exclude> 
       </excludes> 
       </relocation> 
      </relocations> 
      </configuration> 
     </execution> 
     </executions> 
    </plugin> 
    </plugins> 
</build> 

这指示通过移动从包装org.codehaus.plexus.util类及其子包到包org.shaded.plexus.util插件移动相应的JAR文件条目并重写受影响的字节码。 Xpp3Dom和其他一些类将保留在原始包中。