亲宝软件园·资讯

展开

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

Mr-Wanter 人气:0
想了解SpringCloud maven-assembly-plugin 多级目录打包的实现的相关内容吗,Mr-Wanter在本文为您仔细讲解SpringCloud maven-assembly-plugin 的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:spring-boot-maven-plugin,maven-assembly-plugin,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),符合目前项目部署的目录结构。

在这里插入图片描述

加载全部内容

相关教程
猜你喜欢
用户评论