有没有办法为 gradle 中新创建的源集指定依赖项?

Age*_*dit 5 java dependency-management gradle

在 gradle 中,我sourceSet为这样的服务测试创建了一个新的:

sourceSets{
    servicetest{
        java.srcDirs = [//path to servicetests]
    }
}
Run Code Online (Sandbox Code Playgroud)

此源集取决于 TestNG,因此我希望通过执行以下操作来降低依赖性:

dependencies{
    servicetest(group: 'org.testng', name: 'testng', version: '5.8', classifier: 'jdk15')
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,这会返回一个错误。有什么方法可以声明特定的依赖项sourceSet还是我不走运?

Pet*_*ser 8

最近的 Gradle 版本会为每个源集 'foo' 自动创建和连接配置 'fooCompile' 和 'fooRuntime'。

如果您仍在使用旧版本,则可以声明自己的配置并将其添加到源集的 compileClasspath 或 runtimeClasspath。就像是:

configurations {
    serviceTestCompile
}

sourceSets {
    serviceTest {
        compileClasspath = configurations.serviceTestCompile
    }
}

dependencies {
    serviceTestCompile "org.testng:testng:5.8"
}
Run Code Online (Sandbox Code Playgroud)

  • 当你说最近的时候,从什么版本开始?这不适用于 Gradle 1.4。 (2认同)