iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >使用maven-assembly-plugin如何打包多模块项目
  • 953
分享到

使用maven-assembly-plugin如何打包多模块项目

2024-04-02 19:04:59 953人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

目录Maven-assembly-plugin打包多模块项目概述1. 需求2. 打包流程使用maven-assembly-plugin插件来定制化打包使用内置的Assembly De

maven-assembly-plugin打包多模块项目

概述

maven-assembly-plugin 是目前maven项目中最常用的打包工具,它便利、配置简单,因此可以满足我们大部分的需求。

实际开发过程中大部分Maven项目都是多模块的,因为工作需要,对其进行了研究与尝试,目前可以将多模块按照自己需求打包到一起。

1. 需求

项目本身代码非常复杂,最深可以到三层模块,即GrandFather -> Parent -> child,要求打包后的结构如下:

​​​​

目录的含义不

再追溯,下面直接将打包方法。

2. 打包流程

2.1 新建打包模块

Maven多模块打包在一起时,需要新建一个专门用于打包的模块,该模块不需要有任何Java代码,只需要配置一些最终形成的环境即可。但是该模块有一个非常重要的要求: 该模块必须是整个项目最后一个进行打包的。

可以通过配置pom.xml中加载模块的顺序来控制,最后配置进入的即最后进行打包的:

本人项目比较复杂,最终配置的打包是在tools子模块下的tools-package模块中进行配置。

2.2 配置打包模块

根据最终生成的路径需求,将所有的相关路径及一些命令、配置文件等按照要求在打包模块中实现,放在resources目录即可。

注:该项目中不需要任何Java代码

2.3 配置打包模块的pom.xml

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4.1</version>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <Goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <finalName>xxxxx</finalName>
                            <descriptors>
                                <descriptor>src/main/resources/assemble/assemble.xml</descriptor>
                            </descriptors>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

2.4 配置其他模块打包方式

因为项目中使用的SpringBootspringBoot也提供了一种打包方式:spring-boot-maven-plugin,这种方式打包时会将该项目中所有依赖的包(包括静态文件)统一打包至一个jar中,这个不是我们项目的需求,我们的需求是将所有的jar包(包括第三方及当前项目的jar包)都放在lib目录,且是单独存在。

因此需要将其他模块的打包配置修改,修改分为两步:

1)不再使用spring-boot-maven-plugin进行打包,即将其从pom.xml中删除;

2)在pom.xml文件中使用maven-jar-plugin进行打包,该打包如何配置请参考文章(待定)。

2.5 配置assemble.xml文件

assemble.xml文件中描述了具体如何打包,该打包过程和实际需求关系密切

<?xml version='1.0' encoding='UTF-8'?>
<assembly xmlns="Http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0
                    http://maven.apache.org/xsd/assembly-1.1.0.xsd">
    <id>${project.version}</id>
    <fORMats>
        <format>tar.gz</format>
    </formats>
    <includeBaseDirectory>true</includeBaseDirectory>
    <moduleSets>
        <moduleSet>
            <useAllReactorProjects>true</useAllReactorProjects>
            <includes>
                <include>xxx.xx.x:base1</include>
                <include>xxx.xx.x:base2</include>
                <include>xxx.xx.x:base3</include>
                <include>xxx.xx.x:base4</include>
            </includes>
            <binaries>
                <outputDirectory>lib</outputDirectory>
                <unpack>false</unpack>
            </binaries>
        </moduleSet>
    </moduleSets>
    <fileSets>
        <fileSet>
            <directory>src/main/resources/bin</directory>
            <outputDirectory>bin</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>src/main/resources/conf</directory>
            <outputDirectory>conf</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>src/main/resources/docs</directory>
            <outputDirectory>docs</outputDirectory>
        </fileSet><fileSet>
        <directory>src/main/resources/keys</directory>
        <outputDirectory>keys</outputDirectory>
    </fileSet>
        <fileSet>
            <includes>
                <include>README.md</include>
            </includes>
            <outputDirectory></outputDirectory>
        </fileSet>
    </fileSets>
    <dependencySets>
        <dependencySet>
            <unpack>false</unpack>
            <useProjectArtifact>true</useProjectArtifact>
            <outputDirectory>lib</outputDirectory>
            <scope>provided</scope>
        </dependencySet>
        <dependencySet>
            <unpack>false</unpack>
            <useProjectArtifact>true</useProjectArtifact>
            <outputDirectory>lib</outputDirectory>
            <scope>system</scope>
        </dependencySet>
        <dependencySet>
            <unpack>false</unpack>
            <useProjectArtifact>true</useProjectArtifact>
            <outputDirectory>lib</outputDirectory>
            <scope>runtime</scope>
        </dependencySet>
    </dependencySets>
