Springboot依赖打包分离及第三方包

我们可以在Springboot框架中使用Maven插件来管理我们的依赖。

1、正常打包

会将所有的依赖一起打入jar包

<plugins>
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
</plugins>
2、依赖和代码分离
<plugins>

    <!--配置是否依赖分离-->
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
            <layout>ZIP</layout>
            <!-- 注释后和依赖包一起打包 取消注释则不打包依赖-->
            <includes>
                <include>
                    <groupId>nothing</groupId>
                    <artifactId>nothing</artifactId>
                </include>
            </includes>
        </configuration>
    </plugin>

    <!--指定启动类-->
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
            <archive>
                <manifest>
                    <addClasspath>true</addClasspath>
                    <classpathPrefix>lib/</classpathPrefix>
                    <mainClass>com.zl.Application</mainClass>
                </manifest>
            </archive>
        </configuration>
    </plugin>

    <!--复制依赖到target/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-dependencies</goal>
                </goals>
                <configuration>
                    <outputDirectory>target/lib</outputDirectory>
                    <excludeTransitive>false</excludeTransitive>
                    <stripVersion>false</stripVersion>
                    <includeScope>runtime</includeScope>
                </configuration>
            </execution>
        </executions>
    </plugin>
</plugins>
3、集成第三方包

将第三方包放到项目根目录的lib文件夹下

<dependencies>
    <!-- 用systemPath指定第三方包的路径 -->
    <dependency>
        <groupId>com.zl</groupId>
        <artifactId>com.zl</artifactId>
        <version>1.0</version>
        <scope>system</scope>
        <systemPath>${project.basedir}/lib/sapjco.jar</systemPath>
    </dependency>

</dependencies>

<build>
    <resources>
        <!-- 加入如下配置,把jar包以资源形式打包进指定路径 -->
        <resource>
            <directory>${project.basedir}/lib</directory>
            <targetPath>BOOT-INF/lib/</targetPath>
            <includes>
                <include>**/*.jar</include>
            </includes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <targetPath>BOOT-INF/classes/</targetPath>
        </resource>
    </resources>

    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>2.5.7</version>
        </plugin>
    </plugins>
</build>

推荐阅读更多精彩内容