gradle打可执行jar,并将依赖jar包打到单独的lib文件夹中

适用于 gradle 4.x ~ 6.8 版本

注意事项: jar配置一定要声明到dependencies后面,不能声明到其前面

repositories {
    mavenLocal()
    maven {
        url "https://maven.aliyun.com/nexus/content/groups/public"
    }
    mavenCentral()
    jcenter()
}

dependencies {
    implementation "org.kohsuke:github-api:$githubApiVersion"
    implementation "org.springframework.boot:spring-boot-starter-actuator"
    implementation "org.springframework.boot:spring-boot-starter-data-jpa"
    implementation "org.springframework.boot:spring-boot-starter-web"
    implementation "org.springframework.boot:spring-boot-starter-jetty"
    implementation "org.springframework.boot:spring-boot-starter-freemarker"
    implementation 'org.springframework.boot:spring-boot-starter-validation'
...
}

// 这里要声明一下开启jar,否则可能会跳过gradle jar的生命周期
jar.enabled = true

task copyDependencies(type: Copy) {
    from configurations.default  // 这里采用default,因为runtime和compile不能把jar包打出来
    into 'build/libs/lib'  // 将jar包打到lib目录下
}
jar.dependsOn(copyDependencies)

jar {
    manifest {
        attributes "Implementation-Title": project.name
        attributes "Implementation-Version": project.version
        attributes 'Main-Class': 'run.halo.app.Application'  // main方法入口
    }
    archiveBaseName = "halo-mini"  // 打包名前缀
	  version = "1.0.0"  // 版本号,此处可不写,会自动取最外层声明的version
	  // 将打出的lib中的jar声明到MANIFEST文件中的ClassPath中,避免打出的jar包找不到依赖包
     if (!configurations.default.files.isEmpty()) {
         manifest.attributes('Class-Path': '. lib/' + configurations.default.files.collect { it.name }.join(' lib/'))
     }
}

打出的jar包中的MANIFEST.MF文件,类似于以下样子

