Referencing JAR configuration in subproject from root project

In simple, single project builds, I use the GitHub Release plugin to publish artifacts to releases in GitHub using the following closure:

githubRelease {
   token <token>
   owner <owner>
   repo rootProject.name
   overwrite true
   generateReleaseNotes true
   releaseAssets jar.destinationDirectory.files()
}

I now have a multi-project with artifacts produced by many of the subprojects, and I want to dynamically collect references to all of those artifacts for the releaseAssets property, but I’m not able to figure out how to reference those files from the root project.

Could not get unknown property 'jar'

I’m sure it’s something simple, but I haven’t been able to find it yet. I appreciate the help.

What you probably do is someSubProject.jar....
What you probably mean is someSubProject.tasks.jar....
But you should never do that.
Find more information and proper ways here: Sharing outputs between projects

Thanks for the answer Bjorn. I tried the following:

releaseAssets project(':plugin').tasks.jar.destinationDirectory.files()

I received the following error:

Could not get unknown property 'jar' for task set of type org.gradle.api.internal.tasks.DefaultTaskContainer

Notice, that :plugin:jar is indeed a JAR task:

❯ gradle help --task :plugin:jar

> Task :help
Detailed task information for :plugin:jar

Path
     :plugin:jar

Type
     Jar (org.gradle.api.tasks.bundling.Jar)

Description
     Assembles a jar archive containing the main classes.

Group
     build

BUILD SUCCESSFUL in 994ms
1 actionable task: 1 executed

Some points:

  1. It is Björn or Bjoern, but not Bjorn
  2. I guess you try to access the task before it is actually registered as the plugin project is probably configured later. You could maybe mitigate this using evaluationDependsOn, but you really shouldn’t.
  3. You shouldn’t because what you do is inherently broken and unsafe and I already told you in my previous answer explicitly to never do that and also where you find more information about how to do it properly.

I’ve updated the post to be more clear with my use case.

I understand how to use a producer for declaring dependencies between projects, but that is not what I’m doing here. I’m trying to pass a JAR artifact from a subproject directly to an extension of a plugin, specifically, the releaseAssets property, which accepts a FileCollection. Your solution may be able to assist me to do this safely, but I don’t understand how.

I’ve updated the post to be more clear with my use case.

That’s actually not too nice.
Now my answers read as if I didn’t properly read your question. :-/

I understand how to use a producer for declaring dependencies between projects, but that is not what I’m doing here.

Of course, this is exactly what you are doing here.
Actually it is easier than what is shown in the linked docs, as you want the jars, which are declared as outgoing variants already.
So yes, that is exactly what you want.
You want a configuration which depends on the subprojects and when you resolve the configuration, you get the jars.
So something like

configurations {
    foo {
        canBeConsumed = false
        canBeResolved = true
        hidden = true
    }
}
dependencies {
    subprojects {
        foo(it)
    }
}
githubRelease {
   [...]
   releaseAssets configurations.foo
}

should probably do.
But this is untested, just written from mind, could need some fixing.

This worked. Thank you.

1 Like