使用Maven打包你的應用——maven-jar-plugin & maven-assembly-plugin & maven-shade-plugin

我是賣報滴小行家?發表於2018-10-17

原文連結:https://www.dubby.cn/detail.html?id=9091

介紹maven-jar-pluginmaven-assembly-pluginmaven-shade-plugin的使用,和他們之間的區別。原文出自:blog.dubby.cn/detail.html…

1.maven-jar-plugin

首先,需要配置這個plugin:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>lib/</classpathPrefix>
                <mainClass>cn.dubby.maven.jar.plugin.Main</mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>
複製程式碼

編寫啟動類:

public static void main(String[] args) {
    System.out.println("Hello, this is a application packaged by maven-jar-plugin");
      LocalDateTime localDateTime = new LocalDateTime();
      System.out.println(localDateTime);
}
複製程式碼

啟動:

➜  target git:(master) ✗ java -jar maven-jar-plugin-1.0-SNAPSHOT.jar
Hello, this is a application packaged by maven-jar-plugin
複製程式碼

但是,如果加上一個依賴呢?

我們加上joda-time的依賴:

<dependencies>
    <!-- https://mvnrepository.com/artifact/joda-time/joda-time -->
    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
        <version>2.9.9</version>
    </dependency>
</dependencies>
複製程式碼

修改啟動類:

public static void main(String[] args) {
    System.out.println("Hello, this is a application packaged by maven-jar-plugin");
    LocalDateTime localDateTime = new LocalDateTime();
    System.out.println(localDateTime);
}
複製程式碼

再執行:

➜  target git:(master) ✗ java -jar maven-jar-plugin-1.0-SNAPSHOT.jar
Hello, this is a application packaged by maven-jar-plugin
Exception in thread "main" java.lang.NoClassDefFoundError: org/joda/time/LocalDateTime
        at cn.dubby.maven.jar.plugin.Main.main(Main.java:13)
Caused by: java.lang.ClassNotFoundException: org.joda.time.LocalDateTime
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 1 more
複製程式碼

發現我們增加的依賴並沒有找到,怎麼辦呢?

請注意觀察我們上面配置maven-jar-plugin時的另外兩個配置項:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>lib/</classpathPrefix>
                <mainClass>cn.dubby.maven.jar.plugin.Main</mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>
複製程式碼

addClasspathclasspathPrefix表明會把lib下的jar作為第三方依賴加入package裡,所以我們需要把依賴拷貝到lib目錄下,我們可以使用maven-dependency-plugin:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>copy</id>
            <phase>compile</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <outputDirectory>
                    ${project.build.directory}/lib
                </outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>
複製程式碼

這樣我們重新打包再執行:

➜  target git:(master) ✗ java -jar maven-jar-plugin-1.0-SNAPSHOT.jar
Hello, this is a application packaged by maven-jar-plugin
2018-01-22T14:48:00.915
複製程式碼

去target目錄下也可以發現多了個lib目錄。

原文出自:blog.dubby.cn/detail.html…

2.maven-assembly-plugin

先配置plugin:

<!-- Maven Assembly Plugin -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.4.1</version>
    <configuration>
        <!-- get all project dependencies -->
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <!-- MainClass in mainfest make a executable jar -->
        <archive>
            <manifest>
                <mainClass>cn.dubby.maven.assembly.plugin.Main</mainClass>
            </manifest>
        </archive>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <!-- bind to the packaging phase -->
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>
複製程式碼

啟動類同上:

public static void main(String[] args) {
    System.out.println("Hello, this is a application packaged by maven-assembly-plugin");
    LocalDateTime localDateTime = new LocalDateTime();
    System.out.println(localDateTime);
}
複製程式碼

打包執行結果:

➜  target git:(master) ✗ java -jar maven-assembly-plugin-1.0-SNAPSHOT-jar-with-dependencies.jar
Hello, this is a application packaged by maven-assembly-plugin
2018-01-22T15:44:05.166
複製程式碼

原文出自:blog.dubby.cn/detail.html…

3.maven-shade-plugin

配置如下:

<!-- maven-shade-plugin -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.1.0</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>cn.dubby.maven.shade.plugin.Main</mainClass>
                    </transformer>
                </transformers>
            </configuration>
        </execution>
    </executions>
</plugin>
複製程式碼

啟動類同上:

public static void main(String[] args) {
    System.out.println("Hello, this is a application packaged by maven-shade-plugin");
    LocalDateTime localDateTime = new LocalDateTime();
    System.out.println(localDateTime);
}
複製程式碼

結果:

➜  target git:(master) ✗ java -jar maven-shade-plugin-1.0-SNAPSHOT.jar
Hello, this is a application packaged by maven-shade-plugin
2018-01-22T15:42:19.662
複製程式碼

原文出自:blog.dubby.cn/detail.html…

4.package比較

-rw-r--r--  1 teeyoung  staff   622K  1 22 15:43 maven-assembly-plugin-1.0-SNAPSHOT-jar-with-dependencies.jar
-rw-r--r--  1 teeyoung  staff   3.1K  1 22 14:47 maven-jar-plugin-1.0-SNAPSHOT.jar
-rw-r--r--  1 teeyoung  staff   622K  1 22 15:42 maven-shade-plugin-1.0-SNAPSHOT.jar
複製程式碼

相關文章