Maven 如何打包可執行jar包
阿新 • • 發佈:2019-02-19
目標
解決可執行jar包的打包問題;上一篇文章我們介紹了mvn package可以打包,那麼如果是一個可執行的jar包,該如何打包呢?
可以執行jar
擁有函式入口(public static void main(String[] args) {}),我們可以通過java -jar xxx.jar 來執行進入這個main函式。打成可執行效果,是通過mainfest.mf檔案來指定的。因此如果不依賴其他工具進行打包,需要手動新增 MANIFEST.MF 到 META-INF/MANIFEST.MF
例如:
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: wangzhiping
Created-By: Apache Maven 3.1 .0
Build-Jdk: 1.8.0_101
Main-Class: wzp.study.maven.mainclass.HelloWorld
maven 打包可執行jar
maven-jar-plugin
- pom.xml 配置
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
<modelVersion>4.0.0</modelVersion>
<groupId>study.wzp.maven</groupId>
<artifactId>maven-hello</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>maven-hello</name>
<dependencies>
<dependency >
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<!-- META-INF/MANIFEST.MF 新增 Main-Class: -->
<mainClass>study.wzp.maven.App</mainClass>
<!-- META-INF/MANIFEST.MF 新增 ClassPath: 外部依賴指定 -->
<addClasspath>true</addClasspath>
<!-- META-INF/MANIFEST.MF : 指定依賴包所在目錄字首 -->
<classpathPrefix>/lib</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
<!-- 自動實現將依賴拷貝到 lib 目錄下,不然需要手動的執行-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<!-- ${project.build.directory} 這是專案屬性,後續篇章會有講述 -->
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
- MANIFEST.MF
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: wangzhiping
Class-Path: /lib/junit-4.10.jar /lib/hamcrest-core-1.1.jar
Created-By: Apache Maven 3.1.0
Build-Jdk: 1.8.0_101
Main-Class: study.wzp.maven.App
- 生成的目錄
使用maven-jar-plugin,存在外部依賴時,需要指定外部依賴的位置(建議使用:maven-dependency-plugin)幫助管理,不能打包在一塊,感覺不是很方便。
maven-shade-plugin
- pom.xml 配置
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>study.wzp.maven</groupId>
<artifactId>maven-hello</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>maven-hello</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>study.wzp.maven.App</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
- MANIFEST.MF
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: wangzhiping
Created-By: Apache Maven 3.1.0
Build-Jdk: 1.8.0_101
Main-Class: study.wzp.maven.App
- 專案目錄
會生成maven-hello-1.0-SNAPSHOT.jar 和 origin-maven-hello-1.0-SNAPSHOT.jar 兩個打包檔案,
1、maven-hello-1.0-SNAPSHOT.jar :將依賴一起合併打包的jar;
2、origin-maven-hello-1.0-SNAPSHOT.jar:不包含任何依賴的jar,如果存在依賴,那麼這個檔案會報錯;
maven-assembly-plugin
- pom.xml 配置
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>study.wzp.maven.App</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
- 專案目錄
與shade相比,基本一致,會生成兩個檔案
1、maven-hello-1.0-SNAPSHOT.jar:無依賴關係,不可執行;
2、maven-hello-1.0-SNAPSHOT-jar-with-dependencies.jar:依賴合併打包,可執行。
總結
如果打包可執行jar,建議使用maven-shade-plugin或者maven-assembly-plugin,一起打包會更好些。可以直接執行。