Gradle创建的新sourceSet在Eclipse中无法显示。

5

我在我的Gradle项目中创建了一个名为“integration-test”的额外源代码集。一切都很顺利,但是Eclipse无法看到为这个源代码集定义的依赖类。

输入图像描述

subprojects {
        apply plugin: 'java'
        apply plugin: 'eclipse'

        repositories {
           mavenCentral()
        }

        sourceSets {
            integrationTest {
                java {
                    compileClasspath += main.output + test.output
                    runtimeClasspath += main.output + test.output
                    srcDir file('src/integration-test/java')
                }
                resources.srcDir file('src/integration-test/resources')
            }
        }

        configurations {
            integrationTestCompile.extendsFrom testCompile
            integrationTestRuntime.extendsFrom testRuntime
        }

        dependencies {
            testCompile 'junit:junit:4.12'
            testCompile 'org.mockito:mockito-all:1.10.19'
            integrationTestCompile 'org.springframework:spring-test:4.1.7.RELEASE'
            compile 'org.springframework:spring-context:4.1.7.RELEASE'
            compile 'org.springframework:spring-core:4.1.7.RELEASE'
        }

        task integrationTest(type: Test) {
            testClassesDir = sourceSets.integrationTest.output.classesDir
            classpath = sourceSets.integrationTest.runtimeClasspath
            outputs.upToDateWhen { false }
        }

        check.dependsOn integrationTest
        integrationTest.mustRunAfter test

        version = '1.0'
    }

当我通过命令“build gradle”构建此项目时,项目已经构建完成,唯一的问题是出现在eclipse上。如果我将依赖项'org.springframework:spring-test:4.1.7.RELEASE'从“integrationTestCompile”更改为“testCompile”,问题就解决了。

2个回答

7
很抱歉回答您的问题有点晚,但我只是找到了一个解决方案,因为我遇到了完全相同的问题。
添加以下内容:
eclipse {
    classpath {
        plusConfigurations.add configurations.integrationTestCompile
        plusConfigurations.add configurations.integrationTestRuntime
    }
}

将gradle文件中的内容修改解决了该问题。希望对您也有同样的作用。

1
在进行操作之前,请不要忘记添加“apply plugin: 'eclipse'”插件。 - fuemf5
1
@fuemf5 你说得完全正确!我假设人们会弄清楚,但是我自己忘了,在阅读自己的帖子时...! - sbraconnier
3
为了使其正常工作,我必须使用plusConfigurations.add configurations.integrationTestCompileClasspath而不是plusConfigurations.add configurations.integrationTestCompile - Dave L.
@sbraconnier 这救了我的一天,谢谢。虽然我很想了解为什么 IDE 需要特殊配置。 - powersource97

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接