打包方法
方法一:使用maven-jar-plugin
和maven-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。默認(rèn)為false庄撮。如果為true唯竹,則會(huì)在manifest文件中添加classpath沸停,這樣在啟動(dòng)的時(shí)候就不用再手動(dòng)指定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的值中薪丁,每個(gè)jar包的前綴都是libs/。本質(zhì)上馅精,這個(gè)配置的值是所依賴(lài)jar包所在的文件夾严嗜。配置正確了才能找到依賴(lài)
mainClass: 指定啟動(dòng)時(shí)的Main Class
其次,maven-dependency-plugin
會(huì)把所依賴(lài)的jar包c(diǎn)opy到指定目錄
<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中的配置都很重要洲敢,按照上面的配置來(lái)就行了漫玄。outputDirectory指定了要將所依賴(lài)的jar包c(diǎn)opy到哪個(gè)目錄。要與maven-jar-plugin
中的classpathPrefix一致。
執(zhí)行如下命令睦优,即可打包:
mvn package
打包結(jié)果是渗常,自己寫(xiě)的Class在jar包中,所依賴(lài)的jar包在libs目錄中:
├── embedded-jetty-1.0.0-SNAPSHOT.jar
├── lib
│ ├── jetty-server-9.4.7.v20170914.jar
│ ├── jetty-http-9.4.7.v20170914.jar
執(zhí)行如下命令即可啟動(dòng)jar包:
java -jar embedded-jetty-1.0.0-SNAPSHOT.jar
優(yōu)點(diǎn)
有諸多配置項(xiàng)汗盘,很自由皱碘,每個(gè)步驟都可控
缺點(diǎn)
打成的最終jar包中沒(méi)有所依賴(lài)的jar包。依賴(lài)跟自己的代碼不在一個(gè)jar包中隐孽。部署或者移動(dòng)的時(shí)候癌椿,要考慮到多個(gè)文件,比較麻煩
方法二:使用maven-assembly-plugin
(推薦)
maven-assembly-plugin
可以將所有的東西都打包到一個(gè)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>
執(zhí)行mvn package
后踢俄,會(huì)在target文件夾下生成兩個(gè)jar包,一個(gè)是不帶依賴(lài)的jar包晴及,一個(gè)是后綴有-dependencies
帶有依賴(lài)的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
啟動(dòng)時(shí),直接執(zhí)行即可:
java -jar embedded-jetty-1.0.0-SNAPSHOT-jar-with-dependencies.jar
優(yōu)點(diǎn)
所有的東西都打到一個(gè)jar包中抗俄,很方便
缺點(diǎn)
配置項(xiàng)少脆丁,不自由。
方法三:使用maven-shade-plugin
跟maven-assembly-plugin
類(lèi)似动雹,都可以將所有的東西都打包到一個(gè)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>
執(zhí)行mvn package
后,會(huì)在target文件夾下生成兩個(gè)jar包胰蝠,一個(gè)是不帶依賴(lài)的jar包歼培,一個(gè)是后綴有-shaded
帶有依賴(lài)的jar包,如:
May 31 16:53 embedded-jetty-1.0.0-SNAPSHOT-shaded.jar
May 31 16:53 embedded-jetty-1.0.0-SNAPSHOT.jar
啟動(dòng)時(shí)茸塞,直接執(zhí)行即可:
java -jar embedded-jetty-1.0.0-SNAPSHOT-jar-with-shaded.jar
優(yōu)點(diǎn)
功能同maven-assembly-plugin
躲庄,但比前者強(qiáng)大
缺點(diǎn)
配置起來(lái)太麻煩。當(dāng)你需要高級(jí)功能的時(shí)候钾虐,更是麻煩的不要不要的噪窘。
方法四:使用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>
優(yōu)點(diǎn)
clean delegation model, allows classes to be at the top-level of the One Jar, supports external jars and can support Native libraries
缺點(diǎn)
not actively supported since 2012
方法五:使用spring-boot-maven-plugin
能同時(shí)打可執(zhí)行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>
兩個(gè)重點(diǎn):
-
goal
要寫(xiě)成repackage
-
classifier
要寫(xiě)成spring-boot
優(yōu)點(diǎn)
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
缺點(diǎn)
添加了一些不必要的Spring和Spring Boot依賴(lài)
方法六:使用tomcat7-maven-plugin
可打包成一個(gè)web工程類(lèi)型的jar包。其實(shí)是內(nèi)嵌了一個(gè)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.
優(yōu)點(diǎn)
只有一個(gè)jar包
缺點(diǎn)
打包出的文件很大倔监。因?yàn)槔锩鎯?nèi)嵌了一個(gè)tomcat