问题背景
项目中需要用到远程资源库中不存在的jar包,将jar包放在项目目录下,并用<systemPath>的方式引用,windows系统本地调试的时候没问题,部署到Linux能成功启动,但编译时报警告[WARNING] 'dependencies.dependency.systemPath',运行时找不到类java.lang.NoClassDefError。
解决方案
1.指定${pom.basedir}
有网友提出指定${project.basedir},本人测试无效。
代码如下:
<dependency>
<groupId>com.itextpdf.pdfoffice</groupId>
<artifactId>pdfoffice</artifactId>
<version>2.0.2</version>
<scope>system</scope>
<systemPath>${pom.basedir}/lib/pdfoffice-2.0.2.jar</systemPath>
</dependency>
编译时警告解决了,但运行时仍然找不到类
2.将jar包发布到部署服务器的本地maven库
(1)在pom文件中编辑代码发布jar包,部署
(2)在pom文件中直引用jar包,部署
注意以上两步操作要分别修改部署两次,因为第一步是发包,第二步是引包,若一次修改完成直接部署,jar包还没发布上去,仍然会去远程资源库下载,还是会报错。
代码如下:
(1)发布jar包,此段代码jar包发布完成后可删除。
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
<executions>
<execution>
<id>install-pdfoffice</id>
<phase>clean</phase>
<configuration>
<file>${pom.basedir}/lib/pdfoffice-2.0.2.jar</file>
<repositoryLayout>default</repositoryLayout>
<groupId>com.itextpdf.pdfoffice</groupId>
<artifactId>pdfoffice</artifactId>
<version>2.0.2</version>
<packaging>jar</packaging>
<generatePom>true</generatePom>
</configuration>
<goals>
<goal>install-file</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
(2)直接引用,无需指定<systemPath>
<dependency>
<groupId>com.itextpdf.pdfoffice</groupId>
<artifactId>pdfoffice</artifactId>
<version>2.0.2</version>
</dependency>
至此,问题解决。
声明:本文引用的jar包为付费服务,仅供学习探讨,商业用途需购买许可。