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插件来实现项目构建过程中的特定任务:
maven-jar-plugin:
- 配置生成的jar文件的manifest文件,指定主类为
com.qiwenshare.file.fileapplication
,并添加类路径前缀lib/。 - 排除静态文件(static/**)不被打包进jar文件。
maven-assembly-plugin:
- 配置在打包阶段执行,生成发布文件时不在文件名后追加assembly id。
- 使用
src/main/resources/build/assembly.xml
作为描述符文件来定义打包规则。
maven-antrun-plugin:
- 在打包阶段执行自定义的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文件,但包含项目的附件。
触发脚本
- 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个带参数选项,分别是-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的社区版本rpm包并安装,然后使用sudo yum install -y mysql-server命令安装mysql服务器。
构建项目:
- 使用maven清理并安装项目,指定设置文件路径。
- 修改配置文件release/conf/config/application-dev.properties中的路径。
帮助信息:
- 如果第一个参数为h, h, 或help,则显示使用说明。
解析命令行参数:
- 使用getopts解析命令行参数-s和-h。
- 根据解析结果执行相应的操作。
实战 _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
常见问题及解决方案
- 插件未执行:确保在
executions
中定义了正确的phase
和goal
。 - 依赖冲突:检查依赖版本,确保没有冲突,必要时使用
dependencymanagement
来管理版本。 - 文件未打包:确认
filesets
配置的路径和规则是否正确。
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论