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>
相关推荐
他҈姓҈林҈3 小时前
使用 Spring Boot 进行开发
spring boot
柏油5 小时前
MySQL InnoDB 行锁
数据库·后端·mysql
咖啡调调。5 小时前
使用Django框架表单
后端·python·django
Java&Develop5 小时前
onloyoffice历史版本功能实现,版本恢复功能,编辑器功能实现 springboot+vue2
前端·spring boot·编辑器
白泽talk5 小时前
2个小时1w字| React & Golang 全栈微服务实战
前端·后端·微服务
摆烂工程师5 小时前
全网最详细的5分钟快速申请一个国际 “edu教育邮箱” 的保姆级教程!
前端·后端·程序员
一只叫煤球的猫5 小时前
你真的会用 return 吗?—— 11个值得借鉴的 return 写法
java·后端·代码规范
Asthenia04126 小时前
HTTP调用超时与重试问题分析
后端
颇有几分姿色6 小时前
Spring Boot 读取配置文件的几种方式
java·spring boot·后端
AntBlack6 小时前
别说了别说了 ,Trae 已经在不停优化迭代了
前端·人工智能·后端