SpringCloud maven-assembly-plugin 多级目录打包的实现

 更新时间:2021年10月27日 10:48:29   作者:Mr-Wanter  
本文主要介绍了SpringCloud maven-assembly-plugin 多级目录打包的实现,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

1、spring-boot-maven-plugin

springboot默认打包工具为spring-boot-maven-plugin

pom配置:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <mainClass>com.gsafety.bg.enterprise.EnterpriseApplication</mainClass>
        <layout>ZIP</layout>
        <!-- 打增量包时需要includes部分, 要打全量包删除includes -->
        <includes>
            <include>
                <groupId>com.gsafety.bg</groupId>
                <artifactId>enterprise-controller</artifactId>
            </include>
            <include>
                <groupId>com.gsafety.bg</groupId>
                <artifactId>enterprise-service</artifactId>
            </include>
            <include>
                <groupId>com.gsafety.bg</groupId>
                <artifactId>enterprise-dao</artifactId>
            </include>
        </includes>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
        </execution>
    </executions>
</plugin>

打包后的目录结构:

在这里插入图片描述

BOOT-INF内包含目录:lib(enterprise-service-1.0.0.jar、enterprise-dao-1.0.0.jar、enterprise-controller-1.0.0.jar)、classes、classpath.idx

2、maven-assembly-plugin

maven-assembly-plugin 插件的主要作用是允许用户将项目输出与它的依赖项、模块、站点文档、和其他文件一起组装成一个可分发的归档文件,简单的说,就是自定义打包的工具,有自己的配置文件(Assembly描述符文件)。微服务使用这个插件的概率比较高,平时普通的项目不需要这样的实现方式。

pom配置:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.4.1</version>
    <configuration>
        <finalName>enterprise</finalName>
        <encoding>utf-8</encoding>
        <descriptors>
            <descriptor>src/main/assembly/assembly.xml</descriptor>
        </descriptors>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

assembly.xml

全部可设置节点可参考官网:http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html

