Intellij IDEA中Maven项目添加外部jar依赖后maven install报错如何解决
问题原因
你在IDEA中通过Project Structure手动添加的外部Jar,仅会被IDEA自身的构建类路径识别,Maven的构建生命周期完全独立,仅会读取pom.xml中声明的依赖,因此执行mvn install时无法找到对应依赖包,抛出包不存在错误。
解决方法
方法1(推荐):将外部Jar安装到本地Maven仓库,正常引入依赖
- 执行如下Maven命令将本地Jar安装到本地仓库,按你的实际情况修改命令中参数:
mvn install:install-file -Dfile=<你的外部Jar文件绝对路径> -DgroupId=<自定义groupId,比如com.ph.custom> -DartifactId=<自定义artifactId,比如custom-utils> -Dversion=<自定义版本号,比如1.0.0> -Dpackaging=jar
- 在
pom.xml的dependencies节点下添加对应依赖:
<dependency> <groupId>你刚才自定义的groupId</groupId> <artifactId>你刚才自定义的artifactId</artifactId> <version>你刚才自定义的版本号</version> </dependency>
方法2:使用system scope引入项目内的Jar
如果你不想安装Jar到本地仓库,可以用该方案:
- 在你的项目根目录新建
lib文件夹,把外部Jar放到该文件夹下 - 在
pom.xml的dependencies节点下添加依赖,按实际情况修改参数:
<dependency> <groupId>自定义groupId</groupId> <artifactId>自定义artifactId</artifactId> <version>自定义版本号</version> <scope>system</scope> <systemPath>${project.basedir}/lib/你的Jar文件名.jar</systemPath> </dependency>
- 因为你当前pom中
maven-dependency-plugin的copy-dependencies配置默认不会复制system scope的依赖,需要修改该插件的配置,新增includeSystemScope参数:
<plugin> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy-libs</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.parent.basedir}/target/lib</outputDirectory> <excludeScope>provided</excludeScope> <!-- 新增这一行,允许复制system scope的依赖 --> <includeSystemScope>true</includeSystemScope> </configuration> </execution> </executions> </plugin>
内容的提问来源于stack exchange,提问作者Praveen
相关产品推荐
相关产品推荐

