springboot项目将第三方jar、配置文件打包到jar包外部

springboot项目默认打成一个jar包,在多环境时不友好,需要将依赖的第三方jar及resources目录下的配置文件打包到与jar包同级目录下,方便环境变更,具体操作如下:

打包到外部目录

  1. pom.xml需修改

     <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <appendAssemblyId>false</appendAssemblyId>
                    <descriptors>
                        <descriptor>src/main/assembly/assembly.xml</descriptor>
                    </descriptors>
                    <outputDirectory>${project.build.directory}/gwall/</outputDirectory>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
    
            <!-- 打包成jar文件,并指定lib文件夹以及resources资源文件夹 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.xxl.job.admin.XxlJobAdminApplication</mainClass>
                            <!--依赖前缀 -->
                            <classpathPrefix>lib/</classpathPrefix>
                            <addClasspath>true</addClasspath>
                        </manifest>
                        <manifestEntries>
                            <Class-Path>resources/</Class-Path>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
  2. 添加配置文件
    image.png
    assembly.xml文件内容如下:

    <assembly
    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    
    <id>distribution</id>
    
    <!--输出格式 zip 最终结果生成zip -->
    <formats>
        <format>tar.gz</format>
    </formats>
    
    <includeBaseDirectory>false</includeBaseDirectory>
    
    <!--设置需要输出文件 -->
    <fileSets>
        <fileSet>
            <directory>src/main/bin</directory>
            <outputDirectory>/</outputDirectory>
            <fileMode>0755</fileMode>
        </fileSet>
        <fileSet>
            <directory>src/main/resources/</directory>
            <outputDirectory>/resources</outputDirectory>
            <fileMode>0644</fileMode>
        </fileSet>
    </fileSets>
    
    <dependencySets>
        <dependencySet>
            <!--依赖包的输出目录 -->
            <outputDirectory>/lib</outputDirectory>
            <scope>runtime</scope>
            <excludes>
                <exclude>${project.groupId}:${project.artifactId}</exclude>
            </excludes>
        </dependencySet>
        <dependencySet>
            <!--jar包的输出目录 -->
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>${project.groupId}:${project.artifactId}</include>
            </includes>
        </dependencySet>
    </dependencySets></assembly>

    start.sh文件内容如下:

    #!/bin/bash
    curpath=`pwd`
    app=xxl-job-admin-2.1.0.jar
    echo 'curpath:$curpath    app:$app'
    ps -aux|grep $curpath/$app|grep -v grep|awk '{print $2}'|xargs -r kill -9
    nohup java  -jar $curpath/$app > $curpath/nohup.out 2>&1 &

    start.bat文件内容如下:

    @echo off & setlocal enabledelayedexpansion
    java -jar xxl-job-admin-2.1.0.jar
    pause

    最终结果如下:

    image.png
    image.png
    image.png

    注意问题

    logback不生效

    主要是使用logback-spring.xml
    另外需要在application.yml中配置

     logging:
       config: config/logback-spring.xml

    参见:
    https://my.oschina.net/u/2022...

阅读 1k

推荐阅读