gradle打包web jar_Gradle打包可执行jar文件

dependencies {

testImplementation group: 'junit', name: 'junit', version: '4.12'

implementation('io.netty:netty-all:4.1.43.Final')

implementation("com.google.guava:guava:28.1-jre")

}

在根项目的build.gradle中,添加如下代码

jar {

//详细信息参考 https://docs.gradle.org/current/dsl/org.gradle.api.tasks.bundling.Jar.html

archivesBaseName = 'MiniRPC'//基本的文件名

archiveVersion = '0.0.3' //版本

manifest { //配置jar文件的manifest

attributes(

"Manifest-Version": 1.0,

'Main-Class': 'com.mirs.minirpc.App' //指定main方法所在的文件

)

}

//打包依赖包

from {

(configurations.runtimeClasspath).collect {

it.isDirectory() ? it : zipTree(it)

}

}

}

这里踩了个坑,网上绝大多数文章在打包依赖的时候都是使用的configurations.runtime。使用compile引入的依赖,使用这种方式可以打包,但对于implementation引入的依赖,要使用configurations.runtimeClasspath才能把依赖打包进去。无论compile还是implementation引入依赖,configurations.runtimeClasspath都可以打包依赖。

此外对于testImplementation引入的依赖,则使用configurations.testRuntimeClasspath。

(参考文章 https://www.zhuyanbin.com/?p=654)

打包后的jar文件

image.png

运行结果

image.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值