</assembly>

具体的配置参数可以查看 https://maven.apache.org/plugins/maven-assembly-plugin/assembly.html

本人在实验过程中重点是处理了dependencySet这个配置:

<dependencySets>
        <dependencySet>
            <unpack>false</unpack>
            <useProjectArtifact>true</useProjectArtifact>
            <outputDirectory>lib</outputDirectory>
            <scope>provided</scope>
        </dependencySet>
        <dependencySet>
            <unpack>false</unpack>
            <useProjectArtifact>true</useProjectArtifact>
            <outputDirectory>lib</outputDirectory>
            <scope>system</scope>
        </dependencySet>
        <dependencySet>
            <unpack>false</unpack>
            <useProjectArtifact>true</useProjectArtifact>
            <outputDirectory>lib</outputDirectory>
            <scope>runtime</scope>
        </dependencySet>
    </dependencySets>

请配置上上述三个部分,要不然总是会缺少一些依赖包打不进来,Mark一下,很重要!

使用maven-assembly-plugin插件来定制化打包

简单的说,maven-assembly-plugin 就是用来帮助打包用的,比如说打出一个什么类型的包,包里包括哪些内容等等。

目前至少支持以下打包类型:

  • zip
  • tar
  • tar.gz
  • tar.bz2
  • jar
  • dir
  • war

默认情况下,打jar包时,只有在类路径上的文件资源会被打包到jar中,并且文件名是${artifactId}-${version}.jar,下面看看怎么用maven-assembly-plugin插件来定制化打包。

首先需要添加插件声明:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.4</version>
    <executions>
        <execution>
            <!-- 绑定到package生命周期阶段上 -->
            <phase>package</phase>
            <goals>
                <!-- 绑定到package生命周期阶段上 -->
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

使用内置的Assembly Descriptor

要使用maven-assembly-plugin,需要指定至少一个要使用的assembly descriptor 文件。默认情况下,maven-assembly-plugin内置了几个可以用的assembly descriptor:

  • bin : 类似于默认打包,会将bin目录下的文件打到包中;
  • jar-with-dependencies : 会将所有依赖都解压打包到生成物中;
  • src :只将源码目录下的文件打包;
  • project : 将整个project资源打包。

要查看它们的详细定义,可以到maven-assembly-plugin-2.4.jar里去看,例如对应 bin 的assembly descriptor 如下:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
    <id>bin</id>
    <formats>
        <format>tar.gz</format>
        <format>tar.bz2</format>
        <format>zip</format>
    </formats>
    <fileSets>
        <fileSet>
            <directory>${project.basedir}</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>README*</include>
                <include>LICENSE*</include>
                <include>NOTICE*</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>${project.build.directory}</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>${project.build.directory}/site</directory>
            <outputDirectory>docs</outputDirectory>
        </fileSet>
    </fileSets>
</assembly>

自定义Assembly Descriptor

一般来说,内置的assembly descriptor都不满足需求,这个时候就需要写自己的assembly descriptor的实现了。先从一个最简单的定义开始:

<?xml version='1.0' encoding='UTF-8'?>
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0  
                    http://maven.apache.org/xsd/assembly-1.1.0.xsd">
    <id>demo</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>${project.build.directory}/classes</directory>
            <outputDirectory>/</outputDirectory>
        </fileSet>
    </fileSets>
