从 fat jar 中删除所有不必要的库

Vik*_* V. 6 java gradle spring-data-jpa spring-boot

我正在使用 Spring Boot Jpa 和 MySQL 连接器编写 java 控制台应用程序。如何轻松地从胖罐中排除所有不必要的库?

构建.gradle

buildscript {
    ext {
        springBootVersion = '1.5.9.RELEASE'
    }
    repositories {
//        mavenCentral()
        jcenter()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath 'ca.cutterslade.gradle:gradle-dependency-analyze:1.2.0'
    }
}

apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'ca.cutterslade.analyze'

sourceCompatibility = 1.8
targetCompatibility = 1.8

ext {
    mysqlVersion = '6.0.6'
    hibernateVersion = '5.2.12.Final'
}

repositories {
    mavenCentral()
}

dependencies {
//    compile group: 'org.hibernate', name: 'hibernate-core', version: '5.2.12.Final'
//    compile group: 'org.hibernate', name: 'hibernate-entitymanager', version: '5.2.12.Final'
//    compile group: 'mysql', name: 'mysql-connector-java', version: '6.0.6'
//    compile group: 'org.springframework.boot', name: 'spring-boot', version: '1.5.9.RELEASE'
//    compile group: 'org.hibernate.javax.persistence', name: 'hibernate-jpa-2.0-api', version: '1.0.0.Final'
//    compile group: 'org.springframework', name: 'spring-context', version: '4.3.13.RELEASE'
//    compile group: 'org.springframework', name: 'spring-beans', version: '4.3.13.RELEASE'
//    compile group: 'org.springframework.boot', name: 'spring-boot-autoconfigure', version: '1.5.9.RELEASE'
//    compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25'
//    compile group: 'org.springframework', name: 'spring-tx', version: '2.5.4'
//    compile group: 'org.springframework.data', name: 'spring-data-jpa', version: '1.11.9.RELEASE'
//    testCompile group: 'org.springframework.boot', name: 'spring-boot-test', version: '1.5.9.RELEASE'
//    testCompile group: 'junit', name: 'junit', version: '4.12'
//    testCompile group: 'org.springframework', name: 'spring-test', version: '4.3.13.RELEASE'



    compile group: 'mysql', name: 'mysql-connector-java', version: mysqlVersion
    compile group: 'org.hibernate', name: 'hibernate-core', version: hibernateVersion
    compile("org.springframework.boot:spring-boot-starter-data-jpa")
    testCompile("org.springframework.boot:spring-boot-starter-test")
}

jar {
    baseName 'ReportGenerator'
    version '1.0'
}
Run Code Online (Sandbox Code Playgroud)

更新

我尝试使用gradle-dependency-analyze并收到以下结果:

usedUndeclaredArtifacts: 
 - org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.0.Final
 - org.springframework.boot:spring-boot:1.5.9.RELEASE
 - org.springframework:spring-context:4.3.13.RELEASE
 - org.springframework:spring-beans:4.3.13.RELEASE
 - org.springframework.boot:spring-boot-autoconfigure:1.5.9.RELEASE
 - org.slf4j:slf4j-api:1.7.25
 - org.springframework:spring-tx:4.3.13.RELEASE
 - org.springframework.data:spring-data-jpa:1.11.9.RELEASE
unusedDeclaredArtifacts: 
 - mysql:mysql-connector-java:6.0.6
 - org.hibernate:hibernate-core:5.2.12.Final
 - org.springframework.boot:spring-boot-starter-data-jpa:1.5.9.RELEASE
Run Code Online (Sandbox Code Playgroud)

如您所见,unused libraries目前我的 gradle 中标记了所有库。并且required libraries仅标记在我的应用程序类范围内使用的内容,但不要让我知道这些必需的库也依赖于什么。如果我只将这些必需的依赖项列表放在 gradle 中,那么由于缺少某些库,我将收到不同的初始化错误。

pvp*_*ran 5

几个建议。

  1. 在您的项目中使用dependency:analyze,它将列出未使用的依赖项,您可以排除或摆脱它们。
  2. <scope>对 pom 中的每个依赖项使用该属性,这可以大大减少 fat jar 的大小。提供正确的范围参数,如编译、测试......