Java 如何使用具有实现依赖项的 Gradle 创建可执行的胖 jar

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49278063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 02:58:56  来源:igfitidea点击:

How do I create an executable fat jar with Gradle with implementation dependencies

javagradleexecutable-jargradle-pluginfatjar

提问by Dmitry Senkovich

I've got a simple project in Gradle 4.6 and would like to make an executable jar of it. I've tried shadow, gradle-fatjar-plugin, gradle-one-jar, spring-boot-gradle-pluginplugins but neither of them adds my dependencies declared as implementation(I don't have any compileones). It works with compilee.g. for gradle-one-jarplugin but I would like to have implementationdependencies.

我在 Gradle 4.6 中有一个简单的项目,想制作一个可执行的 jar。我试过shadow, gradle-fatjar-plugin, gradle-one-jar,spring-boot-gradle-plugin插件,但它们都没有添加我声明为的依赖项implementation(我没有任何依赖项compile)。它适用于compile例如gradle-one-jar插件,但我想要implementation依赖。

Thank you very much!

非常感谢!

采纳答案by miskender

You can use the following code.

您可以使用以下代码。

jar {
    manifest {
        attributes(
                'Main-Class': 'com.package.YourClass'
        )
    }
    from {
        configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
    }
 }

Be sure to replace com.package.YourClasswith the fully qualified class name containing static void main( String args[] ).

确保替换com.package.YourClass为包含static void main( String args[] ).

This will pack the runtime dependencies. Check the docsif you need more info.

这将打包运行时依赖项。如果您需要更多信息,请查看文档

回答by SerCe

The same task can be achieved using Gradle Kotlin DSLin a similar way:

可以以类似的方式使用Gradle Kotlin DSL完成相同的任务:

val jar by tasks.getting(Jar::class) {
    manifest {
        attributes["Main-Class"] = "com.package.YourClass"
    }

    from(configurations
        .runtime
        // .get() uncomment this on Gradle 6+
        // .files
        .map { if (it.isDirectory) it else zipTree(it) })
}

回答by JohnP2

Based on the accepted answer, I needed to add one more line of code:

根据接受的答案,我需要再添加一行代码:

task fatJar(type: Jar) {
  manifest {
    attributes 'Main-Class': 'com.yourpackage.Main'
  }
  archiveClassifier = "all"
  from {
    configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
    configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
    }
  with jar
}

Without this additional line, it omitted my source files and only added the dependencies:

如果没有这个额外的行,它省略了我的源文件,只添加了依赖项:

configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }

回答by LiuWenbin_NO.

from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } }

from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } }

This line is essential to me.

这条线对我来说是必不可少的。