How to use Gradle task output from included build

650 Views Asked by At

So I need to copy the outputs of a couple projects in a Gradle composite project to a local directory. Normally, in a single build, it would go like this:

tasks{
  register<Copy>("copyJarToLocalServer"){
    from(jar) // autowires to jar.outputs.files , the outputs of the jar task
    into("some\\dir")
  }
}

Now I need to do the same thing from a composite build; I used includeBuild to include some projects. The problem is that I cannot find a way to grab the output from the 'jar' tasks from those included projects. This unanswered question seems to have a similar problem.

Things I tried

I tried getting the task from gradle.includedBuild:

register<Copy>("copyJarToLocalServer") {
  from(gradle.includedBuild("witheronia-maze").task("jar")) // witheronia-maze is one of the included builds
  into("some\\dir")
}

This doesn't work because includedBuild().task() returns a TaskReference, not the actual task.

I tried getting all the tasks that have that name recursively:

register<Copy>("copyJarToLocalServer") {
  from(getTasksByName(":jar", true).flatMap { it.outputs.files }) // Also tried "jar", ":witheronia-maze:jar" etc.
  into("some\\folder")
}

This gives the task output NO-SOURCE and doesn't do anything

I tried getting the project and then getting the task

from(project("witheronia-maze").getTasksByName("jar", false).flatMap { it.outputs.files })
// or
from(project("witheronia-maze").tasks.findByPath("jar"))

This tells me the project was not found in root project, also tried "../witheronia-maze" (actual path), ":witheronia-maze" ( I think an included build is not actually considered a project )

Can someone point me in the right direction? Thanks!

1

There are 1 best solutions below

2
TheGates On

The problem here is that I used includeBuild to include the other project's build. This does not add it as a project, and you can't access it's tasks. I solved this by using include instead and setting the project path.

include(":witheronia-maze")
project(":witheronia-maze").projectDir = file("../witheronia-maze")
// Without setting the path manually it would put the project inside the composite, which I don't want

The final Copy task now looks like this:

register<Copy>("copyJarToLocalServer") {
    from(project(":witheronia-maze").tasks.getByPath("jar"))
    into("some\\dir")
}

EDIT: The above is not the solution. include does make the tasks resolve correctly, but dependencies do not get substituted anymore and won't resolve. I now use a task in the dependencies that copies the required artifacts to a folder in /build:

register<Copy>("pluginJar"){
    from(jar)
    into(buildDir.resolve("pluginJar"))
}

And then in the composite build:

register<Copy>("copyJarToLocalServer"){
    from(gradle.includedBuild("witheronia-maze").projectDir.resolve("build/pluginJar/"))
    to("some\\dir")
}