项目打包需要resources下的配置文件以及项目依赖的jar包均分离出所打的jar包,结合网上的资源,现整理如下:
task clearJar(type: Delete) {
delete "$buildDir\\libs\\lib"
}
task copyJar(type: Copy, dependsOn: 'clearJar') {
from configurations.runtimeClasspath
into "$buildDir\\libs\\lib"
}
// 拷贝配置文件
task copyResources(type: Copy) {
delete "$buildDir\\libs\\config"
from('src/main/resources')
into 'build/libs/resources'
}
jar {
//这里排除resources下的文件及文件夹
excludes = ["lib/**","*.properties","*.xml","*.yml","mybatis/**"]
dependsOn clearJar
dependsOn copyJar
dependsOn copyResources
//通过configurations.runtime拿到所有的运行时的依赖jar包,然后.each遍历他,通过it.name获取到每个的jar包的name,赋值
manifest {
attributes 'Main-Class': '*.Application'
attributes 'Class-Path': 'resources/ '+ configurations.runtimeClasspath.files.collect { "lib/$it.name" }.join(' ')
}
}