Eclipse Maven 依赖 jar 变灰,无法从中导入类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51372670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 23:33:34  来源:igfitidea点击:

Eclipse Maven dependency jar grayed out, can't import classes from it

eclipsemavenm2eclipse

提问by CustardBun

I'm helping a friend configure a maven project with m2eclipse for the first time. We're both pretty unfamiliar with it and are encountering an issue where even though a dependency jar is showing up with packages in it under "maven dependencies" in the Project directory, if we try to import anything from any of that jar's packages, it can't find the class.

第一次帮朋友用m2eclipse配置maven项目。我们都非常不熟悉它,并且遇到了一个问题,即使依赖 jar 显示在项目目录中的“maven 依赖项”下的包中,如果我们尝试从该 jar 的任何包中导入任何内容,它找不到班级。

I noticed that the jars that are having issues are gray and not as opaque as the rest of the jars that are working.

我注意到有问题的罐子是灰色的,不像其他正在工作的罐子那样不透明。

What's strange is if you hover of the class name in the import, it shows a brief description of the class (from the documentation in the jar!) but it won't let me import it. All the other maven dependencies can be imported fine. Any ideas? We can't seem to even find what the darker icon means.

奇怪的是,如果您将鼠标悬停在导入中的类名上,它会显示该类的简要说明(来自 jar 中的文档!)但它不会让我导入它。所有其他 Maven 依赖项都可以正常导入。有任何想法吗?我们似乎甚至无法找到较暗的图标意味着什么。

enter image description hereenter image description here

在此处输入图片说明在此处输入图片说明

Also, the pom.xml is dead simple:

此外,pom.xml 非常简单:

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0

  <groupId>com.something.portal.test</groupId>
  <artifactId>PortalFrontEndTests</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>PortalFrontEndTests</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <!-- Selenium -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>2.53.1</version>
    </dependency>

    <!-- TestNG -->
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.11</version>
        <scope>test</scope>
    </dependency>

  </dependencies>
</project>

I'm not sure what I'm missing here

我不确定我在这里错过了什么

回答by CustardBun

I found the issue. It was because I had the class in the source directory instead of the test directory and both of the maven dependencies had been marked as "Visible only to test"

我发现了这个问题。这是因为我在源目录而不是测试目录中有该类,并且两个 maven 依赖项都被标记为“仅测试可见”

回答by Vishnu M. D.

open your pom.xml file check for the name of the grayed out jar file change

打开你的 pom.xml 文件检查变灰的 jar 文件的名称更改

<scope>test</scope>

to

<scope>compile</scope>

回答by Harish kumar NG

check for your dependency scope in POM file

检查 POM 文件中的依赖范围

compile, provided, system and test these were the available test

编译、提供、系统和测试这些是可用的测试

test -> compile would change your dependencies from grey to white.

test -> compile 会将您的依赖项从灰色更改为白色。

If your dependency is for test scope then that dependency is not available for normal use in application whereas compile scope sets that dependency in class path of your project.

如果您的依赖项用于测试范围,那么该依赖项在应用程序中无法正常使用,而编译范围会在您的项目的类路径中设置该依赖项。

回答by springe

I had the same problem when i used the <scope>test</scope>in the maven pom.

当我<scope>test</scope>在 maven pom 中使用时,我遇到了同样的问题。

It seems as if the newer Eclipse/Java versions do have a new Attribute :

似乎较新的 Eclipse/Java 版本确实有一个新的 Attribute :

<classpathentry kind="src" output="target/test-classes" path="src/test/java/...">
    <attributes>
        <attribute name="test" value="true"/>
    </attributes>
</classpathentry>

This should be enabled in the Java Build Path Settings:

这应该在 Java 构建路径设置中启用:

Image showing "Containts test sources" option from build path menu

显示构建路径菜单中的“包含测试源”选项的图像

After enabling this i got rid of all the compiler errors.

启用此功能后,我摆脱了所有编译器错误。

回答by Ashvini_Sharma

I am not sure on the grayed out part. If this is the feature because it suggest that Testing class should be under /test rather /src. However, solution to your problem is scope of plugin, change it to compile and you will be good to go. i.e. replace test with compile: <scope>test</scope><scope>compile</scope>

我不确定灰色部分。如果这是功能,因为它建议测试类应该在 /test 下而不是 /src 下。但是,您的问题的解决方案是插件的范围,将其更改为 compile 就可以了。即用编译替换测试: <scope>test</scope><scope>compile</scope>

That's it. you will not get any error for import testing packages.

就是这样。导入测试包不会出现任何错误。