Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove all unnecessary libs from fat jar

I'm writing java console application using Spring Boot Jpa and MySQL connector. How I can easily exclude all unnecessary libs from my fat jar?

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

UPDATE

I have tried to use gradle-dependency-analyze and received the following result:

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

As you see unused libraries are marked all libraries in my gradle at the moment. And required librariesare marked only which are used in the scope of my classes in app but don't get me know what these required libraries depends on also. If I will put only these list of required dependencies in the gradle then I will get different initialization errors since some libraries are missed.

like image 546
Viktor M. Avatar asked Jan 11 '18 14:01

Viktor M.


1 Answers

Couple of suggestions.

  1. Use dependency:analyze on your project which will list the unused dependencies, which you can exclude or get rid of.
  2. use the <scope> attribute for each and every dependency in your pom, which can greatly reduce your fat jar size. Provide correct scope parameters, like compile, test .....
like image 71
pvpkiran Avatar answered Oct 25 '22 20:10

pvpkiran