Springboot 打jar包分离lib,配置文件正确方式

Posted 楼下有位

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Springboot 打jar包分离lib,配置文件正确方式相关的知识,希望对你有一定的参考价值。

文章来源:https://my.oschina.net/xiaozhutefannao/blog/1932764

POM.xml

<?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>

    <groupId>com.elvish</groupId>
    <artifactId>test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

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

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
        <relativePath />
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
    </dependencies>

    <build>
        <finalName>test</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </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>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <layout>ZIP</layout>
                    <includes>
                        <include>
                            <groupId>cn.jstars</groupId>
                            <artifactId>datatocloud</artifactId>
                        </include>
                    </includes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <filtering>true</filtering>
                <directory>src/main/resources</directory>
                <excludes>
                    <exclude>static/**</exclude>
                    <exclude>templates/**</exclude>
                    <exclude>*.yml</exclude>
                    <exclude>*.properties</exclude>
                    <exclude>*.xml</exclude>
                    <exclude>*.txt</exclude>
                </excludes>
            </resource>
        </resources>
    </build>

</project>

解释说明

  • maven-dependency-plugin 打出项目依赖的第三方包,放在lib下面
  • spring-boot-maven-plugin springboot打包插件,只保留了项目运行的jar包
  • resources 排除了我们需要外置的文件

运行方式

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

  • lib
  • test.jar
  • *.yml
  • *.xml
  • *.properties
  • static
  • templates

最终运行

java -jar -Dloader.path=.,lib test.jar

完美运行,觉得不错的,点赞一波,收藏一波吧

以上是关于Springboot 打jar包分离lib,配置文件正确方式的主要内容,如果未能解决你的问题,请参考以下文章

springboot打包依赖包和配置文件分离

spring boot 打jar包分离lib和resources

项目打包(jar)和依赖(lib)分离

SpringBoot maven项目打jar包并部署到 linux/windows服务

SpringBoot maven项目打jar包并部署到 linux/windows服务

SpringBoot 打包静态资源和 jar包分离怎么配置?