有没有一种方法可以指定Gradle中新创建的源代码集的依赖关系?

问题描述:

在gradle这个我已经创造了这样的服务测试的新sourceSet有没有一种方法可以指定Gradle中新创建的源代码集的依赖关系?

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

此源集依赖于TestNG的,所以我希望通过做一些像拉依赖性下降:

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

不幸的是,这会返回一个错误。有什么办法可以声明一个具体的sourceSet的依赖关系,还是我运气不好?

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

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

configurations { 
    serviceTestCompile 
} 

sourceSets { 
    serviceTest { 
     compileClasspath = configurations.serviceTestCompile 
    } 
} 

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

很高兴知道,但不幸的是,我现在连接到gradle 0.9.2 – AgentRegEdit 2012-01-09 17:06:18

+0

当你说最近,至于什么版本?这不适合Gradle 1.4。 – 2013-05-23 16:06:10

以下适用于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 
}