Warm tip: This article is reproduced from stackoverflow.com, please click
gradle java java-module google-truth

How to fix a split-package in test dependencies?

发布于 2020-03-27 10:27:22

I have created an example project to explain the issue I am having. I have tests that use two dependency jars which have a split package. In this case google-truth has the split-package.

build.gradle

plugins {
    id 'java-library'
    id 'org.javamodularity.moduleplugin' version '1.5.0'
}

group 'example'
version '1.0-SNAPSHOT'

sourceCompatibility = 11

repositories {
    mavenCentral()
}

dependencies {
    testImplementation group: 'junit', name: 'junit', version: '4.12'
    testImplementation 'com.google.truth:truth:0.42'
    testImplementation 'com.google.truth.extensions:truth-java8-extension:0.42'
}

src/main/java/module-info.java

module truth.tests.main {
    exports example.truthtest;
}

src/test/java/module-info.test

--add-reads
  truth.tests.main=truth

src/test/java/example/truthtest/ATest.java

package example.truthtest;

import org.junit.Test;

import static com.google.common.truth.Truth.assertThat;

public class ATest {
    @Test
    public void test() {
        assertThat(15).isEqualTo(15);
    }
}

When I run the build I get the following error.

> Task :compileTestJava FAILED
error: the unnamed module reads package com.google.common.truth from both truth and truth.java8.extension
error: module truth reads package com.google.common.truth from both truth and truth.java8.extension
error: module hamcrest.core reads package com.google.common.truth from both truth and truth.java8.extension
error: module com.google.common reads package com.google.common.truth from both truth and truth.java8.extension
error: module checker.compat.qual reads package com.google.common.truth from both truth and truth.java8.extension
error: module checker.qual reads package com.google.common.truth from both truth and truth.java8.extension
error: module diffutils reads package com.google.common.truth from both truth and truth.java8.extension
error: module auto.value.annotations reads package com.google.common.truth from both truth and truth.java8.extension
error: module error.prone.annotations reads package com.google.common.truth from both truth and truth.java8.extension
error: module jsr305 reads package com.google.common.truth from both truth and truth.java8.extension
error: module j2objc.annotations reads package com.google.common.truth from both truth and truth.java8.extension
error: module animal.sniffer.annotations reads package com.google.common.truth from both truth and truth.java8.extension
error: module truth.java8.extension reads package com.google.common.truth from both truth and truth.java8.extension
error: module junit reads package com.google.common.truth from both truth.java8.extension and truth
error: module truth.tests.main reads package com.google.common.truth from both truth and truth.java8.extension
15 errors

FAILURE: Build failed with an exception.

I believe the problem is that truth and truth.java8.extension both contain classes in the same package com.google.truth. If I remove truth-java8-extensions the tests compile but this is a required dependency in my actual project. How can I get these two modules to work together in tests using java's module system?

When I debug gradle these are the compiler options that fail (paths are truncated).

Compiler arguments: 
-source 11 
-target 11 
-d build\classes\java\test 
-g 
-sourcepath  
-proc:none 
-s build\generated\sources\annotationProcessor\java\test -XDuseUnsharedTable=true 
-classpath  
--module-path build\classes\java\main;build\resources\main;truth-java8-extension-0.42.jar;truth-0.42.jar;junit-4.12.jar;hamcrest-core-1.3.jar;guava-25.1-android.jar;checker-compat-qual-2.5.3.jar;checker-qual-2.5.3.jar;diffutils-1.3.0.jar;auto-value-annotations-1.6.2.jar;error_prone_annotations-2.3.1.jar;jsr305-3.0.2.jar;j2objc-annotations-1.1.jar;animal-sniffer-annotations-1.14.jar 
--patch-module truth.tests.main=src\test\java 
--add-modules junit 
--add-reads truth.tests.main=junit src\test\java\example\truthtest\ATest.java
Questioner
John Mercier
Viewed
77
John Mercier 2019-07-04 01:46

The fix is to patch truth with truth.java8.extension. This can be done in gradle with patchModules.

patchModules.config = [
        "truth=truth-java8-extension-0.42.jar"
]

This config results in the following:

Compiler arguments: 
-source 11 
-target 11 
-d build\classes\java\test 
-g 
-sourcepath  
-proc:none 
-s build\generated\sources\annotationProcessor\java\test 
-XDuseUnsharedTable=true 
-classpath  
--module-path build\classes\java\main;build\resources\main;truth-0.42.jar;junit-4.12.jar;hamcrest-core-1.3.jar;guava-25.1-android.jar;checker-compat-qual-2.5.3.jar;checker-qual-2.5.3.jar;diffutils-1.3.0.jar;auto-value-annotations-1.6.2.jar;error_prone_annotations-2.3.1.jar;jsr305-3.0.2.jar;j2objc-annotations-1.1.jar;animal-sniffer-annotations-1.14.jar 
--patch-module truth.tests.main=src\test\java 
--add-modules junit 
--add-reads truth.tests.main=junit 
--patch-module truth=${gradle-path}\truth-java8-extension-0.42.jar 
--add-reads truth.tests.main=truth src\test\java\example\truthtest\ATest.java

With the extensions patch to truth the tests now work. The nice thing is this configuration fills in the path to the jar automatically.