简体   繁体   中英

Managing package dependencies

It's been a while since I did a much coding, so bear with me for some basic questions!

I'm trying to write a plugin for Atlassian Confluence which integrates with another third party platform. This third party platform provides an SDK library to use with its REST API. It's a handy thing to use as I'm really just trying to prototype a concept quickly. I'm using Java 1.8, IntelliJ and Maven and Confluence 5.8.

Here is my first problem - the third party SDK library that I want to include as a dependency has dependencies of it's own. Among others, this is an example:

 <!-- 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>

It goes on, but you get the idea. My first problem is that I could not get the third party SDK library successfully added and the project to compile without adding ALL of the SDK's dependencies to my own project. Is that expected? It bloats the size of my jar quite substantially.

This leads into the next problem. Confluence already uses an older - and Atlassian-customised - version of the Jersey library (1.8-atlassian-16) which I suspect is clashing with the newer Jersey library (1.19.1) used by the SDK library. If I specify the Atlassian Jersey version in my project, it compiles and installs but fails when I try to run it. If I specify the Jersey version the SDK library uses, it compiles but doesn't install properly. Is there anything I can do about this Jersey library version difference so that I can use the SDK library in my project?

I'll provide error details if requested, but I'm pretty sure it's related to the Jersey version clash as errors in both cases are "NoSuchMethod" and class cast exceptions for Jersey classes.

The situation of 2 different SDK's using different versions of library is a little tricky to solve. For example here in your case , you will have to find a transitive version of the Jersey dependency that can solve both. You will have to find out what are the dependencies of the external SDK's (using tree) you are including and choose to exclude the specific version it refers to (using excludes) , bundle up a transitive version that can solve both the dependencies and run maven goals. You will get a good idea from these threads enter link description here and enter link description here as well. Hope it helps

You can use some tool to repackage your 3rd party SDK together with its dependencies, renamings the package name to avoid conflicts with confluence's own jar files.

For example, jarjar .

Or better, use 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>

This instructs the plugin to move classes from the package org.codehaus.plexus.util and its subpackages into the package org.shaded.plexus.util by moving the corresponding JAR file entries and rewritting the affected bytecode. The class Xpp3Dom and some others will remain in their original package.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM