Maven 生成打包可執(zhí)行jar包的方法步驟
最近IDEA打可執(zhí)行Jar包搞了三天,一直失敗,好好學(xué)習(xí)一下Maven-assembly,在此記錄一下
1. 需求
項(xiàng)目打包,滿足以下要求:
1.整個(gè)項(xiàng)目打一個(gè)Zip包下面包括應(yīng)用程序、應(yīng)用程序依賴的jar包、說明文檔
2.項(xiàng)目打的jar包可以執(zhí)行不同類里的Main函數(shù)
3.項(xiàng)目源碼打的jar包要與依賴的第三方j(luò)ar包分開
4.項(xiàng)目里的執(zhí)行腳本也要一塊打包并進(jìn)行分類
5.document目錄下的readme.txt放在壓縮包的根目錄下,其他的還放在這個(gè)目錄下
6.打的jar包去掉不需要的目錄(文件)
2. 開發(fā)環(huán)境
IDEA-2016 Maven3.3.9
項(xiàng)目的目錄結(jié)構(gòu):

3. Maven打包插件介紹
assembly翻譯過來就是組裝、裝配的意思
Maven對項(xiàng)目打包常用的打包插件有三種,分別是:
| 插件 | 功能 |
|---|---|
| maven-jar-plugin | maven 默認(rèn)打包插件,用來創(chuàng)建 project jar |
| maven-shade-plugin | 打可執(zhí)行包,executable(fat) jar |
| maven-assembly-plugin | 支持自定義打包方式 |
這里使用maven-jar-plugin和maven-assembly-plugin
項(xiàng)目目錄:
每次找jar包之前先clean一下,不然的話IDEA會認(rèn)為你的項(xiàng)目沒有修改而不重新加載
另:配置文件的注釋已經(jīng)很詳細(xì)了,這里就不另外再說明了
4. Maven使用maven-jar-plugin打可執(zhí)行jar包
主要配置如下:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<!-- 對要打的jar包進(jìn)行配置 -->
<configuration>
<!-- Configuration of the archiver -->
<archive>
<!--生成的jar中,不要包含pom.xml和pom.properties這兩個(gè)文件-->
<addMavenDescriptor>false</addMavenDescriptor>
<!-- Manifest specific configuration -->
<manifest>
<!--是否要把第三方j(luò)ar放到manifest的classpath中-->
<addClasspath>true</addClasspath>
<!--生成的manifest中classpath的前綴,
因?yàn)橐训谌絡(luò)ar放到lib目錄下,
所以classpath的前綴是lib/-->
<classpathPrefix>lib/</classpathPrefix>
</manifest>
</archive>
<!--過濾掉不希望包含在jar中的文件-->
<excludes>
<!-- 排除不需要的文件夾(路徑是jar包內(nèi)部的路徑) -->
<exclude>**/assembly/</exclude>
</excludes>
</configuration>
</plugin>
完整配置見底部
5. Maven使用maven-assembly-plugin裝需要打包的文件打進(jìn)zip包
pom.xml下的主要配置如下:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<!-- 對項(xiàng)目的組裝進(jìn)行配置 -->
<configuration>
<!-- 指定assembly插件的配置文件所在位置 -->
<descriptors>
<descriptor>src/main/resources/assembly/package.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!-- 將組裝綁定到maven生命周期的哪一階段 -->
<phase>package</phase>
<goals>
<!-- 指定assembly插件的打包方式-->
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
assembly插件的配置文件package.xml見底部
6. Maven生成可執(zhí)行jar包及zip項(xiàng)目壓縮包
雙擊執(zhí)行mvn:package會生成兩個(gè)包:可執(zhí)行jar包和項(xiàng)目壓縮包,因?yàn)閍ssembly的裝配配置的是綁定到這上面來的
雙擊執(zhí)行assembly:single只生成項(xiàng)目壓縮包

這里執(zhí)行mvn:package

解壓后的項(xiàng)目壓縮包目錄結(jié)構(gòu):

7. 執(zhí)行jar包
解壓縮生成的項(xiàng)目包
TestString的源碼:
public class TestString {
public static void main(String[] args) {
String[] arr = new String[]{"aaa", "bbb", "ccc", "DDD", "EEE", "FFF"};
System.out.println(StringUtils.join(arr, "---"));
}
}
TestNumber的源碼:
public class TestNumber {
public static void main(String[] args) {
Integer[] arr = new Integer[]{11, 22, 33, 44, 55, 66};
System.out.println(StringUtils.join(arr, "---"));
}
}
命令行運(yùn)行生成的jar
java -classpath dong.jar com.dong.bigdata.TestString java -classpath dong.jar com.dong.bigdata.TestNumber
運(yùn)行結(jié)果:

