How to run gradle script from gradle

How to run another gradle script from gradle. I have several gradle scripts to run tests in the <root_project>/test directory. <root_project>/build.gradle is called with the name of the gradle test file to run. for instance

gradle run_test -PtestFile=foobar

This should be done by <root_project>/test/foobar.gradle (its default tasks)

What I am doing now calls project.exec in the run_test task. This works, but now I need to pass the project properties from the gradle process that runs run_test to the one that 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 necessary information to the child gradle script?

+5
source share
3 answers

I do something like this when the "build" task in one gradle is sent to the "execute" task in another gradle file in the directory it created (called an artifact) and goes through all the project properties too.

Here is the appropriate code for 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() 
+4
source

Pretty weird requirement. Could you provide additional information? Maybe there is another, easier way to do this.

However, this can be done using the Gradle API tools . You can find the API docs here , and ProjectConnection is the best place to start.

+1
source

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

 gradle --build-file anotherBuild.gradle taskName 

or

 gradle -b anotherBuild.gradle taskName 
+1
source

Source: https://habr.com/ru/post/1205069/


All Articles