简体   繁体   中英

Is there a way to specify dependencies for a newly created sourceset in gradle?

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?

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"
}

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM