Jar manifest classpath to include another jar from the same script

I have a script that creates two jars as below:

jar {
  exclude "**/common/**"
  manifest {
      'Class-Path':       <- Need name of commonJar here
 }
}

task commonJar(type: Jar) { 
  from sourceSets.main.output 

include '**/common/**'
}

I need the name of the commonJar in the manifest classpath of the other jar. How would I accomplish that.

I haven’t actually tested this code, but give this a go (based on https://docs.gradle.org/current/userguide/building_java_projects.html#sec:jar_manifest):

task commonJar(type: Jar) { 
    from sourceSets.main.output 
    include '**/common/**'
}

jar {
    exclude "**/common/**"
    manifest {
        attributes( 'Class-Path': tasks.commonJar.archiveFileName )
    }
}

The docs for Manifest.attributes says “The values can be any object. For evaluating the value objects their toString() method is used.” However, the implementation also handles objects of type Provider and calls their get() method instead. The archiveFileName property is a Provider.

That does not work. I get the following error:

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

Sorry, I missed your reply.

Did you re-order things so that the commonJar task is created before referencing it in the jar task’s configuration like in my example?