</assembly>

这个定义很简单:

  • format:指定打包类型;
  • includeBaseDirectory:指定是否包含打包层目录(比如finalName是output,当值为true,所有文件被放在output目录下,否则直接放在包的根目录下);
  • fileSets:指定要包含的文件集,可以定义多个fileSet;
  • directory:指定要包含的目录;
  • outputDirectory:指定当前要包含的目录的目的地。

要使用这个assembly descriptor,需要如下配置:

<configuration>  
    <finalName>demo</finalName>  
    <descriptors>
        <!--描述文件路径-->
        <descriptor>assemblies/demo.xml</descriptor>  
    </descriptors>  
    <outputDirectory>output</outputDirectory>
</configuration> 

最后会生成一个demo-demo.jar 文件在目录 output 下,其中前一个demo来自finalName,后一个demo来自assembly descriptor中的id,其中的内容和默认的打包出来的jar类似。

如果只想有finalName,则增加配置:

<appendAssemblyId>false</appendAssemblyId>  

添加文件

上面演示了添加所有编译后的资源,同样的可以增加其他资源,例如想添加当前工程目录下的某个文件 b.txt ,在assembly descriptor的assembly结点下增加

<files>
    <file>
        <source>b.txt</source>
        <outputDirectory>/</outputDirectory>
    </file>
</files>

这里用到了 files 元素类型,可以想象 fileSets 下的结点都是针对文件夹的;files 下的结点都是针对文件的。

也可以改变打包后的文件名,例如上面的 b.txt ,希望打包后的名字为 b.txt.bak, 只需要在file 里添加以下配置 :

<destName>b.txt.bak</destName>

排除文件

在 fileSet 里可以使用 includes 和 excludes 来更精确的控制哪些文件要添加,哪些文件要排除。

例如要排除某个目录下所有的txt文件:

<fileSet>  
    <directory>${project.build.directory}/classes</directory>  
    <outputDirectory>/</outputDirectory>  
    <excludes>  
        <exclude>***.class</include>
    </includes>
</fileSet>

添加依赖

如果想把一些依赖库打到包里,可以用 dependencySets 元素,例如最简单的,把当前工程的所有依赖都添加到包里:

<dependencySets>
    <dependencySet>
        <outputDirectory>/</outputDirectory>
    </dependencySet>
</dependencySets>

在assembly下添加以上配置,则当前工程的依赖和工程本身生成的jar都会被打包进来。

如果要排除工程自身生成的jar,则可以添加

<useProjectArtifact>false</useProjectArtifact>

unpack参数可以控制依赖包是否在打包进来时是否解开,例如解开所有包,添加以下配置:

<unpack>true</unpack>

和 fileSet 一样,可以使用 excludes 和 includes 来更详细的控制哪些依赖需要打包进来;另外 useProjectAttachments,useTransitiveDependencies,useTransitiveFiltering等参数可以对间接依赖、传递依赖进行控制。

其他选项

  • moduleSets:当有子模块时候用;
  • repositories:想包含库的时候用;
  • containerDescriptorHandlers:可以进行一些合并,定义ArtifactHandler之类的时候可以用,(可以参考:说明);
  • componentDescriptors:如上所述,可以包含一些componentDescriptor定义,这些定义可以被多个assembly共享。

Assembly Plugin更多配置

上面已经看到了一些Assembly Plugin本身的配置,例如 finalName, outputDirectory, appendAssemblyId 和 descriptors 等,除了这些还有其他的一些可配置参数,参见:single,其中某些参数会覆盖在assembly descriptor 中的参数。有一个比较有用的参数是: arcHive,它的详细配置在:archive。

下面介绍一些archive的用法。

指定Main-Class

archive的一个重要用处就是配置生成的MANIFEST.MF文件。默认会生成一个MANIFEST.MF文件,不过这个文件默认值没什么意义。如果想指定生成jar的Main-Class,可以如下配置:

<archive>  
    <manifest>  
        <mainClass>demo.DemoMain</mainClass>  
    </manifest>  
</archive>

