Maven Assembly實戰(zhàn)教程
Assembly插件
Maven Assembly插件用于創(chuàng)建項目的可分發(fā)包,如JAR、ZIP或TAR文件。
它可以將項目的代碼、依賴項、資源文件打包在一起,方便部署和發(fā)布。
常見用途包括生成包含所有依賴的JAR文件、創(chuàng)建特定格式的歸檔文件等。
基本配置
在pom.xml中添加Maven Assembly插件的配置:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<descriptors>
<descriptor>src/assembly/your-assembly.xml</descriptor>
</descriptors>
<finalName>${project.artifactId}-${project.version}</finalName>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
重要配置項:
descriptors:指定自定義描述符文件的路徑,允許更靈活的打包方式。finalName:定義生成包的最終名稱。
使用示例
示例1:創(chuàng)建包含依賴的JAR包
使用默認描述符生成包含所有依賴的JAR:
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
運行命令:
mvn clean package
示例2:自定義描述符
創(chuàng)建src/assembly/your-assembly.xml文件:
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
<id>custom</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>${project.build.finalName}.jar</include>
</includes>
</fileSet>
</fileSets>
</assembly>
示例3:多模塊項目打包
在父模塊的pom.xml中配置Assembly插件,并為每個子模塊定義打包策略。
實戰(zhàn) _qiwenfile
結構

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>
<parent>
<groupId>com.qiwenshare</groupId>
<artifactId>qiwenshare</artifactId>
<version>1.2.8</version>
</parent>
<artifactId>qiwen-file</artifactId>
<version>1.2.8-SNAPSHOT</version>
<name>qiwen-file</name>
<packaging>jar</packaging>
<properties>
<release-path>target/../release</release-path>
<app-name>${project.artifactId}-${project.version}</app-name>
</properties>
<dependencies>
......省略
</dependencies>
<build>
<plugins>
<!--排除靜態(tài)文件-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<!-- 添加index則不從mainfest中讀取classpath,而是從Index.list中讀取 -->
<!-- <index>true</index> -->
<manifest>
<mainClass>com.qiwenshare.file.FileApplication</mainClass>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
<manifestEntries>
<Class-Path>./</Class-Path>
</manifestEntries>
</archive>
<excludes>
<exclude>static/**</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<!-- not append assembly id in release file name -->
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>src/main/resources/build/assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<!--ant插件執(zhí)行自定義動作-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<delete dir="${release-path}" />
<copy todir="${release-path}" >
<fileset dir="target/${app-name}/${app-name}">
<exclude name="**/*-android-*.jar"/>
</fileset>
</copy>
</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
主要配置了三個Maven插件來實現(xiàn)項目構建過程中的特定任務:
maven-jar-plugin:
- 配置生成的JAR文件的MANIFEST文件,指定主類為
com.qiwenshare.file.FileApplication,并添加類路徑前綴lib/。 - 排除靜態(tài)文件(static/**)不被打包進JAR文件。
maven-assembly-plugin:
- 配置在打包階段執(zhí)行,生成發(fā)布文件時不在文件名后追加assembly ID。
- 使用
src/main/resources/build/assembly.xml作為描述符文件來定義打包規(guī)則。
maven-antrun-plugin:
- 在打包階段執(zhí)行自定義的Ant任務,刪除
${release-path}目錄,然后將目標目錄中的文件(排除特定的JAR文件)復制到${release-path}目錄

- assembly.xml
<assembly>
<!-- 定義組裝標識符 -->
<id>assembly</id>
<!-- 指定輸出格式,此處為目錄格式 -->
<formats>
<format>dir</format>
</formats>
<!-- 是否包含基礎目錄 -->
<includeBaseDirectory>true</includeBaseDirectory>
<!-- 定義文件集合 -->
<fileSets>
<!-- 定義第一個文件集 -->
<fileSet>
<!-- 源目錄為src/main/script -->
<directory>src/main/script</directory>
<!-- 輸出目錄為bin,并設置文件模式為0755 -->
<outputDirectory>bin</outputDirectory>
<fileMode>0755</fileMode>
<!-- 包含所有文件和目錄 -->
<includes>
<include>*.*</include>
</includes>
</fileSet>
<!-- 定義第二個文件集 -->
<fileSet>
<!-- 源目錄為src/main/resources -->
<directory>src/main/resources</directory>
<!-- 輸出目錄為conf,并設置文件模式為0644 -->
<outputDirectory>conf</outputDirectory>
<fileMode>0644</fileMode>
<!-- 排除static目錄下的所有內容 -->
<excludes>
<exclude>static/**</exclude>
</excludes>
</fileSet>
<!-- 定義第三個文件集 -->
<fileSet>
<!-- 源目錄為src/main/resources/static -->
<directory>src/main/resources/static</directory>
<!-- 輸出目錄為static,并設置文件模式為0644 -->
<outputDirectory>static</outputDirectory>
<fileMode>0644</fileMode>
</fileSet>
<!-- 定義第四個文件集,用于復制本工程的jar文件 -->
<fileSet>
<!-- 源目錄為target -->
<directory>target</directory>
<!-- 輸出目錄為lib,并包含所有jar文件 -->
<outputDirectory>lib</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
</fileSets>
<!-- 定義依賴集合 -->
<dependencySets>
<dependencySet>
<!-- 依賴輸出目錄為lib -->
<outputDirectory>lib</outputDirectory>
<!-- 不使用項目自身的主要工件 -->
<useProjectArtifact>false</useProjectArtifact>
<!-- 使用項目附件 -->
<useProjectAttachments>true</useProjectAttachments>
<!-- 僅包含運行時范圍的依賴 -->
<scope>runtime</scope>
</dependencySet>
</dependencySets>
</assembly>
格式設置:指定輸出格式為目錄(dir)。
基礎目錄:包含基礎目錄(includeBaseDirectory)。
文件集:
- 將src/main/script目錄下的所有文件復制到bin目錄,文件模式為0755。
- 將src/main/resources目錄下的文件(排除static目錄)復制到conf目錄,文件模式為0644。
- 將src/main/resources/static目錄下的文件復制到static目錄,文件模式為0644。
- 將target目錄下的JAR文件復制到lib目錄。
依賴集:
將運行時依賴項復制到lib目錄,不包含項目自身的JAR文件,但包含項目的附件。

觸發(fā)腳本
- install.bat
set settingDir=src/main/resources/build/settings.xml mvn clean install -s %settingDir% pause
自行這個BAT腳本,就會生成

- install.sh
#/*************************************************
#* install.sh write by echo at Changsha. Hunan, 2021年 05月 24日 星期一 11:33:25 CST
#*************************************************/
#!/bin/sh
function echo_dbg_p(){
echo "echo_dbg, $@"
}
function usage(){
echo -e "usages: $0 [H|h|help] [-h] [-s]
[H|h|help]: check the usages\n
[]"
}
#main
#maven install check
cmd_package=yum
if ! mvn -v >/dev/null;then
sudo $cmd_package install -y maven
fi
#java install check
if ! java -version &>/dev/null;then
sudo $cmd_package install -y java
fi
if ! mysql -V>/dev/null;then
sudo wget https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm;
sudo rpm -ivh mysql57-community-release-el7-9.noarch.rpm
sudo yum install -y mysql-server
fi
#build path check
#build_root_path=./
settingDir=file-common/src/main/resources/conf/settings.xml
mvn clean install -s $settingDir
sed -i "s#D:/temp_db#/tmp/#g" release/conf/config/application-dev.properties
echo_dbg_p "warning, PLS create mysql with name file, and set the password follow the file qiwen-file/file-web/src/main/resources/config/application-prod.properties"
case $1 in
H|h|help)
usage
;;
*)
# getopts :s:h表示這個命令接受2個帶參數(shù)選項,分別是-h和-s
while getopts :s:h opt
do
case $opt in
s)
echo "-s=$OPTARG"
;;
:)
echo "-$OPTARG needs an argument"
;;
h)
echo "-h is set"
;;
*)
echo "-$opt not recognized"
;;
esac
done
;;
esac
檢查并安裝Maven:
- 使用mvn -v命令檢查Maven是否已安裝。
- 如果未安裝,使用sudo yum install -y maven命令安裝Maven。
檢查并安裝Java:
- 使用java -version命令檢查Java是否已安裝。
- 如果未安裝,使用sudo yum install -y java命令安裝Java。
檢查并安裝MySQL:
- 使用mysql -V命令檢查MySQL是否已安裝。
- 如果未安裝,下載MySQL的社區(qū)版本RPM包并安裝,然后使用sudo yum install -y mysql-server命令安裝MySQL服務器。
構建項目:
- 使用Maven清理并安裝項目,指定設置文件路徑。
- 修改配置文件release/conf/config/application-dev.properties中的路徑。
幫助信息:
- 如果第一個參數(shù)為H, h, 或help,則顯示使用說明。
解析命令行參數(shù):
- 使用getopts解析命令行參數(shù)-s和-h。
- 根據(jù)解析結果執(zhí)行相應的操作。
實戰(zhàn) _nacos