<assembly>
    <id>1.0</id>
    <formats>
        <format>tar.gz</format>
    </formats>

    <includeBaseDirectory>true</includeBaseDirectory>
    <!--    项目所需lib包-->
    <!--    <dependencySets>-->
    <!--        &lt;!&ndash;把依赖都打包进libs文件夹&ndash;&gt;-->
    <!--        <dependencySet>-->
    <!--            <useProjectArtifact>true</useProjectArtifact>-->
    <!--            <outputDirectory>libs</outputDirectory>-->
    <!--            <scope>runtime</scope>-->
    <!--        </dependencySet>-->
    <!--    </dependencySets>-->
    <fileSets>
        <!--打包启动文件到deploy目录-->
        <fileSet>
            <!--需要打包的文件所在目录 即start.sh-->
            <directory>src/main/assembly/bin</directory>
            <!--要打包到的地址-->
            <outputDirectory>deploy</outputDirectory>
            <!--linux权限-->
            <fileMode>0755</fileMode>
        </fileSet>
        <!--打包可执行jar到application-server目录-->
        <fileSet>
            <directory>target</directory>
            <outputDirectory>application-server</outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>
        <!--打包src/main/resources到logs/enterprise目录-->
        <fileSet>
            <directory>src/main/resources</directory>
            <outputDirectory>logs/enterprise</outputDirectory>
            <fileMode>0755</fileMode>
            <!--打包不包含src/main/resources所有文件,即生成logs/enterprise空目录-->
            <excludes>
                <exclude>**/*</exclude>
            </excludes>
        </fileSet>
    </fileSets>
</assembly>

打包后的目录结构:

在这里插入图片描述

查看application-server文件夹内可执行文件解压目录:

在这里插入图片描述

发现与spring-boot-maven-plugin打包后的目录不一致,明显缺少lib内的三个jar和其他一些文件

3、maven-assembly-plugin打包后的可执行文件缺失lib问题

修改pom文件:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <mainClass>com.gsafety.bg.enterprise.EnterpriseApplication</mainClass>
                <layout>ZIP</layout>
                <!-- 打增量包时需要includes部分, 要打全量包删除includes -->
                <includes>
                    <include>
                        <groupId>com.gsafety.bg</groupId>
                        <artifactId>enterprise-controller</artifactId>
                    </include>
                    <include>
                        <groupId>com.gsafety.bg</groupId>
                        <artifactId>enterprise-service</artifactId>
                    </include>
                    <include>
                        <groupId>com.gsafety.bg</groupId>
                        <artifactId>enterprise-dao</artifactId>
                    </include>
                </includes>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
		<!-- 生成项目依赖到lib本地,并设置需要排除的jar-->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>../../../lib</outputDirectory>
                        <!-- 需要排除的jar的 groupId -->
                        <excludeArtifactIds>
                            enterprise-controller,enterprise-service,enterprise-dao
                        </excludeArtifactIds>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4.1</version>
            <configuration>
                <finalName>enterprise</finalName>
                <encoding>utf-8</encoding>
                <descriptors>
                    <descriptor>src/main/assembly/assembly.xml</descriptor>
                </descriptors>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

即plugins先引用spring-boot-maven-plugin 后引用maven-assembly-plugin,这样spring-boot-maven-plugin会将enterprise-service-1.0.0.jar、enterprise-dao-1.0.0.jar、enterprise-controller-1.0.0.jar三个jar打包到lib中,打包后maven-assembly-plugin就会将其打包进enterprise-1.0.tar.gz。

这样enterprise-1.0.tar.gz内就包含了启动文件(deploy)、可执行文件(application-server/enterprise-main-1.0.0.jar)、日志目录(logs/enterprise),符合目前项目部署的目录结构。

在这里插入图片描述

到此这篇关于SpringCloud maven-assembly-plugin 多级目录打包的实现的文章就介绍到这了,更多相关SpringCloud maven-assembly-plugin 多级目录打包内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Java日常练习题,每天进步一点点(44)

    Java日常练习题,每天进步一点点(44)

    下面小编就为大家带来一篇Java基础的几道练习题(分享)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧,希望可以帮到你
    2021-07-07
  • Java实现浏览器端大文件分片上传

    Java实现浏览器端大文件分片上传

    本文主要介绍了Java实现浏览器端大文件分片上传,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-08-08
  • SpringBoot 实现定时任务的方法详解

    SpringBoot 实现定时任务的方法详解

    这篇文章主要介绍了SpringBoot 实现定时任务的方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-08-08
  • Jenkins的安装配置详解

    Jenkins的安装配置详解

    这篇文章主要介绍了Jenkins的安装配置详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-06-06
  • Idea springboot springCloud热加载热调试两种常用方式

    Idea springboot springCloud热加载热调试两种常用方式

    这篇文章主要介绍了Idea springboot springCloud热加载热调试常用的两种方式,在项目开发的过程中,需要修改调试的时候偶每次都需要重启项目浪费时间,下面是我整理的两种常用的两种方式,需要的朋友可以参考下
    2023-04-04
  • Java文件操作实例详解

    Java文件操作实例详解

    这篇文章主要为大家详细介绍了Java文件操作实例,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-04-04
  • 数据同步利器DataX简介及如何使用

    数据同步利器DataX简介及如何使用

    DataX 是阿里云 DataWorks数据集成 的开源版本,使用Java 语言编写,在阿里巴巴集团内被广泛使用的离线数据同步工具/平台,今天给大家分享一个阿里开源的数据同步工具DataX,在Github拥有14.8k的star,非常受欢迎
    2024-02-02
  • Java中Lambda表达式用法介绍

    Java中Lambda表达式用法介绍

    本文详细讲解了Java中Lambda表达式的用法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-12-12
  • Java通过导出超大Excel文件解决内存溢出问题

    Java通过导出超大Excel文件解决内存溢出问题

    导出excel是咱Java开发的必备技能,下面这篇文章主要给大家介绍了关于Java通过导出超大Excel文件解决内存溢出问题的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
    2021-09-09
  • java实现将ftp和http的文件直接传送到hdfs

    java实现将ftp和http的文件直接传送到hdfs

    前面几篇文章,我们已经做了很好的铺垫了,几个要用到的工具我们都做了出来,本文就是将他们集合起来,说下具体的用法,小伙伴们可以参考下。
    2015-03-03

最新评论