服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - SpringCloud maven-assembly-plugin 多级目录打包的实现

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

2022-03-02 00:58Mr-Wanter Java教程

本文主要介绍了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>

打包后的目录结构:

SpringCloud maven-assembly-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>

打包后的目录结构:

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

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

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

发现与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 多级目录打包的实现的文章就介绍到这了,更多相关SpringCloud maven-assembly-plugin 多级目录打包内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/mr_wanter/article/details/118697499

延伸 · 阅读

精彩推荐