下面来看一个项目中实际配置的文件:

pom文件:

<plugin>                                                                
    <groupId>org.apache.maven.plugins</groupId>                         
    <artifactId>maven-assembly-plugin</artifactId>                      
    <version>${maven-assembly-plugin.version}</version>                                          
    <configuration>                                                     
        <descriptors>                                                   
            <descriptor>package.xml</descriptor>                        
        </descriptors>                                                  
    </configuration>                                                    
    <executions>                                                        
        <execution>                                                     
            <id>make-assembly</id>                                      
            <phase>package</phase>                                      
            <goals>                                                     
                <goal>single</goal>                                     
            </goals>                                                    
        </execution>                                                    
    </executions>                                                       
</plugin>  

assembly descriptor 文件:

<assembly>
    <id>${assembly-id}</id>
    <!-- 最终打包成一个用于发布的war文件 -->
    <formats>
        <format>${assembly-format}</format>
    </formats>
    <fileSets>
        <!-- 把项目公用的配置文件,打包进zip文件的config目录 -->
        <fileSet>
            <directory>${project.basedir}/src/main/resources/base</directory>
            <outputDirectory>WEB-INF/classes</outputDirectory>
        </fileSet>
        <!-- 把项目环境的配置文件,打包进zip文件的config目录 -->
        <fileSet>
            <directory>${project.basedir}/src/main/resources/${env}</directory>
            <outputDirectory>WEB-INF/classes</outputDirectory>
        </fileSet>
        <!-- 打包项目自己编译出来的jar文件 -->
        <fileSet>
            <directory>${project.build.directory}</directory>
            <outputDirectory>WEB-INF/lib</outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>
        <!-- 打包项目依赖的jar文件 -->
        <fileSet>
            <directory>${project.build.directory}</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>WEB-INF/lib/*.jar</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: 使用maven-assembly-plugin如何打包多模块项目

本文链接: https://www.lsjlt.com/news/143286.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 使用maven-assembly-plugin如何打包多模块项目
    目录maven-assembly-plugin打包多模块项目概述1. 需求2. 打包流程使用maven-assembly-plugin插件来定制化打包使用内置的Assembly De...
    99+
    2022-11-13
  • Maven项目如何用Assembly打包可执行jar包
    这篇文章主要介绍“Maven项目如何用Assembly打包可执行jar包”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Maven项目如何用Assembly打包可执行jar包”文章能帮助大家解决问题。...
    99+
    2023-07-05
  • 如何使用eclipse打包Maven项目
    小编给大家分享一下如何使用eclipse打包Maven项目,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!Maven中最重要的是POM文件,其打包也是基于该文件的,...
    99+
    2023-06-29
  • maven多模块工程如何打包部署
    这篇文章将为大家详细讲解有关maven多模块工程如何打包部署,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。一般maven多模块工程结构如下图,图中分为dao数据层和上层web层(当然还可以有service...
    99+
    2023-05-31
    maven
  • 详解使用Maven构建多模块项目(图文)
    Maven多模块项目,适用于一些比较大的项目,通过合理的模块拆分,实现代码的复用,便于维护和管理。尤其是一些开源框架,也是采用多模块的方式,提供插件集成,用户可以根据需要配置指定的模块。项目结构如下:     test-hd-parent ...
    99+
    2023-05-31
    maven 多模块 多模
  • 如何在Java项目中使用Maven进行打包?
    Maven是一个流行的Java构建工具,它可以帮助我们自动管理项目依赖、构建、测试和部署。在本文中,我们将介绍如何使用Maven构建Java项目并打包成可执行的jar文件。 步骤1:安装Maven 在开始之前,需要先安装Maven。你可以...
    99+
    2023-09-13
    日志 数据类型 打包
  • 如何在kotlin+java项目中使用maven进行打包
    这期内容当中小编将会给大家带来有关如何在kotlin+java项目中使用maven进行打包,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。Java的特点有哪些Java的特点有哪些1.Java语言作为静态面向...
    99+
    2023-06-06
  • 利用IDEA工具修改Maven多模块项目标识包名全过程记录
    目录一、背景二、具备的能力2.1 IDEA2.2 Maven多模块项目三、步骤3.1 移动包3.2 选择重构的条件3.3 清扫战场3.4 小心毒刺3.4.1 redis序列化问题3....
    99+
    2022-11-13
  • 使用maven如何将项目中的test代码打包进jar中
    目录maven将项目的test代码打包进jar中在pom中添加如下在resources中添加一个assembly.xml文件运行mvn packagemaven打包跳过test在po...
    99+
    2022-11-13
  • 如何使用GitLabCI实现多模块项目CI/CD
    这篇文章将为大家详细讲解有关如何使用GitLabCI实现多模块项目CI/CD,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。Monorepo在开发多服务应用程序方面的优势。...
    99+
    2022-10-19
  • Java项目中如何使用NPM打包?
    NPM是一个JavaScript包管理器,可用于在Node.js环境中管理依赖项。但是,在Java项目中使用NPM打包也是可行的。在这篇文章中,我们将介绍如何在Java项目中使用NPM打包。 安装Node.js和NPM 首先,您需要安...
    99+
    2023-07-30
    npm leetcode 打包
  • Go语言中的HTTP模块:如何在项目中打包和加载?
    Go语言是一门相对年轻的编程语言,但它在网络编程方面却表现得十分强大。Go语言内置了HTTP模块,使得它成为了一个优秀的Web开发语言。在本文中,我们将讨论如何在你的项目中使用Go语言的HTTP模块,并且演示如何打包和加载这些模块。 Go语...
    99+
    2023-10-18
    http 打包 load
  • 【Python】项目打包:如何使用PyInstaller打包python程序(exe)
    文章目录 前言一、PyInstaller二、安装PyInstaller库三、PyInstaller的使用1.命令行+参数2.py文件+参数2.1配置文件config.py2.2打包文件pyTe...
    99+
    2023-09-02
    python 开发语言 qt5
  • 如何在Python项目中使用collections模块
    这篇文章主要介绍了如何在Python项目中使用collections模块,编程网小编觉得不错,现在分享给大家,也给大家做个参考,一起跟随编程网小编来看看吧!Python主要用来做什么Python主要应用于:1、Web开发;2、数据科学研究;...
    99+
    2023-06-06
  • 如何在python项目中使用urllib.request模块
    今天就跟大家聊聊有关如何在python项目中使用urllib.request模块,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。urllib子模块urllib.request 打开或请...
    99+
    2023-06-14
  • 如何使用HBuilderX把vue项目打包成apk
    目录1. 下载HBuilderX2. 安装HBuilderX3. 在vscode中打包vue项目3.1 在打包vue项目之前3.2 执行打包命令4. 在HBuilderX中打包apk...
    99+
    2022-11-13
  • 如何使用vue-cli创建项目并webpack打包
    这篇文章主要介绍“如何使用vue-cli创建项目并webpack打包”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“如何使用vue-cli创建项目并webpack打包...
    99+
    2022-10-19
  • 如何使用Docker部署打包发布springboot项目
    这篇文章将为大家详细讲解有关如何使用Docker部署打包发布springboot项目,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。前言从安装docker到多种方式打包发布,编译,镜像,容器等问题,遇到种种...
    99+
    2023-06-29
  • 如何使用jenkins一键打包发布vue项目
    这篇文章主要介绍如何使用jenkins一键打包发布vue项目,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!jenkins的安装Jenkins是一款开源 CI&CD 软件,用于自动化各种任务,包括构建、测试和部...
    99+
    2023-06-15
  • Linux系统下,如何使用PHP打包Laravel项目?
    Laravel是一种流行的PHP框架,具有优雅的语法、强大的功能和广泛的社区支持。在开发Laravel项目时,我们通常需要将代码打包成可执行的文件,以便在不同的环境中部署和运行。本文将介绍如何使用PHP打包Laravel项目,以及一些常见...
    99+
    2023-06-03
    linux 打包 laravel
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作