如何检查maven依赖关系兼容性



我正试图弄清楚是否可以检查maven依赖项在项目的配置中是否仍然兼容。

这是我的测试设置:

共有3个项目。Child-AChild-BChild-C

Child-A有两个版本,它们彼此不兼容。版本0.0.1-SNAPSHOT具有方法

public void myMethod(String oneParameter)

版本0.0.2-SNAPSHOT将此方法更改为

public void myMethod(String oneParameter, String secondParameter)

Child-B依赖于版本0.0.1-SNAPSHOT中的Child-A,并使用一个参数调用方法。

public class ChildB {
    public void callChild(String myParam) {
        final ChildA test = new ChildA();
        String methodParam = String.format("%s is calling %s with Parameter %s ", this.getClass().getName(),
                test.getClass().getName(), myParam);
        test.myMethod(methodParam);
    }
}

CCD_ 10现在具有对Child-B的依赖性和对Child-a版本CCD_。

Child-C以这种方式调用Child-B

public static void main(String[] args) {
    ChildB inner = new ChildB();
    inner.callChild(" Parameter from main method! ");
}

对于编译器来说,这很好,但在运行时Child-B会遇到麻烦,因为Child-A存在于版本0.0.2-SNAPSHOT中,因此只有一个参数的方法不再存在。

我试图以这种方式配置我的maven设置,以便在构建Child-C时,它将检查其依赖项的签名/兼容性及其有效pom的设置。

我认为maven动物嗅探器插件可能是一个解决方案,但没有找到检查内部依赖关系的部分。

有人知道如何制止这些行为吗?

问题不在于Maven。即使两个版本在运行时都存在,JVM也只加载一个版本,因此由于缺少一个或另一个方法,您将获得运行时异常。

最好的解决方案是将单参数方法添加到0.0.2版本,并在所有pom中指定该版本。如果这不起作用,您将需要修改代码,以便只调用双参数方法。

您可以使用maven强制器来实现依赖收敛。

https://maven.apache.org/enforcer/maven-enforcer-plugin/

<build>
<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-enforcer-plugin</artifactId>
        <version>1.0.1</version>
        <executions>
            <execution>
                <id>enforce</id>
                <configuration>
                    <rules>
                        <DependencyConvergence/>
                    </rules>
                </configuration>
                <goals>
                    <goal>enforce</goal>
                </goals>
                <phase>validate</phase>
            </execution>
        </executions>
    </plugin>
</plugins>

这将向您展示像这样的依赖收敛问题:

  Dependency convergence error for org.codehaus.jackson:jackson-jaxrs:1.7.1 paths to dependency are:
+-com.nodeable:server:1.0-SNAPSHOT
  +-org.mule.modules:mule-module-jersey:3.2.1
    +-com.sun.jersey:jersey-json:1.6
      +-org.codehaus.jackson:jackson-jaxrs:1.7.1
and
+-com.nodeable:server:1.0-SNAPSHOT
  +-org.mule.modules:mule-module-jersey:3.2.1
    +-org.codehaus.jackson:jackson-jaxrs:1.8.0

通过这种方式,您可以确保不会将同一库的多个版本作为依赖项,并消除类加载问题的可能性。

如果您提到的方法的签名发生了更改,那么您应该能够在编译时看到错误。

最新更新