# How to refactor common configuration code?

**URL:** <https://discuss.gradle.org/t/how-to-refactor-common-configuration-code/5050>\
**Category:** Old Forum Archive\
**Created:** [June 17, 2014, 10:31pm UTC](https://discuss.gradle.org/t/how-to-refactor-common-configuration-code/5050 "2014-06-17T22:31:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![noel\_yap](https://avatars.discourse-cdn.com/v4/letter/n/b77776/32.png) [@noel\_yap](https://discuss.gradle.org/u/noel_yap)\
**Post date:** [June 17, 2014, 10:31pm UTC](https://discuss.gradle.org/t/how-to-refactor-common-configuration-code/5050/1 "2014-06-17T22:31:00Z")

</div>

I have the following code:

subprojects {

if ([project.name](http://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](http://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

}

}

}

---

<div class="post-metadata">

**Author:** ![Peter\_Niederwieser](https://avatars.discourse-cdn.com/v4/letter/p/9f8e36/32.png) [@Peter\_Niederwieser](https://discuss.gradle.org/u/Peter_Niederwieser)\
**Post date:** [June 18, 2014, 12:37am UTC](https://discuss.gradle.org/t/how-to-refactor-common-configuration-code/5050/2 "2014-06-18T00:37:00Z")

</div>

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 }’.

---

<div class="post-metadata">

**Author:** ![quidryan](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/quidryan/32/73_2.png) [@quidryan](https://discuss.gradle.org/u/quidryan)\
**Post date:** [June 19, 2014, 6:33am UTC](https://discuss.gradle.org/t/how-to-refactor-common-configuration-code/5050/3 "2014-06-19T06:33:00Z")

</div>

Noel, you might want to look at the Facet plugin in nebula-project-plugin: [https://github.com/nebula-plugins/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:

```gradle
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.
