Building a uberjar with Gradle

后端 未结 4 2027
耶瑟儿~
耶瑟儿~ 2020-11-27 04:28

I am a Gradle novice. I want to build a uberjar (AKA fatjar) that includes all the transitive dependencies of the project. What lines do I need to add to my \"build.gradle\"

相关标签:
4条回答
  • 2020-11-27 05:12

    Have you tried the fatjar example in the gradle cookbook?

    What you're looking for is the shadow plugin for gradle

    0 讨论(0)
  • 2020-11-27 05:16

    I found this project very useful. Using it as a reference, my Gradle uberjar task would be

    task uberjar(type: Jar, dependsOn: [':compileJava', ':processResources']) {
        from files(sourceSets.main.output.classesDir)
        from configurations.runtime.asFileTree.files.collect { zipTree(it) }
    
        manifest {
            attributes 'Main-Class': 'SomeClass'
        }
    }
    
    0 讨论(0)
  • 2020-11-27 05:20

    I replaced the task uberjar(.. with the following:

    jar {
        from(configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }) {
            exclude "META-INF/*.SF"
            exclude "META-INF/*.DSA"
            exclude "META-INF/*.RSA"
        }
    
        manifest {
            attributes 'Implementation-Title': 'Foobar',
                    'Implementation-Version': version,
                    'Built-By': System.getProperty('user.name'),
                    'Built-Date': new Date(),
                    'Built-JDK': System.getProperty('java.version'),
                    'Main-Class': mainClassName
        }
    }
    

    The exclusions are needed because in their absence you will hit this issue.

    0 讨论(0)
  • 2020-11-27 05:24

    Simply add this to your java module's build.gradle.

    mainClassName = "my.main.Class"

    jar {
      manifest { 
        attributes "Main-Class": "$mainClassName"
      }  
    
      from {
        configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
      }
    }
    

    This will result in [module_name]/build/libs/[module_name].jar file.

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