Maven Assembly自定义打包插件

前言

在之前的项目中,一个项目被不同的package划分,util负责各种工具类,exception负责异常处理还有mapper等完成各自的内容,项目需要发布时,通过mvn package产生一个jar包或war包进行全量发布。若util包中的一个类出现问题,则需要再次进行全量发布(直接替换class文件这个粗暴的方式就不说了)。通过阅读几个开源项目代码,发现了一个比较不错的包结构设计方式:将原本的单体项目以maven聚合工程的方式展开,每个子模块完成特定的工作,降低耦合性。例如:新建子工程commo就可以将dto、vo、exception和util单独划分为一个模块,在需要时引入依赖即可。

聚合工程打包部署的两种方式

打包为可执行的Jar包

用Maven快速生成带有依赖的可执行jar包

使用maven assembly自定义打包

通过assembly插件可以将项目文档、依赖、配置文件及其他文件打包为zip、tar.gz等格式的文件,这里推荐用zip。更多关于此插件的描述点这里

  • 我认为插件最强大的功能是可以自定义打包格式,通过读取XML配置文件完成文件管理,依赖管理等。

代码

项目结构是这样的

  1. POM文件添加插件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.lhc</groupId>
        <artifactId>springcloud</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <groupId>com.lhc</groupId>
    <artifactId>serviceproducer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>serviceproducer</name>
    <description>服务提供者</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--Nacos Discovery Starter-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>guava</artifactId>
                    <groupId>com.google.guava</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--引用子模块-->
        <dependency>
            <groupId>com.lhc</groupId>
            <artifactId>commo</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>


    <build>
        <!--项目本地打包:执行打包命令只有只有一个Jar包,只包含Java代码-->
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.6</version>
                <!--配置配置文件-->
                <configuration>
                    <excludes>
                        <exclude>*.yml</exclude>
                        <exclude>*.properties</exclude>
                        <exclude>*/**.xml</exclude>
                        <exclude>mapper</exclude>
                        <exclude>*.xml</exclude>
                        <exclude>*.txt</exclude>
                    </excludes>
                    <archive>
                        <manifest>
                            <!-- 主函数的入口 -->
                            <mainClass>com.lhc.serviceproducer.ServiceproducerApplication</mainClass>
                            <!-- 打包时 MANIFEST.MF文件不记录的时间戳版本 -->
                            <useUniqueVersions>false</useUniqueVersions>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>./</classpathPrefix>
                        </manifest>
                        <manifestEntries>
                            <Class-Path>../conf/</Class-Path>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>
            <!--项目全局打包:包含所有的依赖-->
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <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>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
  1. assembly.xml定义了项目的打包格式
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">

    <!-- assembly id -->
    <id>build</id>
    <!--打包后文件的格式,目前支持zip、tar、tar.gz (or tgz)、tar.bz2 (or tbz2)、tar.snappy、tar.xz (or txz)、jar、dir、war,支持指定多种格式-->
    <formats>
        <format>tar.gz</format>
        <format>zip</format>
    </formats>
    <!--在最终存档中包含基本目录-->
    <includeBaseDirectory>true</includeBaseDirectory>
    <!--置生成的程序集档案的基本目录。-->
    <baseDirectory>${project.artifactId}</baseDirectory>


    <!--最终生成bin文件夹,用于存放启动脚本-->
    <fileSets>
        <fileSet>
            <!--路径-->
            <directory>${basedir}/src/main/assembly/bin</directory>
            <!--文件夹名称-->
            <outputDirectory>bin</outputDirectory>
            <!-- linux文件权限     -->
            <fileMode>0755</fileMode>
        </fileSet>

        <!--最终生成conf文件夹,用于存放配置文件等-->
        <fileSet>
            <directory>${basedir}/src/main/resources</directory>
            <includes>
                <!--这里更具自己的项目设置,支持通配符-->
                <include>application.properties</include>
                <include>logback.xml</include>
                <include>banner.txt</include>
            </includes>
            <outputDirectory>conf</outputDirectory>
            <fileMode>0644</fileMode>
        </fileSet>

        <!--最终生成lib文件夹,用于存放项目所需的Jar包-->
        <fileSet>
            <directory>${project.build.directory}</directory>
            <outputDirectory>lib</outputDirectory>
            <includes>
                <include>${project.artifactId}.jar</include>
            </includes>
            <fileMode>0755</fileMode>
        </fileSet>

    </fileSets>

    <dependencySets>
        <dependencySet>
            <outputDirectory>lib</outputDirectory>
            <scope>runtime</scope>
            <!--            打包时跳过测试与-DskipTests效果类似-->
            <excludes>
                <exclude>junit:junit</exclude>
            </excludes>
        </dependencySet>
    </dependencySets>
</assembly>

官网提供了非常丰富的配置,这里只使用了部分,多去官网了解吧

  1. 执行mvn package命令,得到文件,上传发布即可

  2. 在服务器上解压文件得到三个目录

  3. 运行启动脚本即可

最后

通过这种打包方式,可以进行快速发布,修改配置文件接口启动。若依赖的commo模块需要修改,修改之后替换lib包中的文件即可
原文地址
若有错误,欢迎指教,谢谢

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章