阅读 123

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

全部可设置节点可参考官网:maven.apache.org/plugins/mav…

<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),符合目前项目部署的目录结构。 在这里插入图片描述


作者:lombok
链接:https://juejin.cn/post/7023303501325271048


文章分类
后端
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