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:
Manife
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
I managed to create a custom Jar file with its own manifest file like so:
task createJar(type : Jar) {
manifest {
attributes(
'Manifest-Version': "1.0",
'Main-Class': "org.springframework.boot.loader.JarLauncher",
'Start-Class': "com.my.app.AppApplication",
'Spring-Boot-Version': "2.2.4.RELEASE",
'Spring-Boot-Classes': "BOOT-INF/classes/",
'Spring-Boot-Lib': "BOOT-INF/lib/"
)
}
def originDir = file("${buildDir}/unpacked")
def destinationFile = "${buildDir}/repackaged/${project.name}-${version}"
entryCompression ZipEntryCompression.STORED // no compression in case there are files in BOOT-INF/lib
archiveName destinationFile
from originDir
archiveFile
}
If your project has external library dependencies, you could copy the jars to a folder and add the classpath entries in the manifest.
def dependsDir = "${buildDir}/libs/dependencies/"
task copyDependencies(type: Copy) {
from configurations.compile
into "${dependsDir}"
}
task createJar(dependsOn: copyDependencies, type: Jar) {
manifest {
attributes('Main-Class': 'com.example.gradle.App',
'Class-Path': configurations.compile.collect { 'dependencies/' + it.getName() }.join(' ')
)
}
with jar
}
More details can be read here