使用gradle项目打包jar包的时候如果不使用fat jar的打包模式会造成dependency没有全部被打在一个包中的结果
譬如在denpendency的情况为如下的情况时
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile fileTree(dir:'libs',include:['*.jar'])
implementation group: 'commons-validator', name: 'commons-validator', version: '1.4.0'
implementation group: 'commons-io', name: 'commons-io', version: '2.6'
compile group: 'org.apache.commons', name: 'commons-pool2', version: '2.0'
testCompile group: 'junit', name: 'junit', version: '4.12'
implementation 'junit:junit:4.12'
implementation group: 'com.google.protobuf', name: 'protobuf-java-util', version: '3.10.0'
implementation group: 'com.googlecode.protobuf-java-format', name: 'protobuf-java-format', version: '1.2'
//lombok
compile group: 'org.projectlombok', name: 'lombok', version: '1.18.12'
annotationProcessor group: 'org.projectlombok', name: 'lombok', version: '1.18.12'
implementation group: 'org.slf4j', name: 'slf4j-api', version: '1.7.30'
implementation group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.30'
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.12.1'
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.12.1'
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.11'
implementation group: 'io.netty', name: 'netty-all', version: '4.1.59.Final'
implementation group: 'io.grpc', name: 'grpc-all', version: "$ioGrpcVersion"
implementation group: 'io.grpc', name: 'grpc-netty-shaded', version: "$ioGrpcVersion"
}
如果不使用fat jar的打包执行后会出现找不到log4j包的错误
以下提供两种打fatJar的方式
jar {
manifest {
attributes "Main-Class": 'com.foo.bar.Main'
}
from {
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
}
task fatJar(type: Jar) {
manifest.from jar.manifest
classifier = 'all'
from {
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
} {
exclude "META-INF/*.SF"
exclude "META-INF/*.DSA"
exclude "META-INF/*.RSA"
}
with jar
}
代码很简单,只是记录一下以便下次直接使用就不多做解释了