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

原文链接:https://www.dubby.cn/detail.html?id=9091

介绍maven-jar-pluginmaven-assembly-pluginmaven-shade-plugin的使用,和他们之间的区别。原文出自:https://blog.dubby.cn/detail.html?id=9091

1.maven-jar-plugin

首先,需要配置这个plugin:


    org.apache.maven.plugins
    maven-jar-plugin
    
        
            
                true
                lib/
                cn.dubby.maven.jar.plugin.Main
            
        
    

编写启动类:

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的依赖:


    
    
        joda-time
        joda-time
        2.9.9
    

修改启动类:

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时的另外两个配置项:


    org.apache.maven.plugins
    maven-jar-plugin
    
        
            
                true
                lib/
                cn.dubby.maven.jar.plugin.Main
            
        
    

addClasspathclasspathPrefix表明会把lib下的jar作为第三方依赖加入package里,所以我们需要把依赖拷贝到lib目录下,我们可以使用maven-dependency-plugin:


    org.apache.maven.plugins
    maven-dependency-plugin
    
        
            copy
            compile
            
                copy-dependencies
            
            
                
                    ${project.build.directory}/lib
                
            
        
    

这样我们重新打包再运行:

➜  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目录。

原文出自:https://blog.dubby.cn/detail.html?id=9091

2.maven-assembly-plugin

先配置plugin:



    org.apache.maven.plugins
    maven-assembly-plugin
    2.4.1
    
        
        
            jar-with-dependencies
        
        
        
            
                cn.dubby.maven.assembly.plugin.Main
            
        
    
    
        
            make-assembly
            
            package
            
                single
            
        
    

启动类同上:

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

原文出自:https://blog.dubby.cn/detail.html?id=9091

3.maven-shade-plugin

配置如下:



    org.apache.maven.plugins
    maven-shade-plugin
    3.1.0
    
        
            package
            
                shade
            
            
                
                    
                        cn.dubby.maven.shade.plugin.Main
                    
                
            
        
    

启动类同上:

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

原文出自:https://blog.dubby.cn/detail.html?id=9091

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

你可能感兴趣的:(使用Maven打包你的应用——maven-jar-plugin & maven-assembly-plugin & maven-shade-plugin)