Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 545
Paolo Fulgoni Avatar asked Mar 26 '14 11:03

Paolo Fulgoni


4 Answers

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

like image 98
Paolo Fulgoni Avatar answered Oct 13 '22 10:10

Paolo Fulgoni


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"
    }
}
like image 45
Eng.Fouad Avatar answered Oct 13 '22 12:10

Eng.Fouad


Place this at the end of the buid.gradle file. Change the com.example.Main to your own Main class.

jar {
    doFirst {
        manifest {
            if (!configurations.compile.isEmpty()) {
                attributes(
                        'Class-Path': configurations.compile.collect{it.toURI().toString()}.join(' '),
                        'Main-Class': 'com.example.Main')
            }
        }
    }
}
like image 7
fssilva Avatar answered Oct 13 '22 12:10

fssilva


The top answers helped me a lot. Here is what worked for me:

jar {
    manifest {
        attributes "Main-Class": "your.package.classWithMain"
        attributes "Class-Path": configurations.compile.collect { it.absolutePath }.join(" ")
    }
}

So, instead of name, I had to use absolutePath. This may or may not work for you. Some suggest using runtime instead of compile. I used compile because, I have a compile section in dependencies in my build.gradle. So, the jar step picks up dependencies from there. The best thing to do is pick up something that you think will work, do a gradle build, then find the JAR file and expand it to find the META-INF/MANIFEST.MF file. You should be able to see all the directories separated by spaces. If not, you should try something different. Autocomplete feature of your IDE should be helpful in seeing what all methods or fields are available under configurations/compile etc. All this can be done easily in IntelliJ.

Oh.. and if you want to see where the library JARs are physically located on your disk, right click on your project->open module settings->Libraries and then click on any library.

like image 6
Vikas Avatar answered Oct 13 '22 11:10

Vikas