简体   繁体   English

在Maven本地存储库中安装的外部jar上无法解析类导入

[英]class import not resolved on external jars installed in maven local repository

I am using maven 3.2.5. 我正在使用Maven 3.2.5。 I have one external jars which I need to use in my maven project which is not available in maven repository. 我有一个需要在我的maven项目中使用的外部jar,maven存储库中不可用。

I installed those jars by using following command: 我使用以下命令安装了这些罐子:

1) mvn install:install-file -Dfile=p-unit-0.15.319.jar -DgroupId=org.punit -DartifactId=p-unit -Dversion=0.15.319 -Dpackaging=jar 1) mvn install:install-file -Dfile=p-unit-0.15.319.jar -DgroupId=org.punit -DartifactId=p-unit -Dversion=0.15.319 -Dpackaging=jar

2) After this command, I saw in my M2 repository, jar & pom was created .m2\\repository\\org\\punit\\p-unit\\0.15.319\\p-unit-0.15.319.jar .m2\\repository\\org\\punit\\p-unit\\0.15.319\\p-unit-0.15.319.pom 2)执行此命令后,我看到在M2存储库中创建了jar和pom .m2 \\ repository \\ org \\ punit \\ p-unit \\ 0.15.319 \\ p-unit-0.15.319.jar .m2 \\ repository \\ org \\ punit \\ p-unit \\ 0.15.319 \\ p-unit-0.15.319.pom

created pom content: 创建的pom内容:

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.punit</groupId>
  <artifactId>p-unit</artifactId>
  <version>0.15.319</version>
  <description>POM was created from install:install-file</description>
</project>

3) Updated the pom file 3)更新了pom文件

<dependency>
  <groupId>org.punit</groupId>
  <artifactId>p-unit</artifactId>
  <version>0.15.319</version>   
  <scope>provided</scope>                                         
</dependency>

Issue: When I tried to use class of this jar, 问题:当我尝试使用此jar的类时,

 import org.punit.runner.*;
 getting error: import org.punit.runner can't be resolved.

I tried many combination to define the dependency but not able to use the classes. 我尝试了多种组合来定义依赖关系,但无法使用这些类。

How can I resolve it?? 我该如何解决?

Add the corresponding dependency to the pom.xml file of your project under the tag dependencies. 在标签依赖项下,将相应的依赖项添加到项目的pom.xml文件中。

Example : 范例:

<dependencies>
    <dependency>
      <groupId>org.punit</groupId>
      <artifactId>p-unit</artifactId>
      <version>0.15.319</version>   
      <scope>test</scope>
    </dependency>
</dependencies>

Note : assuming the group,artifact ids and version are correct as mentioned in the question. 注意 :假设问题中提到的组,工件ID和版本是正确的。

One thing which I missed, I am mentioning here, which solved this issue. 我在这里提到的我错过的一件事解决了这个问题。 I had put the dependency in wrong profile which was not called during my compilation. 我将依赖项放在错误的配置文件中,该配置文件在编译期间未调用。 When I put in correct profile it worked. 当我输入正确的配置文件时,它可以工作。

Second option: 第二种选择:

We can directly add external jar through eclipse: 我们可以通过eclipse直接添加外部jar:

Properties -> Java Build Path -> Libraries -> Add External Jars. 属性-> Java构建路径->库->添加外部Jar。

and locate the library on the file system. 并在文件系统上找到该库。

Answer to similar query: The import org.junit cannot be resolved 回答类似的问题: 无法解析导入org.junit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM