解决idea使用maven打包时无法将本地lib库文件和resource目录中的资源文件打包进jar文件的问题!!!

一、问题复现

1)项目结构如下

我们看到项目中手动添加了本地lib资源,同时bootspring的配置文件和mapper文件也放在了resouces目录中。

2)上述结构的项目在使用maven打包时,最终生成的jar文件中将不包含lib库文件,甚至有时resouces目录中的资源文件也会缺失。

生成的jar包如下(缺失了lib和resources资源文件):

上述jar包在运行启动时会报错:

二、解决办法

在项目的pom.xml文件中添加编译选项,显示指定打包时需要包含的目录。

YAML 复制代码
<?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">
    <parent>
        <groupId>com.ruoyi</groupId>
        <artifactId>ypt-platform</artifactId>
        <version>3.6.3</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.ruoyi.ypt</groupId>
    <artifactId>ypt-platform-water</artifactId>

    <description>
        xxxx模块
    </description>

    <dependencies>
        <!-- SpringCloud Alibaba Nacos -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

        <!-- SpringCloud Alibaba Nacos Config -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>

        <!-- SpringCloud Alibaba Sentinel -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>

        <!-- SpringBoot Actuator -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!-- Swagger UI -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>${swagger.fox.version}</version>
        </dependency>

        <!-- Mysql Connector -->
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
        </dependency>

        <!-- Ruoyi Common Redis -->
        <dependency>
            <groupId>com.ruoyi</groupId>
            <artifactId>ruoyi-common-redis</artifactId>
        </dependency>

        <!-- RuoYi Common DataSource -->
        <dependency>
            <groupId>com.ruoyi</groupId>
            <artifactId>ruoyi-common-datasource</artifactId>
        </dependency>

        <!-- spring cloud openfeign -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

        <!-- RuoYi Common DataScope -->
        <dependency>
            <groupId>com.ruoyi</groupId>
            <artifactId>ruoyi-common-datascope</artifactId>
        </dependency>

        <!-- RuoYi Common Log -->
        <dependency>
            <groupId>com.ruoyi</groupId>
            <artifactId>ruoyi-common-log</artifactId>
        </dependency>

        <!-- RuoYi Common Swagger -->
        <dependency>
            <groupId>com.ruoyi</groupId>
            <artifactId>ruoyi-common-swagger</artifactId>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.12</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <!-- 引入Apache数学函数库 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-math3</artifactId>
            <version>3.6.1</version>
        </dependency>

        <!-- 引入本地openplant(SIS)实时数据库访问类 -->
        <dependency>
            <groupId>op.net</groupId>
            <artifactId>com.magus.net</artifactId>
            <version>1.0</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/op.net.jar</systemPath>
        </dependency>
    </dependencies>

    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>

        <resources>
            <!-- 将本地lib库打包到最终的jar发布文件中 -->
            <resource>
                <directory>${project.basedir}/lib</directory>
                <targetPath>BOOT-INF/lib/</targetPath>
                <includes>
                    <include>**/*.jar</include>
                </includes>
            </resource>
            <!-- 将resource目录中的资源打包到最终的jar发布文件中 -->
            <resource>
                <directory>${project.basedir}/src/main/resources</directory>
                <targetPath>BOOT-INF/classes/</targetPath>
                <includes>
                    <include>**/*.*</include>
                </includes>
            </resource>
        </resources>
    </build>

</project>

重新打包后的jar包内容如下:

运行结果正常:

相关推荐
u0147237301 小时前
java中 如何从jar中读取资源文件?
java·jar
牧子与羊1 小时前
idea下java的maven项目编译内存溢出GC overhead limit exceeded解决办法
java·maven·intellij-idea
栗筝i5 小时前
Maven 中的依赖管理机制
java·maven
Conmi·白小丑5 小时前
Conmi的正确答案——Maven加载时检测到的漏洞修复
java·maven
jzlhll1237 小时前
通过gradle发布aar或jar携带sources-jar到maven nexus
python·maven·jar
MasterNeverDown7 小时前
IDEA maven生存期中package和插件中的jar有什么区别
java·maven·intellij-idea·jar
XiaoH2331 天前
培训机构Day23
servlet·tomcat·maven
Particle.1 天前
IDEA中Maven依赖包导入失败报红的潜在原因
java·maven·intellij-idea
silence2501 天前
理解 maven-jar-plugin:如何使用 Add-Opens 配置解决 Java 模块访问问题
maven
莫问alicia1 天前
苍穹外卖 项目记录 day03
java·开发语言·spring boot·maven