java spring boot 项目使用gradle打包时分离libs目录下的jar包和yml文件
在项目build.gradle 配置文件中添加如下代码jar {exclude(['application*.yml', 'application.properties'])}task copyConfigFile(type: Copy) {from('src/main/resources') {include 'application.yml...
·
在项目build.gradle 配置文件中添加如下代码 jar { exclude(['application*.yml', 'application.properties']) } task copyConfigFile(type: Copy) { from('src/main/resources') { include 'application.yml' } from('/') { include 'libs/' //exclude 'libs/*.jar' } into 'build/libs' rename ('yml', 'yml') } bootJar.finalizedBy copyConfigFile
注意:如果使用 bootRepackage 命令打包时,最后一行应当添加如下代码:
bootRepackage.finalizedBy copyConfigFile
如果使用bootJar命令打包时,最后一行应当添加如下代码:
bootJar.finalizedBy copyConfigFile
完整代码: build.gradle 文件
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'application'
compileJava.options.encoding = 'UTF-8'
mainClassName = 'cn.tnar.parkservice.ParkServiceApplication'
apply plugin: 'io.spring.dependency-management'
//plugins {
// id "io.spring.dependency-management" version "1.0.7.RELEASE"
//}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-log4j2'
implementation "com.lmax:disruptor:${disruptorVersion}"
implementation 'org.springframework.boot:spring-boot-starter-actuator'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.springframework.cloud:spring-cloud-starter-alibaba-nacos-discovery'
implementation 'org.springframework.cloud:spring-cloud-starter-alibaba-nacos-config'
compile group: 'redis.clients', name: 'jedis', version: '2.9.0'
compile group: 'com.auth0', name: 'java-jwt', version: '3.3.0'
compile group: 'org.apache.shiro', name: 'shiro-core', version: '1.4.0'
compile group: 'org.apache.shiro', name: 'shiro-web', version: '1.3.2'
compile group: 'org.apache.shiro', name: 'shiro-spring', version: '1.3.2'
compile group: 'javax.persistence', name: 'persistence-api', version: '1.0'
compile group: 'com.baomidou', name: 'mybatis-plus-boot-starter', version: '3.1.0'
compile group: 'org.mybatis.spring.boot', name: 'mybatis-spring-boot-starter', version: '1.3.2'
compile group: 'com.alibaba', name: 'druid-spring-boot-starter', version: '1.1.10'
compile group: 'mysql', name: 'mysql-connector-java', version: '6.0.6'
compile group: 'com.squareup.okhttp3', name: 'okhttp', version: '3.10.0'
compileOnly 'org.projectlombok:lombok:1.18.8'
annotationProcessor 'org.projectlombok:lombok:1.18.8'
implementation 'org.apache.httpcomponents:httpclient'
implementation 'com.google.code.gson:gson'
implementation fileTree(dir: "libs", include: ["*.jar"])
compile group: 'io.springfox', name: 'springfox-swagger2', version: '2.9.2'
compile group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.9.2'
compile group: 'org.apache.poi', name: 'poi', version: '3.15-beta2'
compile group: 'org.apache.poi', name: 'poi-ooxml', version: '3.15-beta2'
compile group: 'io.jsonwebtoken', name: 'jjwt', version: '0.7.0'
// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-amqp
compile group: 'org.springframework.boot', name: 'spring-boot-starter-amqp', version: '2.1.7.RELEASE'
compile group: 'com.rabbitmq', name: 'http-client', version: '3.3.0.RELEASE'
// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-redis
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-redis', version: '2.1.7.RELEASE'
// https://mvnrepository.com/artifact/net.coobird/thumbnailator
compile group: 'net.coobird', name: 'thumbnailator', version: '0.4.8'
// https://mvnrepository.com/artifact/com.aliyun.oss/aliyun-sdk-oss
compile group: 'com.aliyun.oss', name: 'aliyun-sdk-oss', version: '3.4.2'
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.9'
}
jar {
exclude(['application*.yml', 'application.properties','bootstrap.yml'])
}
task copyConfigFile(type: Copy) {
from('src/main/resources') {
include 'bootstrap.yml'
}
// from('/') {
// include 'libs/'
// //exclude 'libs/*.jar'
// }
into 'build/libs'
rename ('yml', 'yml')
}
// 清除lib
task myClearLib(type: Delete) {
delete "$buildDir/libs/lib"
}
// 拷贝lib
task myCopyLib(type: Copy) {
into "$buildDir/libs/lib"
from configurations.runtime
from 'libs/'
}
bootJar {
excludes = ["*.jar"]
// lib目录的清除和复制任务
dependsOn myClearLib
dependsOn myCopyLib
// 指定依赖包的路径,运行时不再需要指定 java.ext.dir 或 loader.path 参数。
manifest {
attributes "Manifest-Version": 1.0,
'Class-Path': configurations.runtime.files.collect { "lib/$it.name" }.join(' ')
}
}
bootJar.finalizedBy copyConfigFile
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)