springboot jar分离部署

springboot jar分离部署

  • 注意:spring boot web项目别使用jsp页面,可以使用模板代替,jsp打包时访问页面会报404错误。

1.具体配置如下:

xml 复制代码
<build>
        <plugins>
            <!--更换maven的jar打包插件
            先前使用的是spring-boot-maven-plugin来打包,这个插件会将项目所有的依赖打入BOOT-INF/lib下,
            替换为maven-jar-plugin-->
            <!-- 1. springboot应用与jar分离部署配置-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.1.1</version>
                <configuration>
                    <archive>
                        <manifest>
                            <!--需要加入到类构建路径-->
                            <addClasspath>true</addClasspath>
                            <!--指定生成的Manifest文件中Class-Path依赖lib前面都加上路径,构建出lib/xx.jar-->
                            <classpathPrefix>lib/</classpathPrefix>
                           <!--程序入口--> <mainClass>com.ms.serviceapi.ServiceapiApplication</mainClass>
                        </manifest>
                    </archive>
                    <!-- 3.排除resources配置文件 在jar同级目录增加配置文件-->
                    <excludes >
                        <exclude>**/*.properties</exclude>
                        <exclude>**/*.xml</exclude>
                        <exclude>**/*.yml</exclude>
                    </excludes>
                </configuration>
            </plugin>
            <!-- 2.拷贝依赖到jar外面的lib目录-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-lib</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>target/lib</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <!--自动化配置实现内容拷贝 可用assembly-->

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>

                        <configuration>
                            <target>
                                <property name="dist" value="target/publish"></property>
                                <property name="dist-tmp" value="${dist}/tmp"></property>
                                <property name="app-name" value="${project.artifactId}-${project.version}"></property>

                                <copy file="target/${app-name}.jar" tofile="${dist}/${app-name}.jar" />
                                <delete dir="${dist}/${app-name}-classes.jar" />

                                <!--web项目启用
                                    <mkdir dir="${dist-tmp}" />
                                    <copy file="target/${app-name}.jar" tofile="${dist-tmp}/${app-name}.jar" />
                                    <unzip src="${dist-tmp}/${app-name}.jar" dest="${dist-tmp}" />
                                    <zip destfile="${dist}/${app-name}-pages.jar">
                                    <zipfileset dir="${dist-tmp}/META-INF" prefix="META-INF" />
                                    <zipfileset dir="target/classes/static" prefix="static" />
                                    <zipfileset dir="target/classes/templates" prefix="templates" />
                                </zip>-->
                                <delete dir="${dist-tmp}" />

                                <move todir="${dist}/lib">
                                    <fileset dir="target/lib" />
                                </move>

                                <copy todir="${dist}">
                                    <fileset dir="target/classes">
                                        <include name="**/*.properties" />
                                        <include name="**/*.xml" />
                                        <include name="**/*.yml" />
                                    </fileset>
                                </copy>
                            </target>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <!--属性替换-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <delimiters>
                        <!-- 指定过滤的表达式
                            <delimit>$</delimit> : 表示 过滤开头为 $ 结束为 $ 的内容,例如 $project.versionb$
                            <delimit>${*}</delimit> 表示 过滤${}包裹的内容,例如 ${project.vserion}
                         -->
                        <delimit>${*}</delimit>
                    </delimiters>
                </configuration>
            </plugin>
            <!--4.启动项目 java -jar -Dloader.path=.,lib xx.jar --debug 查看项目日志 -->
        </plugins>
    </build>

2.打包目录如下:

txt 复制代码
lib
xxx.yml
xxx-1.0.jar

3.运行:

shell 复制代码
java -jar -Dloader.path=lib xxx.jar

4.assembly复制打包后的文件

xml 复制代码
<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <!-- The configuration of the plugin -->
                <configuration>
                    <!-- Specifies the configuration file of the assembly plugin -->
                    <descriptors>
                        <descriptor>src/main/resources/assembly/package.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

4.1 package.xml配置文件

xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<assembly
        xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
    <id>package</id>
    <formats>
        <format>tar.gz</format>
        <format>dir</format>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>true</includeBaseDirectory>
    <!--依赖jar包以及项目打包文件存储文件-->
    <dependencySets>
        <dependencySet>
            <!--存储在projectName-assembly-version/lib下-->
            <outputDirectory>lib</outputDirectory>
        </dependencySet>
    </dependencySets>
    
    <fileSets>
        <fileSet>
            <directory>${project.build.directory}/classes</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>**/*.properties</include>
            </includes>
        </fileSet>
 
        <!-- 把项目自己编译出来的jar文件,打包进zip文件的根目录 -->
        <fileSet>
            <directory>${project.build.directory}</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>
    </fileSets>
     <files>
        <!--针对单个文件-->
        <file>
            <!--源文件地址,相对于项目地址-->
            <source>pom.xml</source>
            <!--输出目录为projectName-assembly-version/-->
            <outputDirectory>.</outputDirectory>
            <!--文件的权限-->
            <fileMode>0755</fileMode>
            <!--重命名为-->
            <destName>pom.xml</destName>
        </file>
    </files>
</assembly>
相关推荐
有个傻瓜2 小时前
分页 total 对不上、列表还少几条:MyBatis-Plus 一对多 LEFT JOIN 分页错乱复盘与四种写法实测
spring boot·sql优化·mybatis-plus·分页查询·踩坑复盘
考虑考虑3 小时前
JDK27结构化并发
java·后端·java ee
沐言人生3 小时前
自媒体人的效率神器!监控、采集、下载、发布,这个免费开源工具全拿下!
前端·vue.js·后端
我叫黑大帅5 小时前
拒绝加字段堆屎山!利用位掩码与流程编排优雅落地 上图下文 复合状态
后端·算法·面试
一条Go5 小时前
Grafana 面板嵌入:为什么 kiosk 必须是「裸参数」
后端·github
Cosolar6 小时前
监控指标全正常,订单接口从 80ms 涨到 4s 故障复盘
java·后端·面试
代码丰7 小时前
不依赖 LangGraph,也能实现 Human-in-the-loop:基于数据库状态机的人机协作工作流-CSDN博客
后端·架构
IT_陈寒8 小时前
Java实战技巧总结:当你的finally块没有执行时会发生什么?
前端·人工智能·后端
Csvn8 小时前
Nginx 与 OpenResty:阶段钩子、shared dict 与动态 upstream/证书
后端·nginx
geovindu9 小时前
rust: Functional Options Pattern
开发语言·后端·设计模式·rust·函数式选项模式·惯用法型模式