本篇关键知识点:
Maven的assembly插件实现自定义打包部署(包含依赖jar包)
目前springboot项目的几种常见的部署方式。
1. 使用docker容器去部署,将springboot的应用构建成一个docker image,然后通过容器去启动镜像 ,这种方式在需要部署大规模的应用和应用扩展时是非常方便的,属于目前工业级的部署方案,但是需要掌握docker的生态圈技术。
2. 使用fatjar直接部署启动,这是很多初学者或者极小规模情况下的一个简单应用部署方式。
maven-shade-plugin,用来打可执行JAR包,也就是所谓的fat JAR包;
3. 使用maven-assembly-plugin插件打包,也是目前运维部署最多的一种方式,因为大数据项目中往往有很多shell脚本、SQL脚本、.properties及.xml配置项等,采用assembly插件可以让输出的结构清晰而标准化。
那么直接进入主题吧
配置
pom.xml
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <!-- 添加index则不从mainfest中读取classpath,而是从Index.list中读取 --> <!--<index>true</index> --> <manifest> <!--启动类main方法--> <mainClass>com.xl.xxx.XXXApplication</mainClass> <!-- 是否指定classthpath--> <addClasspath>true</addClasspath> <!--为下面的的<Class-Path>添加前缀,默认为空,'./'为当前目录--> <classpathPrefix>./</classpathPrefix> </manifest> <manifestEntries> <Class-Path>../conf/</Class-Path> </manifestEntries> </archive> <excludes> <exclude>*</exclude> <exclude>*/*</exclude> <exclude>*/static</exclude> </excludes> </configuration> </plugin> <plugin> <artifactId>maven-assembly-plugin</artifactId> <executions> <execution> <id>make-dir</id> <!-- 绑定到package生命周期阶段上 --> <phase>package</phase> <goals> <!-- 绑定到package生命周期阶段上 --> <goal>single</goal> </goals> <configuration> <finalName>${project.artifactId}</finalName> <!--配置描述文件路径--> <descriptors> <!--描述文件路径--> <descriptor>${project.basedir}/assembly/assembly.xml</descriptor> </descriptors> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <skipTests>true</skipTests> </configuration> </plugin> </plugins> </build>
上面配置中, 有maven-jar-plugin 默认打包插件, skipTest 打包跳过测试, 主要配置在
maven-assembly-plugin
插件中
assmbly 配置
结构目录

xml配置
<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 http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 "> <id>${project.version}</id><!--id 标识符,添加到生成文件名称的后缀符。可以不指定--> <formats> <!--打包方式,支持写多个包括(zip、tar、tar.gz (or tgz)、tar.bz2 (or tbz2)、jar、dir、war)--> <format>tar.gz</format> </formats> <includeBaseDirectory>true</includeBaseDirectory> <fileSets> <!-- fileSet用于指定项目路径和打包后的相对路径的映射关系--> <!--将所有resources文件下的文件打包在conf目录下--> <fileSet> <directory>${project.basedir}/src/main/resources</directory> <outputDirectory>conf</outputDirectory> </fileSet> <!--将启动脚本打包在bin目录下--> <fileSet> <directory>${project.basedir}/bin</directory> <outputDirectory>bin</outputDirectory> </fileSet> <!--你还可以将初始化sql文件等打包在init目录下--> <fileSet> <directory>${project.basedir}/sql</directory> <outputDirectory>init</outputDirectory> </fileSet> </fileSets> <dependencySets> <dependencySet> <useProjectArtifact>true</useProjectArtifact> <outputDirectory>lib</outputDirectory> <!-- 将scope为runtime的依赖包打包到lib目录下。 --> <scope>runtime</scope> </dependencySet> </dependencySets> </assembly>
bin脚本命令
start.sh
#!/bin/sh nohup java -jar ../lib/demo-0.1.0-SNAPSHOT.jar >/dev/null 2>&1 & echo $! > /var/run/demo.pid
stop.sh
#!/bin/sh PID=$(cat /var/run/demo.pid) kill -9 $PID
打包成功
打包成功后, 我们可以直接上传tag包至linux 服务器
部署流程
上传tag包 >