- release-nacos.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 1999-2018 Alibaba Group Holding Ltd.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<assembly>
<!-- 定義組裝標識,使用項目版本號 -->
<id>server-${project.version}</id>
<!-- 是否包含基礎目錄 -->
<includeBaseDirectory>true</includeBaseDirectory>
<!-- 定義打包格式 -->
<formats>
<format>dir</format>
<format>tar.gz</format>
<format>zip</format>
</formats>
<!-- 定義文件集合 -->
<fileSets>
<!-- 包含plugins目錄下的所有內容 -->
<fileSet>
<includes>
<include>plugins/**</include>
</includes>
</fileSet>
<!-- 包含conf目錄下的所有內容 -->
<fileSet>
<includes>
<include>conf/**</include>
</includes>
</fileSet>
<!-- 包含bin目錄下的所有文件,并設置文件權限 -->
<fileSet>
<includes>
<include>bin/*</include>
</includes>
<fileMode>0755</fileMode>
</fileSet>
</fileSets>
<!-- 定義單獨的文件 -->
<files>
<!-- 將LICENSE-BIN文件重命名為LICENSE -->
<file>
<source>LICENSE-BIN</source>
<destName>LICENSE</destName>
</file>
<!-- 將NOTICE-BIN文件重命名為NOTICE -->
<file>
<source>NOTICE-BIN</source>
<destName>NOTICE</destName>
</file>
<!-- 打好的jar包名稱和放置目錄 -->
<file>
<source>../console/target/nacos-server.jar</source>
<outputDirectory>target/</outputDirectory>
</file>
</files>
<!-- 定義模塊集合 -->
<moduleSets>
<moduleSet>
<!-- 是否使用所有反應堆項目 -->
<useAllReactorProjects>true</useAllReactorProjects>
<!-- 定義包含的模塊 -->
<includes>
<include>com.alibaba.nacos:nacos-console</include>
</includes>
</moduleSet>
</moduleSets>
</assembly>
輸出 zip / tar.gz


常見問題及解決方案
- 插件未執(zhí)行:確保在
executions中定義了正確的phase和goal。 - 依賴沖突:檢查依賴版本,確保沒有沖突,必要時使用
dependencyManagement來管理版本。 - 文件未打包:確認
fileSets配置的路徑和規(guī)則是否正確。
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
詳解Spring Boot 自定義PropertySourceLoader
這篇文章主要介紹了詳解Spring Boot 自定義PropertySourceLoader,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05
Java ArrayList與Vector和LinkedList的使用及源碼分析
ArrayList、Vector、LinkedList類均在java.util包中,均為可伸縮數(shù)組,即可以動態(tài)改變長度的數(shù)組。ArrayList 和 Vector都是基于存儲元素的Object[] array來實現(xiàn)的,它們會在內存中開辟一塊連續(xù)的內存來存儲2022-11-11
Mybatis?Plus插入數(shù)據(jù)后獲取新數(shù)據(jù)id值的踩坑記錄
在某些情況下,需要在執(zhí)行新增后,需要獲取到新增行的id,這篇文章主要給大家介紹了關于Mybatis?Plus插入數(shù)據(jù)后獲取新數(shù)據(jù)id值的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-08-08

