Add classpath in manifest using Gradle

I would like my Gradle build script to add the complete Classpath to the manifest file contained in JAR file created after the build.

Example:

Manifest-Version: 1.0
Class-Path: MyProject.jar SomeLibrary.jar AnotherLib.jar

My build script already add some information to the manifest this way:

jar {
    manifest {
        attributes("Implementation-Title": project.name,
            "Implementation-Version": version,
            "Main-Class": mainClassName,
    }
}

How do I get the list of dependencies to add to the manifest?


This page of Java tutorials describes more in detail how and why adding classpath to the manifest: Adding Classes to the JAR File's Classpath


Found a solution on Gradle's forum:

jar {
  manifest {
    attributes(
      "Class-Path": configurations.compile.collect { it.getName() }.join(' '))
  }
}

Source: Manifest with Classpath in Jar Task for Subprojects


In the latest versions of gradle, compile and runtime becomes deprecated. Instead, use runtimeClasspath as follows:

'Class-Path': configurations.runtimeClasspath.files.collect { it.getName() }.join(' ')

EDIT:

Note that if you are using Kotlin DSL, you can configure the manifest as follows:

configure<JavaPluginConvention> {
    sourceCompatibility = JavaVersion.VERSION_1_8
    targetCompatibility = JavaVersion.VERSION_1_8
    manifest {
        attributes(
                "Manifest-Version" to "1.0",
                "Main-Class" to "io.fouad.AppLauncher")
    }
}

tasks.withType(Jar::class) {
    manifest {
        attributes["Manifest-Version"] = "1.0"
        attributes["Main-Class"] = "io.fouad.AppLauncher"
    }
}