Maven將代碼及依賴打成一個Jar包的方式詳解(最新推薦)
Maven可以使用mvn package指令對項目進行打包,如果使用java -jar xxx.jar執(zhí)行運行jar文件,會出現(xiàn)"no main manifest attribute, in xxx.jar"(沒有設(shè)置Main-Class)、ClassNotFoundException(找不到依賴包)等錯誤。
要想jar包能直接通過java -jar xxx.jar運行,需要滿足:
1、在jar包中的META-INF/MANIFEST.MF中指定Main-Class,這樣才能確定程序的入口在哪里;
2、要能加載到依賴包。
使用Maven有以下幾種方法可以生成能直接運行的jar包并且是打成一個jar包,可以根據(jù)需要選擇一種合適的方法。
方法一:使用maven-assembly-plugin插件打包
在pom.xml中配置:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.5.5</version> <configuration> <archive> <manifest> <mainClass>com.xxg.Main</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </plugin> </plugins> </build>
打包方式: mvn package assembly:single(在eclipse中執(zhí)行maven build…… ,然后在Goals中輸入 assembly:single)
打包后會在target目錄下生成一個xxx-jar-with-dependencies.jar文件,這個文件不但包含了自己項目中的代碼和資源,還包含了所有依賴包的內(nèi)容。所以可以直接通過java -jar來運行。
此外還可以直接通過mvn package來打包(在eclipse中直接執(zhí)行maven install),無需assembly:single,不過需要加上一些配置:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<configuration>
<archive>
<manifest>
<mainClass>com.xxg.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<!-- 這個jar-with-dependencies是assembly預(yù)先寫好的一個,組裝描述引用 -->
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<!--工程名-->
<finalName>${project.name}</finalName>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>其中<phase>package</phase>、<goal>single</goal>即表示在執(zhí)行package打包時,執(zhí)行assembly:single,所以可以直接使用mvn package打包。
不過,如果項目中用到Spring Framework,用這種方式打出來的包運行時會出錯,使用下面的方法二可以處理。
方法二:使用maven-shade-plugin插件打包
在pom.xml中配置:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.4.1</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>com.xxg.Main</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build>
配置完成后,執(zhí)行mvn package即可打包。在target目錄下會生成兩個jar包,注意不是original-xxx.jar文件,而是另外一個。和maven-assembly-plugin一樣,生成的jar文件包含了所有依賴,所以可以直接運行。
如果項目中用到了Spring Framework,將依賴打到一個jar包中,運行時會出現(xiàn)讀取XML schema文件出錯。原因是Spring Framework的多個jar包中包含相同的文件spring.handlers和spring.schemas,如果生成一個jar包會互相覆蓋。為了避免互相影響,
可以使用AppendingTransformer來對文件內(nèi)容追加合并:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.4.1</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>com.xxg.Main</mainClass> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/spring.handlers</resource> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/spring.schemas</resource> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build>
方法三:使用maven-jar-plugin和maven-dependency-plugin插件打包
原理是通過修改maven 打jar包的maven-jar-plugin插件的配置信息來生成我們需要的指定依賴的可執(zhí)行jar包。
在項目的pom.xml文件中修改默認的jar插件
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<archive>
<manifest>
<!--運行jar包時運行的主類,要求類全名-->
<mainClass>com.hafiz.Runner</mainClass>
<!-- 是否指定項目classpath下的依賴 -->
<addClasspath>true</addClasspath>
<!-- 指定依賴的時候聲明前綴 -->
<classpathPrefix>./lib/</classpathPrefix>
<!--依賴是否使用帶有時間戳的唯一版本號,如:xxx-1.3.0-20121225.012733.jar-->
<useUniqueVersions>false</useUniqueVersions>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<!--接著我們還要配置maven的maven-dependency-plugin插件把當前項目的所有依賴放到target目錄下的lib文件夾下-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>其中,${project.build.directory}表示默認的target文件夾。
我們通過上文的修改便完成了適用maven生成指定依賴的可執(zhí)行jar包。
我們發(fā)現(xiàn)生成的manifest文件中已經(jīng)設(shè)置好了Main-Class以及Class-Path,如下:

如果<addClasspath>設(shè)置為false,則生成的manifest文件中不會聲明依賴(即不會有Class-Path聲明)

程序主類如下:
package com.hafiz;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Desc:主類
* Created by hafiz.zhang on 2018/4/07.
*/
public class Runner {
public static void main(String[] args) {
ApplicationContext context
= new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
System.out.println("context:" + context.getClass());
System.out.println("The Main Class Is Running....");
}
}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>com.hafiz</groupId>
<artifactId>assembly-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>assembly-demo</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.2.6.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<archive>
<manifest>
<!--運行jar包時運行的主類,要求類全名-->
<mainClass>com.hafiz.Runner</mainClass>
<!-- 是否指定項目classpath下的依賴 -->
<addClasspath>true</addClasspath>
<!-- 指定依賴的時候聲明前綴 -->
<classpathPrefix>./lib/</classpathPrefix>
<!--依賴是否使用帶有時間戳的唯一版本號,如:xxx-1.3.0-20121225.012733.jar-->
<useUniqueVersions>false</useUniqueVersions>
</manifest>
</archive>
</configuration>
</plugin>
<!--把當前項目所有的依賴打包到target目錄下的lib文件夾下-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<!--已存在的Release版本不重復(fù)copy-->
<overWriteReleases>false</overWriteReleases>
<!--已存在的SnapShot版本不重復(fù)copy-->
<overWriteSnapshots>false</overWriteSnapshots>
<!--不存在或者有更新版本的依賴才copy-->
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>我們現(xiàn)在進入生成的jar包所在的文件夾下,使用 java -jar xxx.jar來執(zhí)行生成的jar包

到此我們就完成了如何使用maven的jar包生成插件來進行生成指定依賴的可執(zhí)行jar包。
代碼Github地址:https://github.com/hafizzhang/assembly-demo.git
參考網(wǎng)址:
https://blog.csdn.net/xiao__gui/article/details/47341385
http://www.cnblogs.com/hafiz/p/6538107.html
https://www.cnblogs.com/hafiz/p/6538332.html
到此這篇關(guān)于Maven將代碼及依賴打成一個Jar包的方式的文章就介紹到這了,更多相關(guān)maven將依賴打入jar包內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java使用NIO優(yōu)化IO實現(xiàn)文件上傳下載功能
IO 是基于流來讀取的,而NIO則是基于塊讀取,面向流 的 I/O 系統(tǒng)一次一個字節(jié)地處理數(shù)據(jù),這篇文章主要介紹了Java使用NIO優(yōu)化IO實現(xiàn)文件上傳下載功能,需要的朋友可以參考下2022-07-07
SpringBoot登錄驗證token攔截器的實現(xiàn)
本文主要介紹了SpringBoot登錄驗證token攔截器的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-07-07

