我是靠谱客的博主 美满老师,这篇文章主要介绍Maven对Springboot项目配置文件、依赖分离打包,现在分享给大家,希望可以做个参考。

使用maven-assembly-plugin进行配置分离

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
<?xml version="1.0" encoding="UTF-8"?> <assembly> <!-- 可自定义,这里指定的是项目环境 --> <!-- xxx.tar.gz --> <id>${name}</id> <!-- 打包的类型,如果有N个,将会打N个类型的包 --> <formats> <format>tar.gz</format> <format>zip</format> </formats> <includeBaseDirectory>true</includeBaseDirectory> <fileSets> <!-- 配置文件打包-打包至config目录下 --> <fileSet> <directory>src/main/resources/</directory> <outputDirectory>config</outputDirectory> <fileMode>0644</fileMode> <includes> <include>application.yml</include> <include>*.xml</include> <include>*.properties</include>
          <!--可以自己添加配置文件-->
          <include>common/*.properties</include> </includes> </fileSet> <!-- 启动文件目录 --> <fileSet> <directory>${basedir}/src/bin</directory> <outputDirectory>bin</outputDirectory> <fileMode>0755</fileMode> <includes> <include>**.sh</include> <include>**.bat</include> </includes> </fileSet> </fileSets> <dependencySets> <dependencySet> <!-- 依赖库 --> <outputDirectory>lib</outputDirectory> <scope>runtime</scope> <fileMode>0755</fileMode> <excludes> <exclude>${project.groupId}:${project.artifactId}</exclude> </excludes> </dependencySet> <dependencySet> <outputDirectory>boot</outputDirectory> <fileMode>0755</fileMode> <includes> <include>${project.groupId}:${project.artifactId}</include> </includes> </dependencySet> </dependencySets> </assembly>

Pom.xml文件配置

pom文件中build属性的配置

复制代码
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<build> <!-- 打包后的启动jar名称 --> <finalName>message</finalName> <plugins> <!-- 用于排除jar中依赖包 --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <layout>ZIP</layout> <includes> <!-- 项目启动jar包中排除依赖包 --> <include> <groupId>non-exists</groupId> <artifactId>non-exists</artifactId> </include> </includes> </configuration> </plugin> <!-- 将依赖cp到lib目录下 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>3.1.0</version> <executions> <execution> <phase>prepare-package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>target/lib</outputDirectory> <overWriteReleases>false</overWriteReleases> <overWriteSnapshots>false</overWriteSnapshots> <overWriteIfNewer>true</overWriteIfNewer> <includeScope>compile</includeScope> </configuration> </execution> </executions> </plugin> <!-- maven编译 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <!-- 不同版本需要制定具体的版本进行编译 --> <source>${java.version}</source> <target>${java.version}</target> </configuration> </plugin> <!-- 打包时跳过测试 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> <configuration> <skipTests>true</skipTests> </configuration> </plugin> <!-- 将项目中代码文件打成jar包 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.1.0</version> <configuration> <excludes> <!-- 打包后的jar包中不包括配置文件 --> <!-- 通常是指classpath下目录下的文件,这样可以避免编写时的找不到相应文件 --> <exclude>*.xml</exclude> <exclude>*.properties</exclude> <exclude>*.yml</exclude> </excludes> <archive> <manifest> <!-- 项目启动类 --> <mainClass>xx.xxx.Application</mainClass> <!-- 依赖的jar的目录前缀 --> <classpathPrefix>../lib/</classpathPrefix> <addClasspath>true</addClasspath> </manifest> <!-- 将config目录加入classpath目录 --> <manifestEntries> <Class-Path>../config/</Class-Path> </manifestEntries> </archive> </configuration> </plugin> <!-- 打包插件 --> <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>3.1.0</version> <configuration> <descriptors> <descriptor>src/main/resources/assembly.xml</descriptor> </descriptors> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build>

运行:maven package 

运行

1.直接运行 java -jar xxxx.jar即可

2.编写shell/bash脚本

在bin目录下编写脚本文件如下:

shell

复制代码
1
2
3
4
5
6
7
#! /bin/sh HOME = '/opt/xxx/boot' JAR_HOME = 'xxx.jar' cd $HOME nohup java -jar $JAR_HOME

bash

复制代码
1
2
3
4
5
6
7
8
9
10
@echo off rem ====================================================================== rem windows startup script rem rem ====================================================================== rem startup jar java -jar ../boot/xxx.jar pause

最后

以上就是美满老师最近收集整理的关于Maven对Springboot项目配置文件、依赖分离打包的全部内容,更多相关Maven对Springboot项目配置文件、依赖分离打包内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(60)

评论列表共有 0 条评论

立即
投稿
返回
顶部