calling gradle "build" task in another project

26,094

Solution 1

First read this: http://gradle.org/docs/current/userguide/multi_project_builds.html

If you have a multi-project build You need a root project that contains settings.gradle file with something like:

include 'myproject1'
include 'myproject2'

Than you can just make a dependency from one project to another like this:

myproject1/gradle.build

task someOtherTask() << {
   println 'Hello'
}

myproject2/gradle.build

task sometask(dependsOn: ':myproject1:someOtherTask') << {
  //do something here
}

Or if you want to call a task:

project(':myproject1').tasks.build.execute()

Notice: You have to apply java plugin to make build task available.

Solution 2

Adjust the buildFile path, etc.:

task buildSomethingElse(type: GradleBuild) {
  buildFile = '../someOtherDirectory/build.gradle'
  tasks = ['build']
}

build.finalizedBy('buildsomethingElse')

Reference: Gradle organizing build logic guide, paragraph 59.5.

You can apply this to another ant project as well, by adding a one line build.gradle file in your ant project that just calls ant, like so:

ant.importBuild 'build.xml'
Share:
26,094

Related videos on Youtube

hamad32
Author by

hamad32

Updated on June 29, 2020

Comments

  • hamad32
    hamad32 almost 4 years

    I am only a couple of days into using gradle

    In my current build.gradle script I have a task which I would like to call the build task in another project (ie. defined in a different build.gradle somewhere else) after each time it is executed

    My question is how do I call a task from another project?

    I guess I want to do something like tasks.build.execute() but it doesn't seem to work. I tried this:

    commandLine "${rootDir}\\gradle", 'build', 'eclipse'
    

    it at least executed the build and eclipse for my current project just not the master project. Hope my question is clear

    • hamad32
      hamad32 about 9 years
      I was able to reference the task in the root project but when I do this rootProject.tasks.build.execute() it doesn't seem to execute properly
    • Opal
      Opal about 9 years
      What doesn't work exactly?
  • Steven Spungin
    Steven Spungin almost 6 years
    This is much easier than creating a multi-project build, and in some cases more appropriate.
  • Jeff Holt
    Jeff Holt about 5 years
    Until I read this answer, I had thought using more than one build script in a single directory during the execution of a single gradle command was not possible. This seems to be a workaround for that apparent constraint.