首页 > 技术文章 > 使用assembly插件打包SpringBoot项目为tar

Oh-mydream 2021-09-14 09:14 原文

使用assembly插件打包项目为tar

只要没有搬了西瓜丢了玉米,我们就是在进步呀

1.原始项目

一个helloworld Springboot项目

2. 添加插件

在pom.xml中添加

<build>
        <!--我们自己的文件会打成一个demo-server.jar包,此包中不需要配置文件-->
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <excludes>
                    <exclude>**/*.xml</exclude>
                    <exclude>**/*.properties</exclude>
                </excludes>
            </resource>
        </resources>
        <plugins>
            
            <!--assebly插件-->
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.5.5</version>
                <configuration>
                    <finalName>${project.artifactId}</finalName>
                    <skipAssembly>false</skipAssembly>
                    <appendAssemblyId>false</appendAssemblyId>
<!--如果路径长度超过100字符,执行的操作warn" (default), "fail", "truncate", "gnu", or "omit". -->
                    <tarLongFileMode>gnu</tarLongFileMode>
                    <outputDirectory>target</outputDirectory>
                    <!--具体配置文件所在路径-->
                    <descriptors>
                        <descriptor>src/main/assembly/assembly.xml</descriptor>
                    </descriptors>
                </configuration>
                <!--执行的操作-->
                <executions>
                    <execution>
                        <id>assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>

    </build>

注:在使用Assembly打包的时候必须要springboot把默认的打包插件删除掉,否则会报找不到启动类

    <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>

3. 配置assembly.xml 放在 main目录下,与java/resources同级

<?xml version="1.0" encoding="UTF-8"?>
<assembly>
    <id>prd</id>
    <baseDirectory>${project.artifactId}</baseDirectory>
    <includeBaseDirectory>true</includeBaseDirectory>
    <formats>
        <format>tar.gz</format><!--打包的文件格式:tar.zip war zip等-->
    </formats>
    <fileSets>
        <!-- 将src/main/resources下配置问价打包到conf目录 -->
        <fileSet>
            <directory>src/main/resources</directory>
            <outputDirectory>/conf</outputDirectory>
            <includes>
                <include>**/*.xml</include>
                <include>**/*.properties</include>
            </includes>
            <filtered>true</filtered><!-- 是否进行属性替换 -->
        </fileSet>

        <!-- 打包.sh文件到bin 目录 -->
        <fileSet>
            <directory>src/main/assembly/bin</directory>
            <outputDirectory>/bin</outputDirectory>
            <includes>
                <include>*.*</include>
            </includes>
            <fileMode>0755</fileMode><!--赋予文件权限-->
        </fileSet>
    </fileSets>
    <!--依赖输出-->
    <dependencySets>
        <dependencySet>
            <unpack>false</unpack><!--不解压-->
            <outputDirectory>lib</outputDirectory><!--依赖包输出路径-->
            <useProjectArtifact>true</useProjectArtifact><!--当前项目构件是否包含在这个依赖集合里。-->
            <scope>runtime</scope><!-- 将scope为runtime的依赖包打包到lib目录下。 -->

        </dependencySet>
    </dependencySets>
</assembly>

4.assembly目录下创建bin 目录,bin目录下创建.sh文件

5.执行 maven package

在target目录下生成tar包,结构如下

|
  |-bin
  |-conf
  |-lib

推荐阅读