一、问题复现
1)项目结构如下
我们看到项目中手动添加了本地lib资源,同时bootspring的配置文件和mapper文件也放在了resouces目录中。
2)上述结构的项目在使用maven打包时,最终生成的jar文件中将不包含lib库文件,甚至有时resouces目录中的资源文件也会缺失。
生成的jar包如下(缺失了lib和resources资源文件):
上述jar包在运行启动时会报错:
二、解决办法
在项目的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"><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包内容如下:
运行结果正常: