Add classpath in manifest using Gradle

前端 未结 9 1648
醉话见心
醉话见心 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:16

    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.

提交回复
热议问题