目录

Maven 打包

发布 Spring Boot 项目的时候,每次改动一点东西,就需要将整个项目重新打包部署,并且打包出来的jar包太庞大,每次更新项目的时候,需要上传的文件很大,十分不便,故把依赖lib里面的jar包从项目分离出来,每次部署只需要发布代码即可。

spring-boot-maven-plugin

这个插件会将项目所有的依赖打入 BOOT-INF/lib

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

maven-dependency-plugin + maven-resources-plugin

 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>target/lib</outputDirectory>
                            <excludeTransitive>false</excludeTransitive>
                            <stripVersion>false</stripVersion>
                            <includeScope>runtime</includeScope>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <layout>ZIP</layout>
                    <includes>
                        <include>
                            <groupId>cn.jstars</groupId>
                            <artifactId>datatocloud</artifactId>
                        </include>
                    </includes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <filtering>true</filtering>
                <directory>src/main/resources</directory>
                <excludes>
                    <exclude>static/**</exclude>
                    <exclude>templates/**</exclude>
                    <exclude>*.yml</exclude>
                    <exclude>*.properties</exclude>
                    <exclude>*.xml</exclude>
                    <exclude>*.txt</exclude>
                </excludes>
            </resource>
        </resources>
    </build>
Spring-Boot-Layers-Index: BOOT-INF/layers.idx
Start-Class: com.ztx.kol.api.ApiApplication
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Spring-Boot-Version: 2.6.4
Created-By: Apache Maven 3.6.3
Build-Jdk: 1.8.0_131
Main-Class: org.springframework.boot.loader.JarLauncher

Spring-boot的配置信息,其中最主要的是Main-Class是org.springframework.boot.loader.PropertiesLauncher,而非SpringBoot的Appliction启动类。启动类放在了Start-Class。

maven-assembly-plugin

 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
<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <layout>ZIP</layout>
        <includes>
            <include>
                <groupId>com.ynthm</groupId>
                <artifactId>demo</artifactId>
            </include>
        </includes>
    </configuration>
</plugin><!-- 使用assembly打包 -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <descriptors>
            <!-- assembly配置文件位置 -->
            <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-surefire-plugin</artifactId>
    <configuration>
        <skipTests>true</skipTests>
    </configuration>
</plugin>

在 src/main目录建立assembly目录,添加assembly.xml

 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2
       http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <!--
        必须写,否则打包时会有 assembly ID must be present and non-empty 错误
        这个名字最终会追加到打包的名字的末尾,如项目的名字为 speed-api-0.0.1-SNAPSHOT,
        则最终生成的包名为 speed-api-0.0.1-SNAPSHOT-bin.zip
     -->
    <id>bin</id>

    <!-- 打包后的文件格式,可以是zip,tar,tar.gz,tar.bz2,jar,war,dir -->
    <formats>
        <format>zip</format>
    </formats>

    <!-- 压缩包下是否生成和项目名相同的根目录 -->
    <includeBaseDirectory>false</includeBaseDirectory>

    <dependencySets>
        <dependencySet>
            <!-- 不使用项目的artifact,第三方jar不要解压,打包进zip文件的lib目录 -->
            <useProjectArtifact>false</useProjectArtifact>
            <outputDirectory>lib</outputDirectory>
            <unpack>false</unpack>
            <!-- 将scope为runtime的依赖包打包到lib目录下。 -->
            <scope>runtime</scope>
        </dependencySet>
    </dependencySets>

    <fileSets>
        <!-- 把项目相关的说明文件,打包进zip文件的根目录 -->
        <fileSet>
            <directory>${project.basedir}</directory>
            <outputDirectory></outputDirectory>
            <includes>
                <include>README*</include>
                <include>LICENSE*</include>
                <include>NOTICE*</include>
            </includes>
        </fileSet>

        <!-- 把项目的配置文件,打包进zip文件的config目录 -->
        <fileSet>
            <directory>${project.basedir}/src/main/resources</directory>
            <outputDirectory>config</outputDirectory>
            <excludes>
            <exclude>test</exclude>
            <exclude>test/*</exclude>
            <exclude>*.properties</exclude>
            </excludes>
        </fileSet>

        <!-- 把项目的脚本文件,打包进zip文件的bin目录 -->
        <fileSet>
            <directory>${project.basedir}/src/main/bin</directory>
            <outputDirectory></outputDirectory>
        </fileSet>

        <!-- 把项目自己编译出来的jar文件,打包进zip文件的根目录 -->
        <fileSet>
            <directory>${project.build.directory}</directory>
            <outputDirectory></outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>
1
java -Dloader.path=lib/ -jar MyTest.jar

maven-jar-plugin

使用maven-jar-plugin 打包成可执行jar包。 结果:这个方式的确可以生成 lib、bin、config、xx.jar 等目录结构,jar包也可以执行。

但是目录结构比较散乱,散放在target目录中,最终还需要手动拷贝lib和jar包。将所有打包后的结果单独存在一个zip包中。

maven-assembly-plugin 分离 lib

 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
<plugins>
<!-- 指定启动类,将依赖打成外部jar包 -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <!-- 生成的jar中,不要包含pom.xml和pom.properties这两个文件 -->
            <addMavenDescriptor>false</addMavenDescriptor>
            <manifest>
                <!-- 是否要把第三方jar放到manifest的classpath中 -->
                <addClasspath>true</addClasspath>
                <!-- 外部依赖jar包的最终位置 -->
                <classpathPrefix>lib/</classpathPrefix>
                <!-- 项目启动类 -->
                <mainClass>${main.class}</mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>

<!-- 使用assembly打包 -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <descriptors>
            <!-- assembly配置文件位置 -->
            <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>

半自动化部署 maven-jar-plugin

1.替换为插件 maven-jar-plugin

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
<build>
   <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.1.1</version>
            <configuration>
                <archive>
                	<!-- 生成的jar中不要包含pom.xml和pom.properties这两个文件 -->
                    <addMavenDescriptor>false</addMavenDescriptor>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <!--这里需要修改为你的项目的主启动类-->
                        <mainClass>com.ynthm.business.App</mainClass> 
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>
  • addClasspath:表示需要加入到类构建路径
  • classpathPrefix:指定生成的Manifest文件中Class-Path依赖lib前面都加上该前缀路径,构建出lib/xx.jar
  • mainClass:表示项目的启动类。

2.拷贝依赖的jar外面的lib目录

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<build>
   <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-lib</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>target/lib</outputDirectory>
                        <excludeTransitive>false</excludeTransitive>
                        <stripVersion>false</stripVersion>
                        <includeScope>runtime</includeScope>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

3.在和jar包同级的目录下新建一个config目录,放入application.yml文件

这里可能有小伙伴有疑问了,打包的jar里面不是应该有application.yml文件吗,这里为什么再放一份? 这是因为springboot读取配置有一个优先级,放在jar包外面config目录优先级最高,主要是便于从外部修改配置,而不是改jar包中的application.yml文件。优先级如下:

  1. 当前目录的config目录下
  2. 当前目录
  3. classpath的config目录下
  4. classpath的根目录

4 启动项目 加入 –debug 可以让你可以看到比较详细的启动日志: java -jar xxx-1.0.0.jar --debug

全自动化部署 maven-jar-plugin

前面介绍的步骤中,需要手动的拷贝application.yml文件,并且jar包内外都存在配置。这里引入一种自动化配置,将所有东西打成zip文件,直接发布到服务目录,解压后,即可启动。

1.替换为maven-jar-plugin插件

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
<build>
   <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.1.1</version>
            <configuration>
                <archive>
                	<!-- 生成的jar中不要包含pom.xml和pom.properties这两个文件 -->
                    <addMavenDescriptor>false</addMavenDescriptor>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <!--这里需要修改为你的项目的主启动类-->
                        <mainClass>com.ynthm.business.App</mainClass> 
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>
  • addClasspath:表示需要加入到类构建路径
  • classpathPrefix:指定生成的Manifest文件中Class-Path依赖lib前面都加上该前缀路径,构建出lib/xx.jar
  • mainClass:表示项目的启动类。

2.拷贝依赖的 jar 外面的 lib 目录

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<build>
   <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-lib</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>target/lib</outputDirectory>
                        <excludeTransitive>false</excludeTransitive>
                        <stripVersion>false</stripVersion>
                        <includeScope>runtime</includeScope>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

3.排除 resources 下面的 yml

因为需要把它放在 jar 外部,不能让 jar 打包插件将其打入 jar 包 classpath 下去

 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>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <!-- 打包后的包名是否包含assembly的id名 -->
                <appendAssemblyId>false</appendAssemblyId>
                <!-- 指定最后tar或者zip包的名字 -->
                <finalName>djys-business</finalName>
                <!-- tar或者zip包的输出目录 -->
                <outputDirectory>target/</outputDirectory>
                <descriptors>
                    <!-- 引用的assembly配置文件-->
                    <descriptor>src/main/resources/assembly.xml</descriptor>
                </descriptors>
            </configuration>
            <executions>
                <!-- phase加入package后,则在执行maven package时就可以调用maven-assembly-plugin插件定义的打包方式 -->
                <execution>
                    <!--名字任意 -->
                    <id>make-assembly</id>
                    <!-- 绑定到package生命周期阶段上 -->
                    <phase>package</phase>
                    <goals>
                        <!-- 只运行一次 -->
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

assembly.xml 配置,该文件放在 resources 目录下面即可。

 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
<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>package</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>true</includeBaseDirectory>
    <fileSets>
        <!--拷贝application.yml文件到jar包的外部config目录下面-->
        <fileSet>
            <directory>${basedir}/src/main/resources</directory>
            <includes>
                <include>*.yml</include>
            </includes>
            <filtered>true</filtered>
            <outputDirectory>${file.separator}config</outputDirectory>
        </fileSet>

		<!--拷贝lib包到jar包的外部lib下面-->
        <fileSet>
            <directory>${project.build.directory}/lib</directory>
            <outputDirectory>${file.separator}lib</outputDirectory>
            <!-- 打包需要包含的文件 -->
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>

		<!--如有需要,可以配置多个需要拷贝的文件即可-->
        <fileSet>
            <directory>${project.build.directory}</directory>
            <outputDirectory>${file.separator}</outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>

4.解压zip,启动