Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
774 views
in Technique[技术] by (71.8m points)

maven shade plugin - Relocating fastxml.jackson classes to my.package.fastxml.jackson

I'm trying to relocate the packages from "com.fasterxml.jackson" into my own package (ie, "mypackage.com.fasterxml.jackson") and then consume it in another JAR which I own.

I've managed to run the maven-shade plugin to do it using this configuration:

<plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <shadedArtifactAttached>false</shadedArtifactAttached>
                        <createDependencyReducedPom>true</createDependencyReducedPom>
                        <relocations>
                            <relocation>
                                <pattern>com.fasterxml.jackson</pattern>
                                <shadedPattern>mypackage.com.fasterxml.jackson</shadedPattern>
                            </relocation>
                        </relocations>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>

The problem that I'm facing is when I try to consume it, from some reason Eclipse keeps adding a dependency to the original jackson ("mypackage.com.fasterxml.jackson") and not the new one.

Just to be clear, my setup is: - Jar X have a dependency in ThirdPartyArtifcats. - ThirdPartyArtifcats references Jackson and run the maven-shade plugin, thus it contains a modified version of Jackson (mypackage.com.fasterxml.jackson).

When I try to use Jackson's ObjectMapper in Jar X, Eclipse keeps giving a reference to the original jackson.

I'll appreciate your help!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In the end I've used JarJar and created a modified JAR. Then I've added a dependency to my code and updated it manually so it now relates to the modified package instead of the original package.

Here's a procedure describing what I've done:


How to use JarJar in order to relocate classes of a JAR from one package to another

In this example we'll be changing the package from "com.fasterxml.jackson" to "io.kuku.dependencies.com.fasterxml.jackson". - The source JAR is called "jackson-databind-2.6.4.jar" and new modified (target) JAR is called "kuku-jackson-databind-2.6.4.jar". - The "jarjar" JAR file is in version 1.4

  1. Create a "rules.txt" file. The contents of the file should be (watch the period before the '@' character): rule com.fasterxml.jackson.** io.kuku.dependencies.com.fasterxml.jackson.@1

  2. Run the following command: java -jar jarjar-1.4.jar process rules.txt jackson-databind-2.6.4.jar kuku-jackson-databind-2.6.4.jar


Installing the modified JARs to the local repository

In this case I'm installing 3 files located on "c:my-jars" folder.

mvn install:install-file -Dfile=C:my-jarskuku-jackson-annotations-2.6.4.jar -DgroupId=io.kuku.dependencies -DartifactId=kuku-jackson-annotations -Dversion=2.6.4 -Dpackaging=jar

mvn install:install-file -Dfile=C:my-jarskuku-jackson-core-2.6.4.jar -DgroupId=io.kuku.dependencies -DartifactId=kuku-jackson-core -Dversion=2.6.4 -Dpackaging=jar

mvn install:install-file -Dfile=C:my-jarskuku-jackson-databind-2.6.4.jar -DgroupId=io.kuku.dependencies -DartifactId=kuku-jackson-annotations -Dversion=2.6.4 -Dpackaging=jar


Using the modified JARs in the project's pom

In this example, this is the "dependencies" element in the projects pom:

<dependencies>
<!-- ================================================== -->
<!-- kuku JARs -->
<!-- ================================================== -->
<dependency>
    <groupId>io.kuku.dependencies</groupId>
    <artifactId>kuku-jackson-annotations</artifactId>
    <version>2.6.4</version>
</dependency>
<dependency>
    <groupId>io.kuku.dependencies</groupId>
    <artifactId>kuku-jackson-core</artifactId>
    <version>2.6.4</version>
</dependency>
<dependency>
    <groupId>io.kuku.dependencies</groupId>
    <artifactId>kuku-jackson-databind</artifactId>
    <version>2.6.4</version>
</dependency>


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...