ลบ libs ที่ไม่จำเป็นทั้งหมดออกจากขวดไขมัน

ฉันกำลังเขียนแอปพลิเคชันคอนโซล Java โดยใช้ตัวเชื่อมต่อ Spring Boot Jpa และ MySQL ฉันจะแยก lib ที่ไม่จำเป็นทั้งหมดออกจากขวดไขมันของฉันได้อย่างไร?

build.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'
}

อัปเดต

ฉันได้ลองใช้ 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

อย่างที่คุณเห็น unused libraries ถูกทำเครื่องหมายว่าไลบรารีทั้งหมดในการไล่ระดับของฉันในขณะนี้ และ required librariesare ทำเครื่องหมายเฉพาะที่ใช้ในขอบเขตของคลาสของฉันในแอป แต่อย่าให้ฉันรู้ว่าไลบรารีที่จำเป็นเหล่านี้ขึ้นอยู่กับอะไรด้วย หากฉันจะใส่เฉพาะรายการการพึ่งพาที่จำเป็นเหล่านี้ในการไล่ระดับ ฉันจะได้รับข้อผิดพลาดในการเริ่มต้นที่แตกต่างกันเนื่องจากบางไลบรารีหายไป


person Viktor V.    schedule 11.01.2018    source แหล่งที่มา
comment
เนื่องจากคุณควรเพิ่มการพึ่งพาที่จำเป็นเท่านั้นและสิ่งเหล่านั้นควรนำมาซึ่งการพึ่งพาสกรรมกริยาที่จำเป็นเท่านั้นจึงไม่ควรมี lib ที่ไม่จำเป็นมากมายขนาดนั้น - ถ้ามี แน่นอน ถ้าคุณใช้เพียงส่วนหนึ่งของการขึ้นต่อกัน ก็อาจมีการขึ้นต่อกันแบบสกรรมกริยาที่ไม่จำเป็น (เพราะคุณไม่ได้ใช้โค้ดที่ต้องการ) นั่นคือสิ่งที่คุณหมายถึง นั่นคือ ต้นไม้สั่น?   -  person Thomas    schedule 11.01.2018
comment
คุณพูดถูก ฉันต้องการให้ขวดของฉันมีเฉพาะการพึ่งพาที่ฟังก์ชันแอปใช้เท่านั้น แต่ฉันไม่สามารถเข้าใจได้ว่าวิธีใดที่ง่ายในการตรวจสอบว่า libs ใดที่ไม่จำเป็น/จำเป็นต้องแยก/อยู่   -  person Viktor V.    schedule 11.01.2018
comment
ฉันสงสัยว่าการวิเคราะห์อาร์ติแฟกต์ที่ไม่ได้ใช้นั้นถูกต้อง   -  person Jens Schauder    schedule 12.01.2018


คำตอบ (1)


ข้อเสนอแนะสองสามข้อ

  1. ใช้ dependency:analyze ในโครงการของคุณซึ่งจะแสดงรายการการพึ่งพาที่ไม่ได้ใช้ ซึ่งคุณสามารถยกเว้นหรือกำจัดได้
  2. ใช้แอตทริบิวต์ <scope> สำหรับการขึ้นต่อกันใน pom ซึ่งสามารถลดขนาดขวดไขมันของคุณได้อย่างมาก จัดเตรียมพารามิเตอร์ขอบเขตที่ถูกต้อง เช่น คอมไพล์ ทดสอบ .....
person pvpkiran    schedule 11.01.2018