原创

gradle构建springboot项目瘦身,外部依赖jar的终极方法

1.为什么瘦身?

阿里云部署,每次改了代码,上传80多M到服务器,血与泪,如果把jar单独上传的话,那么影响就很小了,只需要传对应的jar上去就可以保证项目正常运行

2.方法build.gradle示例

buildscript {
dependencies {
//配置热部署
classpath 'org.springframework:springloaded:1.2.8.RELEASE'
}
}
//
plugins {
id 'org.springframework.boot' version '2.0.5.RELEASE'
}

apply plugin: 'java'
apply plugin: 'io.spring.dependency-management'

dependencies {
compile project(':cyjz-pojo')
compile project(':cyjz-common')
compile project(':cyjz-interface-auth')
compile project(':cyjz-controller-fileUploadDownload')
//如果要做jar包分离,此处必须要使用compile,不然系统启动失败,血的教训
compile 'org.springframework.boot:spring-boot-starter-amqp'
compile 'org.springframework.boot:spring-boot-starter-data-redis'
compile 'org.springframework.boot:spring-boot-starter-jdbc'
compile 'org.springframework.boot:spring-boot-starter-web'
compile 'org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.2'
testCompile 'org.springframework.boot:spring-boot-starter-test'
compile 'com.alibaba.boot:dubbo-spring-boot-starter:0.2.0'
compile 'org.springframework.session:spring-session-data-redis:2.0.6.RELEASE'
compile 'mysql:mysql-connector-java:5.1.47'

}

//清除lib的jar
task clearJar(type: Delete) {
delete "$buildDir/libs/lib"
}
//复制jar到lib里面去
task copyJar(type: Copy) {
from configurations.runtime
into "$buildDir/libs/lib"
}


bootJar {
// 例外所有的jar
excludes = ["*.jar"]
// lib目录的清除和复制任务
dependsOn clearJar
dependsOn copyJar

// 指定依赖包的路径
manifest {
attributes "Manifest-Version": 1.0,
'Class-Path': configurations.runtime.files.collect { "lib/$it.name" }.join(' ')

}
}


3.调用bootJar

  


4.直接java -jar xxx.jar即可运行项目

正文到此结束