Sundeep Gupta
Sundeep Gupta

Reputation: 1986

How to run gradle script from gradle

How can I run another gradle script from gradle. I have multiple gradle scripts to run tests under <root_project>/test directory. The <root_project>/build.gradle is invoked with the name of the test gradle file to be run. For example

gradle run_test -PtestFile=foobar

This should run <root_project>/test/foobar.gradle (default tasks of it)

What I'm currently doing is invoking project.exec in the run_test task. This works but now I need to pass the project properties from the gradle process running run_test to the one which runs foobar.gradle

How can I do this ? Is there a more gradle-integrated way to run a gradle script from another, passing all the required info to the child gradle script ?

Upvotes: 8

Views: 5532

Answers (2)

Mark Fisher
Mark Fisher

Reputation: 9886

I do something similar where a "construct" task in one gradle hands off to a "perform" task in another gradle file in a dir it has created (called the artefact), and it passes through all the project properties through too.

Here's the relevant code for the handoff:

def gradleTask = "yourTaskToEventuallyRun"
def artefactBuild = project.tasks.create([name: "artefactBuild_$gradleTask", type: GradleBuild])
artefactBuild.buildFile = project.file("${artefactDir}/build.gradle")
artefactBuild.tasks = [gradleTask]

// inject all parameters passed to this build through to artefact build
def artefactProjectProperties = artefactBuild.startParameter.projectProperties
def currentProjectProperties = project.gradle.startParameter.projectProperties
artefactProjectProperties << currentProjectProperties
// you can add more here
// artefactProjectProperties << [someKey: someValue]

artefactBuild.execute()

Upvotes: 5

Edd&#250; Mel&#233;ndez
Edd&#250; Mel&#233;ndez

Reputation: 6549

If you want to specify another gradle script, you can use:

gradle --build-file anotherBuild.gradle taskName

or

gradle -b anotherBuild.gradle taskName

Upvotes: 3

Related Questions