Maven 依赖版本冲突(google.protobuf)

Maven dependency versions conflict (google.protobuf)

我有一个使用 org.openstreetmap.osmosisspark 依赖项的项目。他们都有一个 com.google.protobuf intern 依赖,但需要它的不同版本。当我构建我的项目时,Maven 选择这些子依赖项之一,进一步执行会导致运行时错误。不幸的是,显式声明 protobuf 的依赖关系也无济于事。

我听说使用 maven shade plugin 是个好主意,但我无法很好地配置它。有人可以帮我吗?

shade插件使用示意图:

<plugin>
    <groupId> org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.4.2</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <relocations>
                    <relocation>
                        <pattern>com.google.protobuf</pattern>
                        <shadedPattern>${project.groupId}.${project.artifactId}.shaded.protobuf</shadedPattern>
                    </relocation>
                </relocations>
            </configuration>
        </execution>
    </executions>
</plugin>

Shade 插件在这里无能为力。引用它的文档:

This plugin provides the capability to package the artifact in an uber-jar, including its dependencies and to shade - i.e. rename - the packages of some of the dependencies

这不是您需要的。您需要将一个版本的 protobuf 的类路径与另一个版本的类路径分开。这是一件复杂的事情,你必须尝试使用​​类加载器或一些模块化机制,如 OSGi。

但我会首先尝试明确声明这两个 protobuf 版本的最新版本,并检查它是否向后兼容。