maven 如何解决因本地jar导致的编译错误

时间:2023-03-09 08:09:45
maven 如何解决因本地jar导致的编译错误

如何解决Maven依赖本地非repository中的jar包,依赖jar包放在WEB-INF/lib等目录下的情况客户端编译出错的处理。http://www.mamicode.com/info-detail-169419.html

Maven提供了scope为system的依赖,文档的原文如下:

system
This scope is similar to provided except thatyou have to provide the JAR which contains it explicitly.
The artifact is always available and is notlooked up in a repository.

这样就可以添加dependency而不需要再将WEB-INF/lib目录下的jar包安装到本地库中了。

具体配置录下:

<dependency>
<groupId>org.apache</groupId>
<artifactId>test</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${basedir}/src/main/webapp/WEB-INF/lib/test.jar</systemPath>
</dependency>

如果只添加一个两个jar包,还比较方便,但是如果WEB-INF/lib/下面有几十个jar包,逐个添加就会显得很繁琐。最好是能方便配置 WEB-INF/lib/目录,让该目录下所有jar包都参与编译。这个配置在maven-compiler-plugin中。配置编译参 数<compilerArguments>,添加extdirs将jar包相对路径添加到配置中,如下:

<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
<compilerArguments>
<extdirs>${basedir}\lib</extdirs>
</compilerArguments>
</configuration>
</plugin>

通过配置maven-compiler-plugin 的compilerArguments可以方便在使用Maven的时候,还大量使用以前使用Ant的大批WEB-INF/lib下面的历史遗留jar包。