Skip to content

Linkage Checker Enforcer Rule Tutorial

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

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

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’ll see NoSuchMethodError:

$ mvn clean compile
...
[INFO] BUILD SUCCESS
$ mvn 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 the enforcer rule.)

Why did the NoSuchMethodError occur? Let’s look at dependency graph.

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: