注册 X
我已阅读并同意  服务条款
首页 > IT技术笔记 > 查看笔记

springboot打包分离第三方依赖和资源文件

在用springboot开发项目是,打包好的jar包是可以直接运行的,但是通常情况下,这个jar都会很大,因为他默认打包了所有依赖的jar包。此时就可以通过maven-dependency-plugin等插件将依赖包复制到独立的文件夹,在启动jar包的时候引用即可。

Pom文件配置

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>2.1.4.RELEASE</version>

<relativePath/> <!-- lookup parent from repository -->

</parent>

<groupId>com.test</groupId>

<artifactId>test</artifactId>

<version>0.0.1</version>

<name>test</name>

<description>test project for Spring Boot</description>


<properties>

<java.version>1.8</java.version>

</properties>


<dependencies>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-thymeleaf</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>


<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<scope>runtime</scope>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

<scope>test</scope>

</dependency>

<!-- 热部署 -->

    <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-devtools</artifactId>

        <optional>true</optional>

    </dependency>

<dependency> 

<groupId>org.json</groupId> 

<artifactId>json</artifactId> 

<version>20160212</version> 

</dependency>

<dependency>

    <groupId>net.sf.json-lib</groupId>

    <artifactId>json-lib</artifactId>

    <version>2.4</version>

    <classifier>jdk15</classifier>

</dependency>

</dependencies>


<build>

<!-- 打包输出的根目录 -->

<!-- <directory>target/${project.version}</directory> -->

<plugins>

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

<configuration>

                <fork>true</fork>

<!--skip配置 true 剔除spring-boot打包的org和BOOT-INF文件夹(用于子模块打包)  false 不剔除,作为主程序 -->

<skip>false</skip>

<!-- 指定该jar包启动时的主类[建议] -->

<mainClass>com.rshare.RshareApplication</mainClass>

<layout>ZIP</layout>

<includes>

<!-- 排除第三方依赖jar(只保留本项目的jar) -->

<include>

<groupId>${project.groupId}</groupId>

<artifactId>${project.artifactId}</artifactId>

</include>

</includes>               

            </configuration>

</plugin>


<!-- 打源码包 -->

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-source-plugin</artifactId>

                <configuration>

                    <attach>true</attach>

                </configuration>

<executions>

<execution>

<id>attach-sources</id>

<phase>compile</phase>

<goals>

<goal>jar-no-fork</goal>

</goals>

</execution>

</executions>

</plugin>

<!-- 把项目依赖的第三方包打包在target/lib下 -->

<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>${project.build.directory}/lib</outputDirectory>

<excludeTransitive>false</excludeTransitive>

<stripVersion>false</stripVersion>

<includeScope>runtime</includeScope>

</configuration>

</execution>

</executions>

</plugin>

</plugins>

<resources>

<!-- 打包src/main/java下的xml文件 -->

<resource>

<directory>src/main/java</directory>

<includes>

<include>**/*.xml</include>

</includes>

</resource>

<!-- 排除resources下的配置文件 -->

<resource>

<filtering>true</filtering>

<directory>src/main/resources</directory>

<excludes>

<exclude>static/**</exclude>

<exclude>*.yml</exclude>

<exclude>*.properties</exclude>

</excludes>

<targetPath>BOOT-INF/classes/</targetPath>

</resource>

<!-- 打包lib下的jar包 -->

<resource>

<directory>lib</directory>

<targetPath>BOOT-INF/lib/</targetPath>

<includes>

<include>**/*.jar</include>

</includes>

</resource>

</resources>

</build>

</project>

配置说明

  • maven-dependency-plugin:把项目依赖的第三方jar打包在target/lib下面

  • maven-source-plugin:打包源码,配置中指定了phase为compile,意思是在mvn生命周期为compile时将源文件打包,即只要执行的mvn命令包含compile阶段,就会将源代码打包。phase还可以指定为verify、package、install等
    ## 单独的打源码包命令:mvn source:jar 或 mvn source:test-jar(会生成在target目录下)
    ## 打包同时生成源码包:mvn clean source:jar install等
    ## 注意:在多项目构建中,将source-plugin置于顶层或parent的pom中并不会发挥作用,必须置于具体项目的pom中

  • spring-boot-maven-plugin:springboot打包插件,只保留本项目运行的jar包

  • resources:排除了我们需要外置的文件

运行方式

将target下lib包和test.jar(运行包)以及src/main/resources下你需要外置的文件部署至服务器同一目录下,如:

  • lib

  • static

  • test.jar 

  • *.yml

  • *.properties

运行命令:java -jar -Dloader.path=lib test.jar (多个path使用逗号分隔)



 打赏        分享



评论