How to reuse a build script

Is it possible to have separate gradle project to configure testing? E.g.

dependencies {
 testCompile 'org.uncommons:reportng:1.1.3'
 testCompile 'org.testng:testng:6.3.1'
}
  configurations {
 compile
 runtime {
        extendsFrom compile
    }
 }
  test {
 useTestNG()
 systemProperties "org.uncommons.reportng.stylesheet": "${projectDir}/resources/hudsonesque.css"
 options {
   listeners << 'org.uncommons.reportng.HTMLReporter'
   listeners << 'org.uncommons.reportng.JUnitXMLReporter'
 }
}

This will be in a separate file and each project refers this.

You wouldn’t make this a separate Gradle project but a separate Gradle build script. Save this file as, say, “testing.gradle” and then apply it from other build scripts with apply from: “testing.gradle”. By convention, such build scripts go into $rootProjectDir/gradle.

I want to use the same file in multiple projects. If I make it a project can I still have the same effect.

Not sure what you mean by that. What I was trying to say is that above build script should go into $rootProject.projectDir/gradle/testing.gradle. You can then use “apply from” in as many build scripts as you like.

How do I configure $rootProjectDir? Can I have this in another directory also?

You can have your build scripts in any directory. If you want to change a project directory, you can do so in settings.gradle. For example: project(":foo").projectDir = new File(“bar”)