Manifest-Version: 1.0
Implementation-Title: halo
Implementation-Version: 1.0.0
Class-Path: . lib/github-api-1.84.jar lib/spring-boot-starter-actuator
 -2.4.0.jar lib/spring-boot-starter-data-jpa-2.4.0.jar lib/spring-boot
 -starter-web-2.4.0.jar lib/spring-boot-starter-jetty-2.4.0.jar lib/sp
 ring-boot-starter-freemarker-2.4.0.jar lib/spring-boot-starter-valida
 tion-2.4.0.jar lib/jakarta.mail-1.6.5.jar lib/hutool-crypto-5.3.8.jar
  lib/hutool-extra-5.3.8.jar lib/hutool-setting-5.3.8.jar lib/hutool-l
 og-5.3.8.jar lib/hutool-core-5.3.8.jar lib/java-sdk-4.2.0.jar lib/qin
 iu-java-sdk-7.2.29.jar lib/aliyun-sdk-oss-3.9.1.jar lib/bce-java-sdk-
 0.10.36.jar lib/cos_api-5.6.25.jar lib/esdk-obs-java-3.19.7.jar lib/m
 inio-7.1.4.jar lib/springfox-boot-starter-3.0.0.jar lib/commons-fileu
 pload-1.4.jar lib/commons-lang3-3.10.jar lib/httpasyncclient-4.1.4.ja
 r lib/httpclient-4.5.12.jar lib/jackson-dataformat-yaml-2.11.0.jar li
 b/org.eclipse.jgit-5.9.0.202009080501-r.jar lib/annotations-3.0.1u2.j
 ar lib/flexmark-ext-attributes-0.62.2.jar lib/flexmark-ext-autolink-0
 .62.2.jar lib/flexmark-ext-emoji-0.62.2.jar lib/flexmark-ext-escaped-
 character-0.62.2.jar lib/flexmark-jira-converter-0.62.2.jar lib/flexm
 ark-ext-gfm-strikethrough-0.62.2.jar lib/flexmark-ext-gfm-tasklist-0.
 62.2.jar lib/flexmark-ext-ins-0.62.2.jar lib/flexmark-ext-media-tags-
 0.62.2.jar lib/flexmark-ext-tables-0.62.2.jar lib/flexmark-ext-toc-0.
 62.2.jar lib/flexmark-ext-superscript-0.62.2.jar lib/flexmark-ext-yam
 l-front-matter-0.62.2.jar lib/flexmark-ext-gitlab-0.62.2.jar lib/flex
 mark-ext-wikilink-0.62.2.jar lib/flexmark-0.62.2.jar lib/thumbnailato
 r-0.4.11.jar lib/image4j-0.7zensight1.jar lib/flyway-core-6.5.0.jar l
 ib/core-3.4.0.jar lib/leveldb-0.12.jar lib/h2-1.4.197.jar lib/mysql-c
 onnector-java-8.0.22.jar lib/commons-lang-2.6.jar lib/commons-codec-1
 .15.jar lib/spring-boot-actuator-autoconfigure-2.4.0.jar lib/spring-b
 oot-starter-json-2.4.0.jar lib/jackson-datatype-jsr310-2.11.3.jar lib
 /jackson-datatype-jdk8-2.11.3.jar lib/jackson-module-parameter-names-
 2.11.3.jar lib/jackson-databind-2.11.3.jar lib/commons-io-2.4.jar lib
 /bridge-method-annotation-1.14.jar lib/spring-boot-starter-aop-2.4.0.
 jar lib/spring-boot-starter-jdbc-2.4.0.jar lib/spring-boot-starter-2.
 4.0.jar lib/micrometer-core-1.6.1.jar lib/jakarta.transaction-api-1.3
 .3.jar lib/jakarta.persistence-api-2.2.3.jar lib/hibernate-core-5.4.2
 3.Final.jar lib/spring-data-jpa-2.4.1.jar lib/spring-aspects-5.3.1.ja
 r lib/spring-webmvc-5.3.1.jar lib/spring-web-5.3.1.jar lib/jakarta.se
 rvlet-api-4.0.4.jar lib/jakarta.websocket-api-1.1.2.jar lib/jetty-ser
 vlets-9.4.34.v20201102.jar lib/javax-websocket-server-impl-9.4.34.v20
 201102.jar lib/jetty-annotations-9.4.34.v20201102.jar lib/jetty-plus-
 9.4.34.v20201102.jar lib/jetty-webapp-9.4.34.v20201102.jar lib/websoc
 ket-server-9.4.34.v20201102.jar lib/jakarta.el-3.0.3.jar lib/freemark
 er-2.3.30.jar lib/spring-context-support-5.3.1.jar lib/hibernate-vali
 dator-6.1.6.Final.jar lib/jaxb-runtime-2.3.3.jar lib/jakarta.activati
 on-1.2.2.jar lib/json-20160212.jar lib/okhttp-3.14.9.jar lib/aliyun-j
 ava-sdk-kms-2.7.0.jar lib/gson-2.8.6.jar lib/jdom-1.1.jar lib/jettiso
 n-1.1.jar lib/aliyun-java-sdk-core-3.4.0.jar lib/aliyun-java-sdk-ram-
 3.0.0.jar lib/aliyun-java-sdk-sts-3.0.0.jar lib/aliyun-java-sdk-ecs-4
 .2.0.jar lib/jcl-over-slf4j-1.7.30.jar lib/spring-boot-starter-loggin
 g-2.4.0.jar lib/logback-classic-1.2.3.jar lib/springfox-oas-3.0.0.jar
  lib/springfox-data-rest-3.0.0.jar lib/springfox-bean-validators-3.0.
 0.jar lib/springfox-swagger2-3.0.0.jar lib/springfox-swagger-ui-3.0.0
 .jar lib/springfox-swagger-common-3.0.0.jar lib/springfox-spring-webm
 vc-3.0.0.jar lib/springfox-spring-webflux-3.0.0.jar lib/springfox-spr
 ing-web-3.0.0.jar lib/springfox-schema-3.0.0.jar lib/springfox-spi-3.
 0.0.jar lib/springfox-core-3.0.0.jar lib/spring-plugin-metadata-2.0.0
 .RELEASE.jar lib/spring-plugin-core-2.0.0.RELEASE.jar lib/HikariCP-3.
 4.5.jar lib/spring-data-commons-2.4.1.jar lib/swagger-models-1.5.20.j
 ar lib/log4j-to-slf4j-2.13.3.jar lib/jul-to-slf4j-1.7.30.jar lib/slf4
 j-api-1.7.30.jar lib/logback-core-1.2.3.jar lib/guava-25.1-jre.jar li
 b/jackson-core-2.11.3.jar lib/swagger-models-2.1.2.jar lib/jackson-an
 notations-2.11.3.jar lib/joda-time-2.9.9.jar lib/bcprov-jdk15on-1.64.
 jar lib/java-xmlbuilder-1.1.jar lib/okio-1.17.2.jar lib/log4j-core-2.
 13.3.jar lib/log4j-api-2.13.3.jar lib/simple-xml-safe-2.7.1.jar lib/s
 potbugs-annotations-4.0.0.jar lib/jcip-annotations-1.0.jar lib/jsr305
 -3.0.2.jar lib/classmate-1.5.1.jar lib/httpcore-nio-4.4.13.jar lib/ht
 tpcore-4.4.13.jar lib/snakeyaml-1.27.jar lib/JavaEWAH-1.1.7.jar lib/f
 lexmark-util-0.62.2.jar lib/flexmark-util-format-0.62.2.jar lib/flexm
 ark-util-ast-0.62.2.jar lib/flexmark-util-builder-0.62.2.jar lib/flex
 mark-util-dependency-0.62.2.jar lib/flexmark-util-html-0.62.2.jar lib
 /flexmark-util-options-0.62.2.jar lib/flexmark-util-sequence-0.62.2.j
 ar lib/flexmark-util-collection-0.62.2.jar lib/flexmark-util-data-0.6
 2.2.jar lib/flexmark-util-misc-0.62.2.jar lib/flexmark-util-visitor-0
 .62.2.jar lib/autolink-0.6.0.jar lib/leveldb-api-0.12.jar lib/annotat
 ion-indexer-1.4.jar lib/spring-boot-autoconfigure-2.4.0.jar lib/sprin
 g-boot-actuator-2.4.0.jar lib/spring-boot-2.4.0.jar lib/jakarta.annot
 ation-api-1.3.5.jar lib/spring-context-5.3.1.jar lib/spring-aop-5.3.1
 .jar lib/spring-orm-5.3.1.jar lib/spring-jdbc-5.3.1.jar lib/spring-tx
 -5.3.1.jar lib/spring-beans-5.3.1.jar lib/spring-expression-5.3.1.jar
  lib/spring-core-5.3.1.jar lib/HdrHistogram-2.1.12.jar lib/LatencyUti
 ls-2.0.3.jar lib/aspectjweaver-1.9.6.jar lib/hibernate-commons-annota
 tions-5.1.2.Final.jar lib/jboss-logging-3.4.1.Final.jar lib/javassist
 -3.24.0-GA.jar lib/byte-buddy-1.10.18.jar lib/antlr-2.7.7.jar lib/jan
 dex-2.1.3.Final.jar lib/dom4j-2.1.3.jar lib/jetty-continuation-9.4.34
 .v20201102.jar lib/javax-websocket-client-impl-9.4.34.v20201102.jar l
 ib/websocket-client-9.4.34.v20201102.jar lib/jetty-client-9.4.34.v202
 01102.jar lib/jetty-servlet-9.4.34.v20201102.jar lib/jetty-security-9
 .4.34.v20201102.jar lib/jetty-server-9.4.34.v20201102.jar lib/jetty-h
 ttp-9.4.34.v20201102.jar lib/websocket-common-9.4.34.v20201102.jar li
 b/jetty-io-9.4.34.v20201102.jar lib/jetty-xml-9.4.34.v20201102.jar li
 b/jetty-util-9.4.34.v20201102.jar lib/websocket-servlet-9.4.34.v20201
 102.jar lib/jakarta.validation-api-2.0.2.jar lib/stax-api-1.0.1.jar l
 ib/checker-qual-2.0.0.jar lib/error_prone_annotations-2.1.3.jar lib/j
 2objc-annotations-1.1.jar lib/animal-sniffer-annotations-1.14.jar lib
 /swagger-annotations-2.1.2.jar lib/mapstruct-1.3.1.Final.jar lib/swag
 ger-annotations-1.5.20.jar lib/annotations-15.0.jar lib/spring-jcl-5.
 3.1.jar lib/jakarta.xml.bind-api-2.3.3.jar lib/txw2-2.3.3.jar lib/ist
 ack-commons-runtime-3.0.11.jar lib/websocket-api-9.4.34.v20201102.jar
  lib/asm-commons-9.0.jar lib/asm-analysis-9.0.jar lib/asm-tree-9.0.ja
 r lib/asm-9.0.jar lib/classgraph-4.8.83.jar
Main-Class: run.halo.app.Application


Q.E.D.


寻门而入,破门而出