springboot项目打包优化(迁移第三方依赖jar、配置文件到项目jar外)

2021/5/19 10:27:37

本文主要是介绍springboot项目打包优化(迁移第三方依赖jar、配置文件到项目jar外),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

springboot 项目采用spring-boot-maven-plugin插件打包,会把第三方依赖包打进jar \BOOT-INF\lib 目录中,以及配置文件也会打进jar包。

<plugins>
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <version>2.0.1.RELEASE</version>
        <executions>
            <execution>
                <goals>
                    <goal>repackage</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
</plugins>

第一步:优化方式改用 maven-jar-plugin、maven-dependency-plugin 打包可以把
第三方依赖包copy到外部lib目录

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>lib/</classpathPrefix>
                <mainClass>${start.class}</mainClass>
            </manifest>
        </archive>
        <excludes >
            <exclude>*.yml</exclude>
            <exclude>bootstrap*.properties</exclude>
        </excludes>
    </configuration>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <outputDirectory>target/lib</outputDirectory>
                <excludeTransitive>false</excludeTransitive>
                <stripVersion>false</stripVersion>
                <includeScope>runtime</includeScope>
            </configuration>
        </execution>
    </executions>
</plugin>

第二步:把配置文件打包到外部config文件中,在pom.xml添加maven-assembly-plugin插件

<plugin>
   <artifactId>maven-assembly-plugin</artifactId>
     <configuration>
         <appendAssemblyId>false</appendAssemblyId>
         <descriptors>
             <descriptor>build/assembly.xml</descriptor>
         </descriptors>
     </configuration>
     <executions>
         <execution>
             <id>make-assembly</id>
             <phase>package</phase>
             <goals>
                 <goal>single</goal>
             </goals>
         </execution>
     </executions>
 </plugin>

添加 build/assembly.xml 配置

<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>dir</format>
    </formats>
    <includeBaseDirectory>true</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>${basedir}/src/main/resources</directory>
            <includes>
                <include>config</include>
                <include>bootstrap*</include>
            </includes>
            <filtered>true</filtered>
            <outputDirectory>${file.separator}config</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>${project.build.directory}/lib</directory>
            <outputDirectory>${file.separator}lib</outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>${project.build.directory}</directory>
            <outputDirectory>${file.separator}</outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>


这篇关于springboot项目打包优化(迁移第三方依赖jar、配置文件到项目jar外)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程