7

我有一个 gradle 构建脚本,其中包含一些源集,它们都定义了各种依赖项(一些常见,一些没有),我正在尝试使用 Eclipse 插件让 Gradle 为 Eclipse 生成.project.classpath文件,但我不能弄清楚如何将所有依赖项条目放入.classpath;出于某种原因,实际上很少添加外部依赖项.classpath,因此 Eclipse 构建失败并出现 1400 个错误(使用 gradle 构建工作正常)。

我已经像这样定义了我的源集:

sourceSets {
    setOne
    setTwo {
        compileClasspath += setOne.runtimeClasspath
    }
    test {
        compileClasspath += setOne.runtimeClasspath
        compileClasspath += setTwo.runtimeClasspath
    }
}

dependencies {
    setOne 'external:dependency:1.0'
    setTwo 'other:dependency:2.0'
}

由于我没有使用main源集,我认为这可能与它有关,所以我添加了

sourceSets.each { ss ->
    sourceSets.main {
        compileClasspath += ss.runtimeClasspath
    }
}

但这没有帮助。

我无法弄清楚包含的库的任何共同属性,或者那些不包含的库的任何共同属性,但我找不到任何我确定的东西(尽管当然必须有一些东西)。我有一种感觉,所有包含的库都是test源集的依赖项,无论是直接的还是间接的,但我无法验证这一点,而不仅仅是注意到所有test的依赖项都存在。

如何确保放入所有.classpath源集的依赖项?

4

3 回答 3

3

这个问题的解决方式与我昨天提出的一个类似问题密切相关:

// Create a list of all the configuration names for my source sets
def ssConfigNames = sourceSets.findAll { ss -> ss.name != "main" }.collect { ss -> "${ss.name}Compile".toString() }
// Find configurations matching those of my source sets
configurations.findAll { conf -> "${conf.name}".toString() in ssConfigNames }.each { conf ->
    // Add matching configurations to Eclipse classpath
    eclipse.classpath {
        plusConfigurations += conf
    }
}

更新:

我也在Gradle 论坛中问过同样的问题,并得到了更好的解决方案:

eclipseClasspath.plusConfigurations = configurations.findAll { it.name.endsWith("Runtime") }

它没有那么精确,因为它添加了其他东西,而不仅仅是我的源集中的东西,但它保证它会起作用。而且眼睛更容易=)

于 2013-06-12T11:08:38.947 回答
2

我同意 Tomas Lycken 的观点,最好使用第二个选项,但可能需要稍作修正:

eclipse.classpath.plusConfigurations = configurations.findAll { it.name.endsWith("Runtime") }

于 2014-07-29T12:04:59.683 回答
1

这就是 Gradle 2.2.1 对我有用的:

eclipse.classpath.plusConfigurations = [configurations.compile]
于 2014-12-13T17:21:16.523 回答