8. pom.xml配置
包含兩個(gè)文件:
pom.xml整體的配置
package.xml包含在pom.xml中,用于指定assembly裝配時(shí)的配置
pom.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<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>
<!-- ####################### 基礎(chǔ)設(shè)置 ###################### -->
<!--groupId:項(xiàng)目或者組織的唯一標(biāo)志,并且配置時(shí)生成路徑也是由此生成,如org.myproject.mojo生成的相對路徑為:/org/myproject/mojo-->
<groupId>com.dong</groupId>
<!--項(xiàng)目的通用名稱-->
<artifactId>bigdata</artifactId>
<!--打包機(jī)制,如pom,jar,maven-plugin,ejb,war,ear,rar,par-->
<packaging>jar</packaging>
<!--項(xiàng)目的版本-->
<version>1.0-SNAPSHOT</version>
<!-- ####################### 項(xiàng)目信息 ###################### -->
<!--用戶描述項(xiàng)目的名稱,無關(guān)緊要的東西-->
<name>bigdata</name>
<!--寫明開發(fā)團(tuán)隊(duì)的網(wǎng)站,無關(guān)緊要-->
<url>http://http://www.dong.com/.com</url>
<!-- ####################### 環(huán)境設(shè)置 ###################### -->
<properties>
<!-- 項(xiàng)目執(zhí)行腳本目錄 -->
<project.script.execute.directory>src/main/scripts/execute</project.script.execute.directory>
<!-- 項(xiàng)目說明文檔目錄 -->
<project.document.directory>document</project.document.directory>
<!-- 項(xiàng)目配置文件目錄 -->
<project.config.directory>src/main/resources</project.config.directory>
<!-- 項(xiàng)目編碼 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- 本地編譯JDK版本 -->
<maven.compiler.source>1.8</maven.compiler.source>
<!-- 項(xiàng)目部署JDK版本 -->
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<!--
配置Maven的倉庫, 在此處配置的倉庫會優(yōu)先于setting.xml里配置的倉庫,
建議哪個(gè)倉庫快,哪個(gè)配置在前面, 然后如果Maven在前面配置的倉庫找不到的話會去后面的倉庫找,
如果后面的倉庫都找不到,會去setting.xml中央倉庫里找
-->
<repositories>
<!-- 阿里云倉庫,配置Maven倉庫,速度快配置在最前面 -->
<repository>
<id>aliyun</id>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</repository>
<!-- 國內(nèi)備選倉庫 -->
<repository>
<id>repo2</id>
<url>http://repo2.maven.org/maven2/</url>
</repository>
<!-- Cloudera倉庫,如果在阿里云倉庫里找不到去Cloudera的倉庫里找,主要是CDH版本Hadoop依賴的jar -->
<repository>
<id>cloudera</id>
<url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
</repository>
<!-- Scala倉庫,如果前面兩個(gè)都找不到來倉庫找,如果此倉庫也找不到,去中央倉庫找 -->
<repository>
<id>scala-tools.org</id>
<name>Scala-Tools Maven2 Repository</name>
<url>http://scala-tools.org/repo-releases</url>
</repository>
</repositories>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
</dependencies>
<build>
<finalName>dong</finalName>
<plugins>
<!-- The configuration of maven-jar-plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<!-- 對要打的jar包進(jìn)行配置 -->
<configuration>
<!-- Configuration of the archiver -->
<archive>
<!--生成的jar中,不要包含pom.xml和pom.properties這兩個(gè)文件-->
<addMavenDescriptor>false</addMavenDescriptor>
<!-- Manifest specific configuration -->
<manifest>
<!--是否要把第三方j(luò)ar放到manifest的classpath中-->
<addClasspath>true</addClasspath>
<!--
生成的manifest中classpath的前綴,
因?yàn)橐训谌絡(luò)ar放到lib目錄下,
所以classpath的前綴是lib/
-->
<classpathPrefix>lib/</classpathPrefix>
</manifest>
</archive>
<!--過濾掉不希望包含在jar中的文件-->
<excludes>
<!-- 排除不需要的文件夾(路徑是jar包內(nèi)部的路徑) -->
<exclude>**/assembly/</exclude>
</excludes>
</configuration>
</plugin>
<!-- The configuration of maven-assembly-plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<!-- 對項(xiàng)目的組裝進(jìn)行配置 -->
<configuration>
<!-- 指定assembly插件的配置文件所在位置 -->
<descriptors>
<descriptor>src/main/resources/assembly/package.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!-- 將組裝綁定到maven生命周期的哪一階段 -->
<!--<phase>package</phase>-->
<goals>
<!-- 指定assembly插件的打包方式-->
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
9. package.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<assembly>
<id>full</id>
<!-- 最終打包成一個(gè)用于發(fā)布的zip文件 -->
<formats>
<format>zip</format>
</formats>
<!-- 把依賴jar包打包進(jìn)Zip壓縮文件的lib目錄下 -->
<dependencySets>
<dependencySet>
<!--不使用項(xiàng)目的artifact,第三方j(luò)ar不要解壓,打包進(jìn)zip文件的lib目錄-->
<useProjectArtifact>false</useProjectArtifact>
<!-- 第三方j(luò)ar打包進(jìn)Zip文件的lib目錄下, -->
<!-- 注意此目錄要與maven-jar-plugin中classpathPrefix指定的目錄相同, -->
<!-- 不然這些依賴的jar包加載到ClassPath的時(shí)候會找不到-->
<outputDirectory>lib</outputDirectory>
<!-- 第三方j(luò)ar不要解壓-->
<!--<unpack>false</unpack>-->
</dependencySet>
</dependencySets>
<!-- 文件設(shè)置,你想把哪些文件包含進(jìn)去,或者把某些文件排除掉,都是在這里配置-->
<fileSets>
<!-- 把項(xiàng)目自己編譯出來的可執(zhí)行jar,打包進(jìn)zip文件的根目錄 -->
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory></outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
<!--
把項(xiàng)目readme說明文檔,打包進(jìn)zip文件根目錄下
(這里針對目錄document/readme.txt文件)
${projet.document.directory}是pom.xml中自己配置的
-->
<fileSet>
<directoryl>${projet.document.directory}</directoryl>
<outputDirectory></outputDirectory>
<includes>
<include>readme.*</include>
</includes>
</fileSet>
<!--
把項(xiàng)目相關(guān)的說明文檔(除了readme文檔),
打包進(jìn)zip文件根目錄下的document目錄
(這里針對document/exclode.txt文件)
${project.document.directory}是在pom.xml中自己配置的
-->
<fileSet>
<directory>${project.document.directory}</directory>
<outputDirectory>document</outputDirectory>
<excludes>
<exclude>readme.*</exclude>
</excludes>
</fileSet>
<!--
把項(xiàng)目的腳本文件目錄(src/main/scripts )中的啟動腳本文件,
打包進(jìn)zip文件的根目錄
(這里針對的是src/scripts/execute/include-file.sh文件)
${project.script.execute.directory}
-->
<fileSet>
<directory>${project.script.execute.directory}</directory>
<outputDirectory></outputDirectory>
<includes>
<include>*</include>
</includes>
</fileSet>
</fileSets>
</assembly>
到此這篇關(guān)于Maven 生成打包可執(zhí)行jar包的方法步驟的文章就介紹到這了,更多相關(guān)Maven 生成可執(zhí)行jar 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java 反射調(diào)用靜態(tài)方法的簡單實(shí)例
下面小編就為大家?guī)硪黄狫ava 反射調(diào)用靜態(tài)方法的簡單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-06-06
Mybatis實(shí)現(xiàn)數(shù)據(jù)的增刪改查實(shí)例(CRUD)
本篇文章主要介紹了Mybatis實(shí)現(xiàn)數(shù)據(jù)的增刪改查實(shí)例(CRUD),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05
IDEA利用jclasslib 修改class文件的實(shí)現(xiàn)
這篇文章主要介紹了IDEA利用jclasslib 修改class文件的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
Java?List<JSONObject>中的數(shù)據(jù)如何轉(zhuǎn)換為List<T>
這篇文章主要介紹了Java?List<JSONObject>中的數(shù)據(jù)如何轉(zhuǎn)換為List<T>問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-05-05
Spring?this調(diào)用當(dāng)前類方法無法攔截的示例代碼
這篇文章主要介紹了Spring?this調(diào)用當(dāng)前類方法無法攔截,通過debug 查看這個(gè)proxyService1 和this的區(qū)別,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03

