我在Gradle中有一个多模块项目。

我将常用功能重构为名为common的模块。

我在多模块项目的另一个模块(让我们说module A)中进行了测试,这些模块使用了src/main/java模块的common下的类。

我可以从common的测试类中的module A模块导入这些类,但是当我运行测试时,出现以下错误:



这是build.gradlemodule A文件,它依赖common模块进行测试(我已经尝试了所有这些选项):

 dependencies  {
     compile project(':common')
     testCompile project(':common')
     testRuntime project(':common')
     runtime project(':common')
     implementation project(":common")
     testCompile 'junit:junit:4.12'
     testImplementation 'junit:junit:4.12'
     implementation 'junit:junit:4.12'
     testCompileOnly project(':common')
     testRuntimeOnly project(':common')
     testImplementation project(':common')
     runtimeOnly project(':common')
     testCompile project(":common").sourceSets.test.output
     compile project(":common").sourceSets.test.output
     testRuntime fileTree(dir: 'libs', include: ['*.jar'])
}

我还验证了在common/build/libs中创建了一个jar。
我还能尝试什么?

最佳答案

在这么少的背景下回答您的问题有点困难,但无论如何我还是要尝试解决。据我了解,您具有类似于以下的目录结构(不包括Gradle Wrapper文件):

.
├── common
│   ├── build.gradle
│   └── src
│       └── main
│           └── java
│               └── common
│                   └── Foobar.java
├── moduleA
│   ├── build.gradle
│   └── src
│       └── test
│           └── java
│               └── FoobarTest.java
└── settings.gradle

我可以使用以下文件内容从根目录成功运行./gradlew :moduleA:test(5.6.2版):
./common/build.gradle
plugins {
    id 'java'
}
./common/src/main/java/common/Foobar.java
package common;

public class Foobar {
    public static void main(String... args) {
        System.err.println("Foobar");
    }
}
./moduleA/build.gradle
plugins {
    id 'java'
}

repositories {
    jcenter()
}

dependencies {
    testImplementation project(':common')
    testImplementation 'junit:junit:4.12'
}
./moduleA/src/test/java/FoobarTest.java
import common.Foobar;

public class FoobarTest {

    @org.junit.Test
    public void myTest() {
        org.junit.Assert.assertNotNull(Foobar.class);
    }
}
./settings.gradle
include 'common', 'moduleA'

如前所述,很难说出错误的确切来源。如果您无法使用我的最小设置来修复自己的代码,则可以尝试使用minimal, reproducible example更新您的问题,以解决不起作用的设置。

10-08 01:51