Создать толстый файл Jar - Maven Shade Plugin

Создайте толстый файл Jar - плагин Maven Shade

java jar with maven

В этом руководстве мы покажем вам, как использоватьMaven Shade Plugin для создания Jar вместе с Jar зависимостей в один исполняемый файл Jar, так называемый fat Jar или uber Jar.

ПлагинNote
Maven Shade - лучший плагин для создания толстой / убер-банки, по сравнению сassembly plugin, потому что он предоставляет функциюclass relocating, чтобы избежать конфликта имен одного и того же класса в путь к классам.

1. Обзор проекта Java

Предыдущий проект Java (dateutils) будет повторно использован, см. Следующую структуру папок

one-jar-folder-structure

Note
Этот проект имеет единственную зависимость -joda-time.jar

2. Pom.xml

Прочтите комментарий ниже, чтобы не требовать пояснений.

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. Упаковать это

Чтобы создать окончательный Jar, просто упакуйте его:

$ 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] ------------------------------------------------------------------------

В папкеtarget будут созданы два файла jar.

  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. Обзор

Перечислите содержаниеdateutils.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

Запустить его

$ java -jar target/dateutils.jar

2014-10-21

Скачать исходный код

Скачать -dateUtils-maven-shade-plugin.zip (10 КБ)