推广 热搜: csgo  vue  angelababy  2023  gps  新车  htc  落地  app  p2p 

SpringBoot项目打包分离lib,配置和资源文件部署总结

   2023-08-07 网络整理佚名1500
核心提示:先前发布项目的时候,每次改动一点东西,就需要将整个项目重新打包部署,并且打包出来的jar包太庞大,每次更新项目的时候,需要上传的文件很大,十分不便,故把依赖lib里面的jar包从项目分离出来,每次部署只需要发布代码即可。这里引入一种自动化配置,将所有东西打成zip文件,直接发布到服务目录,解压后,即可启动。.xml配置,该文件放在目录下面即可。

之前项目发布的时候,每次有改动,整个项目都需要重新打包部署,而且打包的jar包太大。 每次项目更新时,需要上传的文件都非常大,非常不方便,所以将依赖lib中的jar包从项目中分离出来,每次部署时只需要发布代码即可。

之前使用-boot-maven-来打包,这个插件会将项目的所有依赖放入BOOT-INF/lib并替换为maven-jar-

# 原来使用的打包插件:spring-boot-maven-plugin
<build>
  	<plugins>
        <plugin>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-maven-pluginartifactId>
            <version>2.1.1.RELEASEversion>
            <executions>
                <execution>
                    <goals>
                        <goal>repackagegoal>
                    goals>
                execution>
            executions>
        plugin>
    plugins>
build>
复制代码

半自动化部署

1. 替换为maven-jar-:

<build>
   <plugins>
        <plugin>
            <groupId>org.apache.maven.pluginsgroupId>
            <artifactId>maven-jar-pluginartifactId>
            <version>3.1.1version>
            <configuration>
                <archive>
                	
                    <addMavenDescriptor>falseaddMavenDescriptor>
                    <manifest>
                        <addClasspath>trueaddClasspath>
                        <classpathPrefix>lib/classpathPrefix>
                        
                        <mainClass>com.cecjx.business.BusinessApplicationmainClass> 
                    manifest>
                archive>
            configuration>
        plugin>
    plugins>
build>
复制代码

2、复制依赖jar外的lib目录

<build>
   <plugins>
        <plugin>
            <groupId>org.apache.maven.pluginsgroupId>
            <artifactId>maven-dependency-pluginartifactId>
            <executions>
                <execution>
                    <id>copy-libid>
                    <phase>packagephase>
                    <goals>
                        <goal>copy-dependenciesgoal>
                    goals>
                    <configuration>
                        <outputDirectory>target/liboutputDirectory>
                        <excludeTransitive>falseexcludeTransitive>
                        <stripVersion>falsestripVersion>
                        <includeScope>runtimeincludeScope>
                    configuration>
                execution>
            executions>
        plugin>
    plugins>
build>
复制代码

3、新建一个与jar包同级的目录,放入.yml文件中。 看到这里可能有些朋友会有疑问。 打包好的jar里面不应该有一个.yml文件吗? 为什么要在这里再放一份副本? 这是因为读取配置是有优先级的,jar包外的目录优先级最高,主要是为了方便外部修改配置,而不是去改变jar包内的.yml文件。优先级如下:(1 ) 当前目录下 (2) 当前目录下 (3) 根目录下 (4)

4 在启动项目中添加--debug可以看到更详细的启动日志:java -jar xxx-1.0.0.jar --debug

全自动部署

上述步骤中,需要手动复制.yml文件,jar包内外都有配置。 这里引入了一个自动配置,将所有内容制作成一个zip文件,直接发布到服务目录中。 解压后即可启动。 1. 替换为maven-jar-:

<build>
   <plugins>
        <plugin>
            <groupId>org.apache.maven.pluginsgroupId>
            <artifactId>maven-jar-pluginartifactId>
            <version>3.1.1version>
            <configuration>
                <archive>
                	
                    <addMavenDescriptor>falseaddMavenDescriptor>
                    <manifest>
                        <addClasspath>trueaddClasspath>
                        <classpathPrefix>lib/classpathPrefix>
                        
                        <mainClass>com.cecjx.business.BusinessApplicationmainClass> 
                    manifest>
                archive>
            configuration>
        plugin>
    plugins>
build>
复制代码

2、复制依赖jar外的lib目录

<build>
   <plugins>
        <plugin>
            <groupId>org.apache.maven.pluginsgroupId>
            <artifactId>maven-dependency-pluginartifactId>
            <executions>
                <execution>
                    <id>copy-libid>
                    <phase>packagephase>
                    <goals>
                        <goal>copy-dependenciesgoal>
                    goals>
                    <configuration>
                        <outputDirectory>target/liboutputDirectory>
                        <excludeTransitive>falseexcludeTransitive>
                        <stripVersion>falsestripVersion>
                        <includeScope>runtimeincludeScope>
                    configuration>
                execution>
            executions>
        plugin>
    plugins>
build>
复制代码

3、排除下面的yml(因为我们需要放在jar外面,不能让jar打包插件将其打包到jar包中)

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-pluginartifactId>
            <configuration>
                
                <appendAssemblyId>falseappendAssemblyId>
                
                <finalName>djys-businessfinalName>
                
                <outputDirectory>target/outputDirectory>
                <descriptors>
                    
                    <descriptor>src/main/resources/assembly.xmldescriptor>
                descriptors>
            configuration>
            <executions>
                
                <execution>
                    
                    <id>make-assemblyid>
                    
                    <phase>packagephase>
                    <goals>
                        
                        <goal>singlegoal>
                    goals>
                execution>
            executions>
        plugin>
    plugins>
build>
复制代码

.xml配置,文件放在目录下即可。

<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
    <id>packageid>
    <formats>
        <format>zipformat>
    formats>
    <includebaseDirectory>trueincludebaseDirectory>
    <fileSets>
        
        <fileSet>
            <directory>${basedir}/src/main/resourcesdirectory>
            <includes>
                <include>*.ymlinclude>
            includes>
            <filtered>truefiltered>
            <outputDirectory>${file.separator}configoutputDirectory>
        fileSet>
		
        <fileSet>
            <directory>${project.build.directory}/libdirectory>
            <outputDirectory>${file.separator}liboutputDirectory>
            
            <includes>
                <include>*.jarinclude>
            includes>
        fileSet>
		
        <fileSet>
            <directory>${project.build.directory}directory>
            <outputDirectory>${file.separator}outputDirectory>
            <includes>
                <include>*.jarinclude>
            includes>
        fileSet>
    fileSets>
assembly>
复制代码

4.解压zip并启动

 
标签: lib
反对 0举报 0 收藏 0 打赏 0评论 0
 
更多>同类资讯
推荐图文
推荐资讯
点击排行
网站首页  |  关于我们  |  联系方式  |  使用协议  |  版权隐私  |  网站地图  |  排名推广  |  广告服务  |  积分换礼  |  网站留言  |  RSS订阅  |  违规举报
Powered By DESTOON