Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 454
AgentRegEdit Avatar asked Jan 09 '12 16:01

AgentRegEdit


2 Answers

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"
}
like image 170
Peter Niederwieser Avatar answered Oct 13 '22 05:10

Peter Niederwieser


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
}
like image 23
Mike Rylander Avatar answered Oct 13 '22 05:10

Mike Rylander