Relocating fastxml.jackson classes to my.package.fastxml.jackson

女生的网名这么多〃 提交于 2019-12-02 08:46:15

问题


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!


回答1:


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-jars\kuku-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-jars\kuku-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-jars\kuku-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>



来源:https://stackoverflow.com/questions/34764732/relocating-fastxml-jackson-classes-to-my-package-fastxml-jackson

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!