【问题标题】:IntelliJ can't resolve gradle java cross module test dependenciesIntelliJ 无法解析 gradle java 跨模块测试依赖项
【发布时间】:2016-06-17 20:16:11
【问题描述】:

IntelliJ 2016.1 摇篮 2.1

使用直接 gradle 在命令行上一切正常。

项目结构:

build.gradle
\module1\build.gradle
\module1\src\test\......
\module2\build.gradle
\module2\src\test\......

模块 2 正在使用模块 1 中的测试类。IntelliJ 显示“无法解析符号”错误。 IntelliJ 中的补救措施是“添加对模块的依赖”,它什么都不做。

如果我手动编辑 module1_text.iml 文件并添加

<orderEntry type="module" module-name="module1_test" production-on-test="" />

然后它会工作一点点。

IntelliJ 是否因为某种原因无法编辑 iml 文件?还是 gradle 中的某些配置不正确?

我的 build.gradle 包含以下方式的测试代码:

testCompile project (path: ':modules:module1')

【问题讨论】:

  • 只是对@capybaras 的 SO 问题的一个小修改。我们像这样包含测试代码:evaluationDependsOn(':modules:modulename') compileTestJava.dependsOn tasks.getByPath(':modules:modulename:testClasses')
  • 你在使用idea gradle插件吗?您可能需要在项目的 iml 文件中添加一个重写挂钩,以将依赖项添加到其他项目的测试输出中。有关此类钩子的一些简短示例,请参阅here。我们在这里将它与 eclipse 插件一起使用,它的工作原理非常相似......
  • 你看过this的问题吗。这应该是你需要做的差不多了......

标签: java testing intellij-idea gradle module


【解决方案1】:

按照this 方法,我能够创建对另一个项目的测试类的依赖项。

rootproject/settings.gradle:

rootProject.name = 'rootproject'
include 'module1'
include 'module2'

rootproject/build.gradle:

task wrapper(type: Wrapper) { gradleVersion = "2.14" }


allprojects {
    repositories {
        mavenCentral()
    }
}


subprojects {
    apply plugin: 'java'
    apply plugin: 'idea'

    sourceCompatibility = 1.8

    dependencies {
        testCompile group: 'junit', name: 'junit', version: '4.11'
    }
}

rootproject/module1/build.gradle:

configurations {
    testArtifacts.extendsFrom testRuntime
}
task testJar(type: Jar) {
    classifier "test"
    from sourceSets.test.output
}
artifacts {
    testArtifacts testJar
}

rootproject/module2/build.gradle

dependencies {
    testCompile project (path: ':module1', configuration: 'testArtifacts')
}

使用 IntelliJ 的 gradle 向导导入根项目后,一切正常。


更新:

  • 添加了 rootproject 的 settings.gradle 文件

【讨论】:

  • 对于使用 Java 库插件的 Gradle 4+,将 testRuntime 替换为 testRuntimeOnly 并将 sourceSets.test.output 替换为 sourceSets.test.output.classesDirs。另外,如果你只需要测试源,而不关心传递依赖,extendsFrom testImplementation 是更好的选择。
猜你喜欢
  • 2018-03-06
  • 2012-09-04
  • 1970-01-01
  • 1970-01-01
  • 2022-01-02
  • 2017-12-17
  • 1970-01-01
  • 2018-09-17
  • 2016-12-17
相关资源
最近更新 更多