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

发布于 2024-12-25 12:09:50 字数 416 浏览 1 评论 0原文

在 gradle 中,我为服务测试创建了一个新的 sourceSet ,如下所示:

sourceSets{
    servicetest{
        java.srcDirs = [//path to servicetests]
    }
}

此源集依赖于 TestNG,因此我希望通过执行以下操作来拉低依赖关系:

dependencies{
    servicetest(group: 'org.testng', name: 'testng', version: '5.8', classifier: 'jdk15')
}

不幸的是,这会返回错误。有没有办法声明特定 sourceSet 的依赖关系,或者我运气不好?

In gradle I have created a new sourceSet for service testing like this:

sourceSets{
    servicetest{
        java.srcDirs = [//path to servicetests]
    }
}

This source set depends on TestNG, so I was hoping to pull the dependency down by doing something like:

dependencies{
    servicetest(group: 'org.testng', name: 'testng', version: '5.8', classifier: 'jdk15')
}

Unfortunately this returns an error. Is there any way to declare a dependency for a specific sourceSet or am I out of luck?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你没皮卡萌 2025-01-01 12:09:50

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

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

configurations {
    serviceTestCompile
}

sourceSets {
    serviceTest {
        compileClasspath = configurations.serviceTestCompile
    }
}

dependencies {
    serviceTestCompile "org.testng:testng:5.8"
}

Recent Gradle versions automatically create and wire configurations 'fooCompile' and 'fooRuntime' for each source set 'foo'.

If you are still using an older version, you can declare your own configuration and add it to the source set's compileClasspath or runtimeClasspath. Something like:

configurations {
    serviceTestCompile
}

sourceSets {
    serviceTest {
        compileClasspath = configurations.serviceTestCompile
    }
}

dependencies {
    serviceTestCompile "org.testng:testng:5.8"
}
千仐 2025-01-01 12:09:50

以下适用于 Gradle 1.4。

apply plugin: 'java'

sourceCompatibility = JavaVersion.VERSION_1_6

sourceSets {
    serviceTest {
        java {
            srcDir 'src/servicetest/java'
        }
        resources {
            srcDir 'src/servicetest/resources'
        }
        compileClasspath += sourceSets.main.runtimeClasspath
    }
}

dependencies {
    compile(group: 'org.springframework', name: 'spring', version: '3.0.7')

    serviceTestCompile(group: 'org.springframework', name: 'spring-test', version: '3.0.7.RELEASE')
    serviceTestCompile(group: 'org.testng', name:'testng', version:'6.8.5')
}


task serviceTest(type: Test) {
    description = "Runs TestNG Service Tests"
    group = "Verification"
    useTestNG()
    testClassesDir = sourceSets.serviceTest.output.classesDir
    classpath += sourceSets.serviceTest.runtimeClasspath
}

The following works for Gradle 1.4.

apply plugin: 'java'

sourceCompatibility = JavaVersion.VERSION_1_6

sourceSets {
    serviceTest {
        java {
            srcDir 'src/servicetest/java'
        }
        resources {
            srcDir 'src/servicetest/resources'
        }
        compileClasspath += sourceSets.main.runtimeClasspath
    }
}

dependencies {
    compile(group: 'org.springframework', name: 'spring', version: '3.0.7')

    serviceTestCompile(group: 'org.springframework', name: 'spring-test', version: '3.0.7.RELEASE')
    serviceTestCompile(group: 'org.testng', name:'testng', version:'6.8.5')
}


task serviceTest(type: Test) {
    description = "Runs TestNG Service Tests"
    group = "Verification"
    useTestNG()
    testClassesDir = sourceSets.serviceTest.output.classesDir
    classpath += sourceSets.serviceTest.runtimeClasspath
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文