用Gradle建立一个uberjar

mis*_*tor 36 java groovy uberjar gradle

我是Gradle新手.我想构建一个uberjar(AKA fatjar),它包含项目的所有传递依赖项.我需要在"build.gradle"中添加哪些行?

这就是我现在所拥有的:(我几天前从某处复制过,但不记得从哪里开始.)

task uberjar(type: Jar) {
    from files(sourceSets.main.output.classesDir)

    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
    }
}
Run Code Online (Sandbox Code Playgroud)

mis*_*tor 39

我用task uberjar(..以下内容替换了:

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
    }
}
Run Code Online (Sandbox Code Playgroud)

需要排除,因为在他们缺席的情况下你会遇到这个问题.

  • 我认为应该使用`configurations.runtime`来包含所有运行时依赖项 (5认同)

tim*_*tes 33

你有没有在gradle食谱中尝试过这个胖子的例子?

你正在寻找的是gradle 的影子插件


Bao*_* Le 7

只需将其添加到java模块的build.gradle即可.

mainClassName ="my.main.Class"

jar {
  manifest { 
    attributes "Main-Class": "$mainClassName"
  }  

  from {
    configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
  }
}
Run Code Online (Sandbox Code Playgroud)

这将导致[module_name]/build/libs/[module_name] .jar文件.


boe*_*107 5

我发现这个项目非常有用.使用它作为参考,我的Gradle uberjar任务将是

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'
    }
}
Run Code Online (Sandbox Code Playgroud)