Créer un gros fichier JAR - Maven Shade Plugin

Créer un gros fichier Jar - Plugin Maven Shade

java jar with maven

Dans ce tutoriel, nous allons vous montrer comment utiliserMaven Shade Plugin pour créer un Jar avec ses Jars de dépendance dans un seul fichier Jar exécutable, appelé fat Jar ou uber Jar.

Note
Le plugin Maven Shade est un meilleur plugin pour créer un pot fat / uber, si comparé avecassembly plugin, car il fournit la fonctionnalitéclass relocating, pour éviter les conflits de nom de même classe dans le classpath.

1. Examiner un projet Java

Le projet Java précédent (dateutils) sera réutilisé, voir la structure de dossiers suivante

one-jar-folder-structure

Note
Ce projet a une seule dépendance -joda-time.jar

2. Pom.xml

Lisez le commentaire ci-dessous pour plus d'explications.

pom.xml


    4.0.0
    com.example.core.utils
    dateUtils
    jar
    1.0-SNAPSHOT
    dateUtils
    http://maven.apache.org

    
        1.7
        2.5
        4.11
    

    
        
            junit
            junit
            ${junit.version}
            test
        
        
            joda-time
            joda-time
            ${jodatime.version}
        
    

    
        dateutils
        

            
            
                org.apache.maven.plugins
                maven-eclipse-plugin
                2.9
                
                    true
                    false
                
            

            
            
                org.apache.maven.plugins
                maven-compiler-plugin
                2.3.2
                
                    ${jdk.version}
                    ${jdk.version}
                
            

        
        
          org.apache.maven.plugins
          maven-shade-plugin
          2.3
          
             
            
            package
            
                shade
            
            
              
                
                                
                    com.example.core.utils.App
                
              
            
              
          
        

        
    

3. Emballez-le

Pour produire le Jar final, il suffit de le conditionner:

$ mvn package

[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ dateUtils ---
[INFO] Building jar: /Users/example/dateUtils/target/dateutils.jar
[INFO]
[INFO] --- maven-shade-plugin:2.3:shade (default) @ dateUtils ---
[INFO] Including joda-time:joda-time:jar:2.5 in the shaded jar.
[INFO] Replacing original artifact with shaded artifact.
[INFO] Replacing /Users/example/dateUtils/target/dateutils.jar with /Users/example/dateUtils/target/dateUtils-1.0-SNAPSHOT-shaded.jar
[INFO] Dependency-reduced POM written at: /Users/example/dateUtils/dependency-reduced-pom.xml
[INFO] Dependency-reduced POM written at: /Users/example/dateUtils/dependency-reduced-pom.xml
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.594s
[INFO] Finished at: Tue Oct 21 15:24:28 MYT 2014
[INFO] Final Memory: 17M/42M
[INFO] ------------------------------------------------------------------------

Deux fichiers jar seront créés dans le dossiertarget.

  1. dateutils.jar – Project and dependency classes in a single jar, this is what you want.

  2. original-dateutils.jar – Only your project classes

P.S The generated dependency-reduced-pom.xml is for reference only, just ignore it.

4. Révise le

Répertoriez le contenu dedateutils.jar

$ jar tf target/dateutils.jar

META-INF/MANIFEST.MF
META-INF/
com/
com/example/
com/example/core/
com/example/core/utils/
com/example/core/utils/App.class
META-INF/maven/
META-INF/maven/com.example.core.utils/
META-INF/maven/com.example.core.utils/dateUtils/
META-INF/maven/com.example.core.utils/dateUtils/pom.xml
META-INF/maven/com.example.core.utils/dateUtils/pom.properties
META-INF/LICENSE.txt
META-INF/NOTICE.txt
org/
org/joda/
org/joda/time/
org/joda/time/base/
org/joda/time/base/AbstractDateTime.class
org/joda/time/base/AbstractDuration.class
//...
org/joda/time/Weeks.class
org/joda/time/YearMonth$Property.class
org/joda/time/YearMonth.class
org/joda/time/YearMonthDay$Property.class
org/joda/time/YearMonthDay.class
org/joda/time/Years.class
META-INF/maven/joda-time/
META-INF/maven/joda-time/joda-time/
META-INF/maven/joda-time/joda-time/pom.xml
META-INF/maven/joda-time/joda-time/pom.properties

MANIFEST.MF

Manifest-Version: 1.0
Build-Jdk: 1.7.0_05
Built-By: example
Created-By: Apache Maven 3.1.1
Main-Class: com.example.core.utils.App
Archiver-Version: Plexus Archiver

Exécuter

$ java -jar target/dateutils.jar

2014-10-21

Télécharger le code source

Téléchargez-le -dateUtils-maven-shade-plugin.zip (10 Ko)