Maven - 打包可执行jar包

打包方法

方法一:使用maven-jar-pluginmaven-dependency-plugin

首先,maven-jar-plugin的作用是配置mainClass和指定classpath。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>libs/</classpathPrefix>
                <mainClass>
                    org.baeldung.executable.ExecutableMavenJar
                </mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>

addClasspath: 是否在manifest文件中添加classpath。默认为false。如果为true,则会在manifest文件中添加classpath,这样在启动的时候就不用再手动指定classpath了。如下所示,文件中增加了Class-Path一行

Manifest-Version: 1.0                                                                                                                                         
Archiver-Version: Plexus Archiver
Built-By: michealyang
Class-Path: libs/jetty-server-9.4.7.v20170914.jar lib/javax.servlet-api
 -3.1.0.jar libs/jetty-http-9.4.7.v20170914.jar 
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_162-ea
Main-Class: com.michealyang.jetty.embeded.EmbeddedJettyServer

classpathPrefix: classpath的前缀。如上面的manifest文件中,Class-Path的值中,每个jar包的前缀都是libs/。本质上,这个配置的值是所依赖jar包所在的文件夹。配置正确了才能找到依赖
mainClass: 指定启动时的Main Class

其次,maven-dependency-plugin会把所依赖的jar包copy到指定目录

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <outputDirectory>
                    ${project.build.directory}/libs
                </outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

executions中的配置都很重要,按照上面的配置来就行了。outputDirectory指定了要将所依赖的jar包copy到哪个目录。要与maven-jar-plugin中的classpathPrefix一致。

执行如下命令,即可打包:

mvn package

打包结果是,自己写的Class在jar包中,所依赖的jar包在libs目录中:

├── embedded-jetty-1.0.0-SNAPSHOT.jar
├── lib
│ ├── jetty-server-9.4.7.v20170914.jar
│ ├── jetty-http-9.4.7.v20170914.jar

执行如下命令即可启动jar包:

java -jar embedded-jetty-1.0.0-SNAPSHOT.jar

优点
有诸多配置项,很自由,每个步骤都可控

缺点
打成的最终jar包中没有所依赖的jar包。依赖跟自己的代码不在一个jar包中。部署或者移动的时候,要考虑到多个文件,比较麻烦

方法二:使用maven-assembly-plugin (推荐)

maven-assembly-plugin可以将所有的东西都打包到一个jar包中。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
            <configuration>
                <archive>
                <manifest>
                    <mainClass>
                        com.michealyang.jetty.embeded.EmbeddedJettyServer
                    </mainClass>
                </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </execution>
    </executions>
</plugin>

执行mvn package后,会在target文件夹下生成两个jar包,一个是不带依赖的jar包,一个是后缀有-dependencies带有依赖的jar包,如:

May 31 16:42 embedded-jetty-1.0.0-SNAPSHOT-jar-with-dependencies.jar
May 31 16:42 embedded-jetty-1.0.0-SNAPSHOT.jar

启动时,直接执行即可:

java -jar embedded-jetty-1.0.0-SNAPSHOT-jar-with-dependencies.jar

优点
所有的东西都打到一个jar包中,很方便
缺点
配置项少,不自由。

方法三:使用maven-shade-plugin

maven-assembly-plugin类似,都可以将所有的东西都打包到一个jar包中。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <shadedArtifactAttached>true</shadedArtifactAttached>
                <transformers>
                    <transformer implementation=
                      "org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>com.michealyang.jetty.embeded.EmbeddedJettyServer</mainClass>
                </transformer>
            </transformers>
        </configuration>
        </execution>
    </executions>
</plugin>

执行mvn package后,会在target文件夹下生成两个jar包,一个是不带依赖的jar包,一个是后缀有-shaded带有依赖的jar包,如:

May 31 16:53 embedded-jetty-1.0.0-SNAPSHOT-shaded.jar
May 31 16:53 embedded-jetty-1.0.0-SNAPSHOT.jar

启动时,直接执行即可:

java -jar embedded-jetty-1.0.0-SNAPSHOT-jar-with-shaded.jar

优点
功能同maven-assembly-plugin,但比前者强大
缺点
配置起来太麻烦。当你需要高级功能的时候,更是麻烦的不要不要的。

方法四:使用onejar-maven-plugin

This provides custom classloader that knows how to load classes and resources from jars inside an archive, instead of from jars in the filesystem.

<plugin>
    <groupId>com.jolira</groupId>
    <artifactId>onejar-maven-plugin</artifactId>
    <executions>
        <execution>
            <configuration>
                <mainClass>org.baeldung.executable.
                  ExecutableMavenJar</mainClass>
                <attachToBuild>true</attachToBuild>
                <filename>
                  ${project.build.finalName}.${project.packaging}
                </filename>
            </configuration>
            <goals>
                <goal>one-jar</goal>
            </goals>
        </execution>
    </executions>
</plugin>

优点
clean delegation model, allows classes to be at the top-level of the One Jar, supports external jars and can support Native libraries
缺点
not actively supported since 2012

方法五:使用spring-boot-maven-plugin

能同时打可执行jar包和war包
This allows to package executable jar or war archives and run an application “in-place”.

需要maven版本不低于3.2

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
            <configuration>
                <classifier>spring-boot</classifier>
                <mainClass>
                  org.baeldung.executable.ExecutableMavenJar
                </mainClass>
            </configuration>
        </execution>
    </executions>
</plugin>

两个重点:

  • goal要写成repackage
  • classifier要写成spring-boot

优点
dependencies inside a jar file, you can run it in every accessible location, advanced control of packaging your artifact, with excluding dependencies from the jar file etc., packaging of war files as well

缺点
添加了一些不必要的Spring和Spring Boot依赖

方法六:使用tomcat7-maven-plugin

可打包成一个web工程类型的jar包。其实是内嵌了一个tomcat在里面。

<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.0</version>
    <executions>
        <execution>
            <id>tomcat-run</id>
            <goals>
                <goal>exec-war-only</goal>
            </goals>
            <phase>package</phase>
            <configuration>
                <path>/</path>
                <enableNaming>false</enableNaming>
                <finalName>webapp.jar</finalName>
                <charset>utf-8</charset>
            </configuration>
        </execution>
    </executions>
</plugin>

The goal is set as exec-war-only, path to your server is specified inside configuration tag, with additional properties, like finalName, charset etc. To build a jar, run man package, which will result in creating webapp.jar in your target directory. To run

To run the application, just write this in your console: java -jar target/webapp.jar and try to test it by specifying the localhost:8080/ in a browser.

优点
只有一个jar包
缺点
打包出的文件很大。因为里面内嵌了一个tomcat

参考

How to Create an Executable JAR with Maven

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,242评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,769评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,484评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,133评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,007评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,080评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,496评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,190评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,464评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,549评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,330评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,205评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,567评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,889评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,160评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,475评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,650评论 2 335

推荐阅读更多精彩内容