1,配置当前 spring boot 项目中的 prom 文件
在pom.xml中,修改build配置项,根据自己的项目实际情况,进行适当修改!
<!--start 分离打包 (使用assembly插件,具体配置在src/main/assembly/assembly.xml,将rbj-web-1.0-1.0.tar.gz 解压后就是分离后的发布文件,运行此文件就行)-->
<build>
<plugins>
<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>
<!-- 依赖包输出目录,将来不打进jar包里 -->
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<excludeTransitive>false</excludeTransitive>
<stripVersion>false</stripVersion>
<includeScope>runtime</includeScope>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<outputDirectory>${project.build.directory}/resources</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<excludes>
<exclude>*.yml</exclude>
<exclude>*.properties</exclude>
<exclude>*.xml</exclude>
<exclude>*.docx</exclude>
<exclude>*.txt</exclude>
<exclude>liquibase/**</exclude>
<exclude>python/**</exclude>
<exclude>mapper/**</exclude>
<exclude>META-INF/**</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<layout>ZIP</layout>
<includes>
<include>
<groupId>non-exists</groupId>
<artifactId>non-exists</artifactId>
</include>
</includes>
<fork>true</fork>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<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>
</build>
<!--end 分离打包 (启动命令 java -jar -Dloader.path=.,resources,lib rbj-web-1.0.jar) -->
一共涉及5个plugin,分别是maven-dependency-plugin、maven-resources-plugin、maven-jar-plugin、spring-boot-maven-plugin、maven-assembly-plugin,分别可以对依赖,资源配置,可执行jar,springboot的maven管理,再把以上独立分离模块打包组装tar.gz。
2,创建并配置 assembly.xml,在对应目录下创建 如:src/main/assembly/assembly.xml,assembly.xml进行如下设置:
<?xml version="1.0" encoding="UTF-8"?>
<assembly>
<!-- 可自定义,这里指定的是项目环境 -->
<!-- spring-boot-assembly-local-1.0.RELEASE.tar.gz -->
<id>${project.version}</id>
<!-- 打包的类型,如果有N个,将会打N个类型的包 -->
<formats>
<format>tar.gz</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<!--
0755->即用户具有读/写/执行权限,组用户和其它用户具有读写权限;
0644->即用户具有读写权限,组用户和其它用户具有只读权限;
-->
<!-- 将src/bin目录下的所有文件输出到打包后的bin目录中 -->
<fileSet>
<directory>${basedir}/src/bin</directory>
<outputDirectory>bin</outputDirectory>
<fileMode>0755</fileMode>
<includes>
<include>**.sh</include>
<include>**.bat</include>
</includes>
</fileSet>
<!-- 指定输出target/classes中的配置文件到config目录中 -->
<fileSet>
<directory>${basedir}/target/classes</directory>
<outputDirectory>resources</outputDirectory>
<fileMode>0644</fileMode>
<includes>
<include>*.yml</include>
<include>mapper/**/*.xml</include>
<include>static/**</include>
<include>templates/**</include>
<include>python/**</include>
<include>*.xml</include>
<include>*.properties</include>
</includes>
</fileSet>
<!-- executable jar -->
<fileSet>
<directory>${project.build.directory}/lib</directory>
<outputDirectory>${file.separator}lib</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
<fileMode>0755</fileMode>
</fileSet>
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>${file.separator}</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
<fileMode>0755</fileMode>
</fileSet>
</fileSets>
</assembly>
3,进行打包操作
在打包目录下会看到一个 xxx.tar.gz 文件
(1)解压xxx.tar.gz 文件后 会有 lib(第三方jar包),resources(静态资源文件),xxx.jar (spring boot 执行文件)
(2) 用压缩软件打开 解压后的 jar 执行文件 删除 \BOOT-INF\classes 下存放静态文件或者模板文件的文件夹(一般是static和templates)后,关闭压缩文件即可
4,运行 spring boot 项目
启动命令 java -jar -Dloader.path=.,resources,lib rbj-web-1.0.jar
本文详细介绍如何在SpringBoot项目中使用Maven插件进行分离打包,包括配置依赖、资源、可执行jar、springboot管理和打包组装tar.gz,以及创建和配置assembly.xml,最后进行打包操作和运行springboot项目。
1481

被折叠的 条评论
为什么被折叠?



