springboot-使用assembly进行项目打包

一、相关依赖

1
2
3
4
5
6
7
8
9
10
11
12
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
    </parent>
 
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

二、项目构建包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

  执行mvn package可自动生成一个独立可执行的jar文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<build>
       <finalName>assembly</finalName>
       <plugins>
           <plugin>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-maven-plugin</artifactId>
               <configuration>
                   <includeSystemScope>true</includeSystemScope>
               </configuration>
           </plugin>
           <plugin>
               <!--主要使用的是maven提供的assembly插件完成-->
               <artifactId>maven-assembly-plugin</artifactId>
               <executions>
                   <execution>
                       <configuration>
                           <appendAssemblyId>false</appendAssemblyId>
                           <!--具体的配置文件-->
                           <descriptors>${project.basedir}/src/main/resources/assembly/package.xml</descriptors>
                       </configuration>
                       <id>make-assembly</id>
                       <!--绑定到maven操作类型上-->
                       <phase>package</phase>
                       <!--运行一次-->
                       <goals>
                           <goal>single</goal>
                       </goals>
                   </execution>
               </executions>
           </plugin>
       </plugins>
   </build>

三、assembly 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?xml version='1.0' encoding='UTF-8'?>
<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">
    <!--打包名称,唯一标识-->
    <id>${project.build.finalName}</id>
    <!--打包格式,可以手动修改-->
    <formats>
        <format>tar.gz</format>
    </formats>
    <!--文件设置-->
    <fileSets>
        <fileSet>
            <!--目标目录,会处理目录里面的所有文件-->
            <directory>${project.basedir}/src/main/resources/config</directory>
            <!--相对于打包后的目录-->
            <outputDirectory>config</outputDirectory>
            <!--文件过滤-->
            <includes>
                <include>*.*</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>${project.basedir}/src/main/resources/script</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>*.*</include>
            </includes>
            <!--文件权限-->
            <fileMode>0755</fileMode>
            <!--如果是脚本,一定要改为unix.如果是在windows上面编码,会出现dos编写问题-->
            <lineEnding>unix</lineEnding>
        </fileSet>
    </fileSets>
    <files>
        <!--包含打包后的jar文件,可以不加入<outputDirectory/>,默认打包的目录-->
        <file>
            <source>${project.build.directory}/${project.build.finalName}.jar</source>
        </file>
        <!--这种方式也可以进行文件处理,但是针对单文件-->
       <!-- <file>
            <source>${project.basedir}/src/main/resources/script/start.sh</source>
            <fileMode>0755</fileMode>
            <lineEnding>unix</lineEnding>
        </file>-->
    </files>
</assembly>

  备注:具体的参数的意义可以参考官网:http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html 

  Maven程序集插件依赖于提供的程序集描述符来指示其执行。尽管已经有预制的描述符可供使用,但它们只能满足一些常见的装配要求。

  因此,为了自定义程序集插件创建程序集的方式,您需要知道如何使用程序集描述符。

  该描述符指定要创建的程序集档案的类型,程序集的内容,以及将依赖项或其模块与程序集捆绑在一起的方式。

  在使用springboot框架的时候,存在一个问题。就是我们配置yaml文件,需要单独提出来做参数修改。当然这个是可以通过spring.profiles.active的方式来配置dev,prod等环境的激活。但是我们如果存在环境不确定,或者需要启动脚本,启动项目的时候,这样通过jar的方式后续会处理很多工作。所以前期的集成工作还是很有必要的。

posted on   书梦一生  阅读(708)  评论(0编辑  收藏  举报

编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示