【问题标题】:Custom source set in Gradle imported to IntelliJ 14Gradle 中的自定义源集导入 IntelliJ 14
【发布时间】:2014-12-15 14:16:17
【问题描述】:

我正在尝试让 Gradle (2.1) 和 IntelliJ (14.0.2) 玩得很好。具体来说,我已将一个示例 Gradle 项目导入 IntelliJ,其中包含用于集成测试的单独源集。

在命令行上使用 Gradle 可以很好地构建项目,并且我能够成功运行集成测试。另一方面,在 IntelliJ 中运行时,我有两个问题:

1) 在 IntelliJ 中编译失败,因为集成测试中依赖于无法解析的第三方库(commons-collections)。

2) 如果我删除上面的依赖项并进行编译,我将无法在 IntelliJ 中运行集成测试。我收到以下错误消息:

没有找到给定的测试包括:[org.gradle.PersonIntegrationTest.canConstructAPersonWithAName]

文件结构如下:

src
  integration-test
    java
    resources
  main
    java
    resources
  test
    java
    resources
build.gradle

还有 build.gradle:

apply plugin: 'java'

repositories {
    mavenCentral()
}

sourceSets {
    integrationTest {
        java.srcDir file('src/integration-test/java')
        resources.srcDir file('src/integration-test/resources')
    }
}

dependencies {
    testCompile 'junit:junit:4.11'
    integrationTestCompile 'commons-collections:commons-collections:3.2'
    integrationTestCompile sourceSets.main.output
    integrationTestCompile configurations.testCompile
    integrationTestCompile sourceSets.test.output
    integrationTestRuntime configurations.testRuntime
}

task integrationTest(type: Test, dependsOn: jar) {
    testClassesDir = sourceSets.integrationTest.output.classesDir
    classpath = sourceSets.integrationTest.runtimeClasspath
    systemProperties['jar.path'] = jar.archivePath
}

check.dependsOn integrationTest

任何关于如何完成这项工作的想法将不胜感激。

完整的 Gradle 示例项目可在 Gradle 发行版中找到,位于 samples/java/withIntegrationTests

【问题讨论】:

    标签: intellij-idea gradle intellij-14


    【解决方案1】:

    您需要告诉 IDEA 将您的 integrationTest 配置中的条目作为 TEST 依赖项映射到您的项目中。我不确定您是否也需要添加源根目录。重要的部分是:

    idea {
      module {
        //and some extra test source dirs
        testSourceDirs += file('some-extra-test-dir')
        generatedSourceDirs += file('some-extra-source-folder')
        scopes.TEST.plus += [ configurations.integrationTest ]
      }
    }
    

    更多内容在http://www.gradle.org/docs/current/dsl/org.gradle.plugins.ide.idea.model.IdeaModule.html中描述


    编辑以反映 Daniel 的 cmets:generatedSourceDirs 是 Gradle 2.2+。 要设置测试任务,您将使用类似

    的任务
    task integTest(type: Test) {
        description = 'Runs the integration tests.'
        group = 'verification'
        testClassesDir = sourceSets.integTest.output.classesDir
        classpath = sourceSets.integTest.runtimeClasspath
        reports.junitXml.destination = file("${project.testResultsDir}/$name")
        reports.html.destination = file("${project.reporting.baseDir}/$name")
        shouldRunAfter test
    }
    check.dependsOn integTest
    

    【讨论】:

    • 谢谢,但我无法让它工作。我收到以下错误:在 org.gradle.plugins.ide.idea.model.IdeaModule_Decorated 上找不到属性“generatedSourceDirs”。另外,我是否需要添加一个 integrationTest 配置才能使其正常工作。
    • 我之前评论中的最后一句话应该以问号结尾,也就是说,我是否需要添加一个“integrationTest”配置才能使其正常工作?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-21
    • 1970-01-01
    • 2016-08-30
    相关资源
    最近更新 更多