当前位置: 首页 > news >正文

Maven - Assembly实战

文章目录

  • Pre
  • Assembly插件
  • 基本配置
  • 使用示例
    • 示例1:创建包含依赖的JAR包
    • 示例2:自定义描述符
    • 示例3:多模块项目打包
  • 实战 _qiwenfile
    • 结构
    • pom.xml
    • 触发脚本
  • 实战 _nacos
  • 输出 zip / tar.gz
  • 常见问题及解决方案

在这里插入图片描述


Pre

Spring Boot - 瘦身大作战:优雅应对Spring Boot Fat Jar

Maven - 打包之争:Jar vs. Shade vs. Assembly


Assembly插件

Maven Assembly插件用于创建项目的可分发包,如JAR、ZIP或TAR文件。它可以将项目的代码、依赖项、资源文件打包在一起,方便部署和发布。常见用途包括生成包含所有依赖的JAR文件、创建特定格式的归档文件等。


基本配置

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:创建包含依赖的JAR包

使用默认描述符生成包含所有依赖的JAR:

<descriptorRefs><descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>

运行命令:

mvn clean package

示例2:自定义描述符

创建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插件,并为每个子模块定义打包策略。


实战 _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><!--排除静态文件--><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插件执行自定义动作--><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插件来实现项目构建过程中的特定任务:

  1. maven-jar-plugin:
    配置生成的JAR文件的MANIFEST文件,指定主类为com.qiwenshare.file.FileApplication,并添加类路径前缀lib/。
    排除静态文件(static/**)不被打包进JAR文件。

  2. maven-assembly-plugin:
    配置在打包阶段执行,生成发布文件时不在文件名后追加assembly ID。使用src/main/resources/build/assembly.xml作为描述符文件来定义打包规则。

  3. maven-antrun-plugin:
    在打包阶段执行自定义的Ant任务,删除${release-path}目录,然后将目标目录中的文件(排除特定的JAR文件)复制到${release-path}目录

配置归档信息
排除静态文件
配置打包文件名
删除目标目录
开始
maven-jar-plugin配置
指定主类和类路径前缀
maven-assembly-plugin配置
使用自定义assembly描述符
maven-antrun-plugin配置
复制文件到目标目录
结束

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文件,但包含项目的附件。
读取配置
解析文件集
处理第一个文件集
处理第二个文件集
处理第三个文件集
处理第四个文件集
解析依赖集
复制依赖库
开始
读取assembly配置
处理文件集
复制脚本文件到bin目录
复制资源文件到conf目录
复制静态文件到static目录
复制JAR文件到lib目录
处理依赖集
复制依赖库到lib目录
结束

触发脚本

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;thensudo $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.rpmsudo yum install -y mysql-server
fi
#build path check
#build_root_path=./
settingDir=file-common/src/main/resources/conf/settings.xmlmvn 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 inH|h|help)usage;;*)
# getopts :s:h表示这个命令接受2个带参数选项,分别是-h和-swhile getopts :s:h optdo  case $opt ins)echo "-s=$OPTARG";;:)echo "-$OPTARG needs an argument";;h)echo "-h is set";;*)echo "-$opt not recognized";;esacdone;;
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的社区版本RPM包并安装,然后使用sudo yum install -y mysql-server命令安装MySQL服务器。

构建项目:
使用Maven清理并安装项目,指定设置文件路径。
修改配置文件release/conf/config/application-dev.properties中的路径。

帮助信息:
如果第一个参数为H, h, 或help,则显示使用说明。

解析命令行参数:
使用getopts解析命令行参数-s和-h。
根据解析结果执行相应的操作。

Java已安装
Java未安装
JAVA_HOME已设置
JAVA_HOME未设置
应用未启动
应用已启动
端口未占用
端口已占用
成功
失败
开始
设置环境变量
获取目录路径
读取配置文件
设置日志路径
检查Java环境
设置JVM参数
检查JAVA_HOME
设置JAVA_HOME
退出并提示
防止重复启动
检查端口占用
退出并提示
构建启动命令
退出并提示
启动应用
等待应用启动
应用启动成功
结束
继续等待

实战 _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>
解析基本信息
dir
tar.gz
zip
plugins
conf
bin
LICENSE-BIN
NOTICE-BIN
nacos-server.jar
com.alibaba.nacos:nacos-console
开始
解析assembly配置
设置ID和基础目录
解析打包格式
添加dir格式
添加tar.gz格式
添加zip格式
解析文件集
包含plugins目录
包含conf目录
包含bin目录并设置权限
解析单个文件
包含并重命名LICENSE
包含并重命名NOTICE
包含nacos-server.jar
解析模块集
包含nacos-console模块
结束

输出 zip / tar.gz

在这里插入图片描述
在这里插入图片描述


常见问题及解决方案

  • 插件未执行:确保在executions中定义了正确的phasegoal
  • 依赖冲突:检查依赖版本,确保没有冲突,必要时使用dependencyManagement来管理版本。
  • 文件未打包:确认fileSets配置的路径和规则是否正确。

在这里插入图片描述


http://www.mrgr.cn/news/55942.html

相关文章:

  • 尚硅谷大数据Flink1.17实战教程-笔记04【Flink DataStream API】
  • RabbitMQ 入门(八)SpringAMQP消息转换器
  • 接口自动化测试实战
  • nbsaas vue3管理后台框架
  • RestHighLevelClient操作es查询文档
  • C/C++ 每日一练:单链表的反转
  • 【3D】几何算法合集2(自用资源)方便cv
  • 04_Pandas高级操作(上)
  • 【Docker】在AlmaLinux 8.10系统中安装Docker-ce过程分享
  • 自学网络安全?一般人我还是劝你算了吧
  • Tkinter中grid设置位置
  • 创建 RpcThreadPoolUtil 工具类
  • 基于Consul的 Redis 多实例监控方案
  • 【Dv2Admin】软删除的修改与恢复
  • 第6天:Intent和页面导航-补充材料——`MainActivity.kt`解读
  • SpringBoot 面试常见问答总结(一)
  • Ovito旋转模型的技巧
  • 【已解决】【Hadoop】 Shell命令易错点及解决方法
  • MRU烟气分析仪维修VARIOplus工业气体分析仪修理
  • 【前端构建】Snowpack: 现代化的前端构建工具
  • SAR ADC面试攻略:关键技术解析与面试问题全攻略
  • 使用GraphRAG系统实现本地部署的Ollama模型问答系统
  • 华为OD机试真题---游戏分组
  • 网络编程中容易踩的坑罗列,谨记!
  • Python 代码读取新闻语料文件,并进行新闻主题的统计分析
  • Linux 定时任务 (Crontab)