【问题标题】:Appending Dependencies from Another Configuration从另一个配置附加依赖项
【发布时间】:2014-08-08 20:33:15
【问题描述】:

我的项目是一个遵循标准约定(src/main、src/test ..)的 Java 项目。我正在尝试编写一个简单的 Gradle 任务,它将发出两个 jar 文件:

  • Gradle 发出的“默认”jar 包含我的应用代码

  • 仅包含运行它们所需的测试类和依赖项的 jar(例如 Junit)。此 jar 不应包含仅非测试代码需要的依赖项(例如下面示例中的 Guava)。

我尝试在下面的 testJar 任务中使用“testCompile”,但这也会选择非测试 Jar。因此,我创建了一个单独的配置,仅列出了测试依赖项,但现在正如我所料,由于未正确填充“testCompile”配置,因此无法编译测试。

我还没有在 DependencyHandler API 中看到将一个配置分配给另一个配置的方法。 如何将 testJars 依赖集添加到 testCompile 集(而不是替换),以便一切“正常”?是,但我可能错了!

apply plugin: 'java'

sourceCompatibility = 1.7
targetCompatibility = 1.7

repositories {
    mavenCentral()
}

configurations {
    testJars
}

dependencies {
    compile group: 'com.google.guava', name: 'guava', version: '14.0.1'
    testJars group: 'junit', name: 'junit', version: '4.+'
    //"testCompile append testJars"??
}

task testJar(type: Jar) {
    classifier = 'tests'
    from sourceSets.test.output
    from { configurations.testJars.collect { it.isDirectory() ? it : zipTree(it) } }
}

【问题讨论】:

  • 不完全确定您要做什么,但将其添加到您的 configurations 块可能会完成它:testCompile { extendsFrom testJars }

标签: java gradle


【解决方案1】:

假设自定义 testJars 配置是内置 testCompile 配置的子集,您应该能够将您的 configurations 块更改为:

configurations {
    testJars
    testCompile {
        extendsFrom testJars
    }
}

这将增加testCompile 以包含testJars 拥有的所有内容。我认为这将为您提供编译测试代码所需的类路径。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多