gradle構建springboot專案瘦身,外部依賴jar的終極方法

DeBuggggggg發表於2019-05-05

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即可執行專案

相關文章