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>
相关推荐
吃海鲜的骆驼14 分钟前
服务异步通讯与RabbitMQ
java·分布式·后端·rabbitmq
m0_7482336416 分钟前
RabbitMQ 进阶
android·前端·后端
m0_7482386325 分钟前
Spring Boot项目接收前端参数的11种方式
前端·spring boot·后端
桦说编程27 分钟前
【硬核总结】如何轻松实现只计算一次、惰性求值?良性竞争条件的广泛使用可能超过你的想象!String实际上是可变的?
后端·函数式编程
菜鸟阿达1 小时前
spring boot 2.7 + seata +微服务 降级失败问题修复
spring boot·后端·微服务
清河__2 小时前
【Go】十七、grpc 服务的具体功能编写
开发语言·后端·golang
张声录12 小时前
国密算法Sm2工具类--golang实现版
开发语言·后端·golang
張葒兵3 小时前
记一次命令行启动springboot项目的问题 java -jar的问题
java·spring boot·jar
一小路一10 小时前
从0-1学习Mysql第五章: 索引与优化
数据库·后端·学习·mysql·面试
mah66610 小时前
SpringBoot项目连接Oracle视图报错整理
spring boot·后端·oracle