Skip to content

Linkage Checker Enforcer Rule Tutorial

Tomo Suzuki edited this page Sep 6, 2019 · 23 revisions

(status: draft)

In this tutorial, you’ll learn how to apply the enforcer rule to statically detect Java diamond dependency conflicts using the enforcer rule. The target audience is Java developers who already know Maven command (mvn) and JDK 8.

Setup

Checkout project via Git. Our project contains example-problems directory.

$ git clone https://github.com/GoogleCloudPlatform/cloud-opensource-java.git
$ cd cloud-opensource-java/example-problems/no-such-method-error-signature-mismatch

The following commands assume that this no-such-method-error-signature-mismatch as current working directory.

Compile and run the project. You observe NoSuchMethodError:

$ mvn clean compile exec:java
...
java.lang.NoSuchMethodError: com.google.common.base.Verify.verify(ZLjava/lang/String;Ljava/lang/Object;)V
    at io.grpc.internal.DnsNameResolver.maybeChooseServiceConfig (DnsNameResolver.java:514)
    at io.grpc.internal.App.main (App.java:31)
...
[INFO] BUILD FAILURE

Now you have a Maven project with a diamond dependency conflict.

Diagnosis of Linkage Error

(This section can be skipped if you just want to learn Linkage Checker Enforcer Rule.)

Linkage Checker Enforcer Rule Tutorial Problem Diagnosis

Linkage Checker Enforcer Rule

Apply Linkage Checker Enforcer Rule to the project.

Add following section to the pom.xml.

Run the enforcer rule.

It detects the errors.

Fix the Linkage Error

The error occurred because the two artifacts and their transitive dependencies are not compatible with each other. To fix the problem, you change the version of the XXX:

Run the linkage checker enforcer rule again. This time you don’t see the errors: Run the main class again. This time it succeeds to run: