使用 Kryo 3.0 的 Maven 阴影插件:ClassNotFoundException

Maven shade plugin with Kryo 3.0: ClassNotFoundException

我是 Maven 的新手,我想将项目从 Kryo 2.22 更新到 3.0.1。在项目中我有以下依赖。

<dependency>
    <groupId>com.esotericsoftware.kryo</groupId>
    <artifactId>kryo</artifactId>
    <version>2.22</version>
</dependency>

项目使用Maven shade插件创建jar文件:

<plugin>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.3</version>
    <executions>
        <execution>
            <id>shade-gremlin-groovy</id>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <createDependencyReducedPom>false</createDependencyReducedPom>
                <artifactSet>
                    <includes>
                        <!-- As of Kryo 2.22, this artifact includes both minlog and reflectasm -->
                        <!-- If we upgrade to later a Kryo version, we may have to add includes
                             for minlog and reflectasm (this is true of 2.24.0, not sure about 3) -->
                        <include>com.esotericsoftware.kryo:*</include>
                    </includes>
                </artifactSet>
                <filters>
                    <filter>
                        <artifact>*:*</artifact>
                        <excludes>
                            <exclude>reflectasm-1.07-shaded.jar</exclude>
                            <exclude>minlog-1.2.jar</exclude>
                            <exclude>objenesis-1.2.jar</exclude>
                            <exclude>META-INF/MANIFEST.MF</exclude>
                        </excludes>
                    </filter>
                </filters>
                <relocations>
                    <relocation>
                        <pattern>com.esotericsoftware.kryo</pattern>
                        <shadedPattern>com.thinkaurelius.shaded.kryo_2_22</shadedPattern>
                    </relocation>
                    <relocation>
                        <pattern>com.esotericsoftware.minlog</pattern>
                        <shadedPattern>com.thinkaurelius.shaded.minlog_1_2</shadedPattern>
                    </relocation>
                    <relocation>
                        <pattern>com.esotericsoftware.reflectasm</pattern>
                        <shadedPattern>com.thinkaurelius.shaded.reflectasm_1_07</shadedPattern>
                    </relocation>
                    <relocation>
                        <pattern>com.esotericsoftware.shaded.org.objenesis</pattern>
                        <shadedPattern>com.thinkaurelius.shaded.objenesis_1_2</shadedPattern>
                    </relocation>
                </relocations>
                <!-- false below means the shade plugin overwrites the main project artifact (the one with no classifier).
                     false does *not* actually detach the main artifact, despite what the option name suggests. -->
                <shadedArtifactAttached>false</shadedArtifactAttached>
                <minimizeJar>false</minimizeJar>
            </configuration>
        </execution>
    </executions>
</plugin>

但是除了 groupId 之外,我不知道应该更改什么:<groupId>com.esotericsoftware.kryo</groupId> 如果我将版本号更改为 3.0.1,它找不到带阴影的 类(抛出 ClassNotfound 异常:

java.lang.NoClassDefFoundError: com/thinkaurelius/shaded/kryo_3_0_1/kryo/Serializer
at java.net.URLClassLoader.run(URLClassLoader.java:372)
at java.net.URLClassLoader.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at com.thinkaurelius.titan.graphdb.database.serialize.kryo.KryoSerializer.<init>(KryoSerializer.java:71)
at com.thinkaurelius.titan.graphdb.database.serialize.StandardSerializer.<init>(StandardSerializer.java:29)
at com.thinkaurelius.titan.graphdb.database.serialize.StandardSerializer.<init>(StandardSerializer.java:41)
at com.thinkaurelius.titan.diskstorage.configuration.backend.KCVSConfiguration.<init>(KCVSConfiguration.java:69)
at com.thinkaurelius.titan.diskstorage.configuration.backend.KCVSConfiguration.<init>(KCVSConfiguration.java:57)
at com.thinkaurelius.titan.diskstorage.configuration.KCVSConfigTest.getConfig(KCVSConfigTest.java:31)

我认为问题在于 Kryo 2.22 的 groupId (com.esotericsoftware.kryo) 与 Kryo 3.0.1 不同,并且 Maven 无法连接依赖项。我该如何解决这个问题?

从您链接到的 maven repo search 看来,依赖项似乎在 Maven 中央存储库中。但是,groupId 在 2.2.x 和 3.x.

之间发生了变化

您尝试过:

<dependency>
    <groupId>com.esotericsoftware</groupId>
    <artifactId>kryo</artifactId>
    <version>3.0.1</version>
</dependency>

即和你一样,但没有尾随 .kryo

maven shade 插件中出现了以下行:

<include>com.esotericsoftware.kryo:*</include>

必须更改为:

<include>com.esotericsoftware:*</include>

由于groupId不同。所以问题是依赖不匹配。