How to refactor common configuration code?


(Noel Yap) #1

I have the following code:

subprojects {

if (project.name == ‘integrationTest’) {

sourceSets {

integrationTest {

java.srcDir file(projectDir)

}

}

idea {

module {

testSourceDirs += file(‘java’)

scopes.TEST.plus += configurations.integrationTestCompile

}

}

task integrationTest(type: Test) {

forkEvery = 1

testClassesDir = sourceSets.integrationTest.output.classesDir

classpath = sourceSets.integrationTest.runtimeClasspath

systemProperties[‘jar.path’] = jar.archivePath

systemProperty ‘com.netflix.cryptexclient.sslCertsRootLoc’, project(’:cryptexclient’).projectDir.toString()

}

} else {

try {

integrationTestProject = project("${project.path}/integrationTest")

task integrationTest(type: Test, dependsOn: integrationTestProject.getTasksByName(“integrationTest”, false))

tasks.publish.dependsOn(‘integrationTest’)

} catch (UnknownProjectException e) {

// ignore exception since it’s not really a problem

}

}

if (project.name == ‘performanceTest’) {

sourceSets {

performanceTest {

java.srcDir file(projectDir)

}

}

idea {

module {

testSourceDirs += file(‘java’)

scopes.TEST.plus += configurations.performanceTestCompile

}

}

task performanceTest(type: Test) {

forkEvery = 1

testClassesDir = sourceSets.performanceTest.output.classesDir

classpath = sourceSets.performanceTest.runtimeClasspath

systemProperties[‘jar.path’] = jar.archivePath

systemProperty ‘com.netflix.cryptexclient.sslCertsRootLoc’, project(’:cryptexclient’).projectDir.toString()

}

} else {

try {

performanceTestProject = project("${project.path}/performanceTest")

task performanceTest(type: Test, dependsOn: performanceTestProject.getTasksByName(“performanceTest”, false))

tasks.publish.dependsOn(‘performanceTest’)

} catch (UnknownProjectException e) {

// ignore exception since it’s not really a problem

}

}

}


(Peter Niederwieser) #2

Either put the code in the respective subprojects, or abstract over it like you would in your production code. This might work better if you used Gradle’s Java APIs rather than its DSL, e.g. ‘def testTaskName = “integrationTest”; sourceSets.create(testTaskName)’ instead of ‘sourceSets { integrationTest }’.


(Justin Ryan) #3

Noel, you might want to look at the Facet plugin in nebula-project-plugin: https://github.com/nebula-plugins/nebula-project-plugin. It’ll create those integrationTest sourcesSets, confs and test tasks for you really easily.

Concerning the refactoring, I’d have a block like this:

def itProject = subprojects.matching { it.name == 'integrationTest' }.first()
itProject.plugins.apply 'nebula-integtest'
  def otherProjects = subprojects.matching { it.name != 'integrationTest' }
otherProjects.all {
    task integrationTest(type: Test) {
        dependsOn: itProject.tasks.integrationTest
    }
    tasks.publish.dependsOn('integrationTest')
 }

Though I’m not a huge fan of this chaining of tasks. If you have a binary dependency on a “integrationTest” than add a dependency.