简体   繁体   English

如何在我的 gradle 项目中创建单个可执行文件 jar

[英]How can I create single executable jar in my gradle project

I am using gradle 6.2 version with jdk 8 in my application, it's builing 2 jar files under build/libs/ directory.我在我的应用程序中使用 gradle 6.2 版本和 jdk 8,它在 build/libs/ 目录下构建了 2 个 jar 文件。 How can I create the single executable jar in my application?如何在我的应用程序中创建单个可执行文件 jar?

My build.gradle,我的build.gradle,

plugins {
  id 'com.google.cloud.tools.jib' version '2.2.0'
  id 'application'
  id 'java'
}

repositories {
    mavenLocal()
    mavenCentral()
    jcenter()
}

jib.to.image = 'gcr.io/' + project.property('gcpProjectId') + '/my-application'
jib.container.creationTime = 'USE_CURRENT_TIMESTAMP'

mainClassName = "com.foo.Myapplication"

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

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

Created Jar files:创建了 Jar 个文件:

ls -ltr build/libs/
total 31184
-rw-r--r-- 1  user 15986229 Jun  9 20:22 my-application-1.0.0.jar
-rw-r--r-- 1  user 15942345 Jun  9 20:22 my-application-1.0.0-all.jar

Why it's creating -all.jar while executing "gradle clean build"?为什么它在执行“gradle clean build”时创建 -all.jar? What did am wrong here?这里出了什么问题? Kindly provide your thoughts on this.请提供您对此的想法。

Gradle Shadow Plugin seems to be exactly what you need. Gradle Shadow Plugin似乎正是您所需要的。 It has interop with Gradle's application plugin as well .它也可以与 Gradle 的application插件互操作

I am able to build a single jar, after modifying the build.gradle likes the following,我能够构建单个 jar,在修改 build.gradle 后,如下所示,

build.gradle, build.gradle,

plugins {
  id 'com.google.cloud.tools.jib' version '2.2.0'
}

repositories {
    mavenLocal()
    mavenCentral()
    jcenter()
}

jib.to.image = 'gcr.io/' + project.property('gcpProjectId') + '/my-application'
jib.container.creationTime = 'USE_CURRENT_TIMESTAMP'

jar {
  manifest { 
    attributes 'Main-Class': 'com.foo.Myapplication'
  }  

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Gradle 为 Selenium TestNG 项目创建具有依赖项的可执行 JAR? - How can I create an executable JAR with dependencies using Gradle for a Selenium TestNG project? 如何使用 Gradle 和实现依赖项创建可执行的胖 JAR? - How do I create an executable fat JAR with Gradle with implementation dependencies? 如何增加应用程序可执行jar文件中的JVM堆大小? 项目类型为Maven Project - How can I increase JVM heap size in my application executable jar file? The project type is Maven Project 如何从我的intellij项目中创建可执行的JAR文件? - How do I create an executable JAR file from my intellij project? 如何使用 Gradle 从可执行 Jar 文件中创建可执行文件? - How To Create An Executable Out Of A Executable Jar File Using Gradle? 我如何从我的java .jar文件创建可执行的apple .app文件? - How can i create executable apple .app file from my java .jar file? 如何使用 Maven 为我的 webapp 创建可执行 jar? - How do I create an executable jar for my webapp with Maven? 如何使用Gradle在我的JAR中包含单个依赖项? - How do I include a single dependency in my JAR with Gradle? 如何将Gradle项目编译为可以在Android应用程序中使用的jar文件 - How to compile gradle project to a jar file I can use in my Android application 如何创建Lagom项目的可执行jar - How to create an executable jar of a Lagom project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM