Add classpath in manifest using Gradle

前端 未结 9 1619
醉话见心
醉话见心 2020-12-01 02:51

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         


        
相关标签:
9条回答
  • 2020-12-01 03:29

    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

    0 讨论(0)
  • 2020-12-01 03:29

    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
    }
    
    0 讨论(0)
  • 2020-12-01 03:29

    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

    0 讨论(0)
提交回复
热议问题