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

Cus*_*Bun 13 eclipse m2eclipse maven

我正在帮助一位朋友第一次使用m2eclipse配置maven项目.我们都非常不熟悉它,并且遇到一个问题,即使依赖jar在Project目录中的"maven依赖项"下显示其中的包,如果我们尝试从任何jar包中导入任何东西,它找不到班级.

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

奇怪的是,如果你在导入中悬停类名,它会显示该类的简要描述(来自jar中的文档!),但它不会让我导入它.所有其他maven依赖项都可以正常导入.有任何想法吗?我们似乎无法找到更暗的图标意味着什么.

在此输入图像描述 在此输入图像描述

此外,pom.xml很简单:

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>
Run Code Online (Sandbox Code Playgroud)

我不确定我在这里缺少什么

Cus*_*Bun 15

我发现了这个问题.这是因为我在源目录而不是测试目录中有类,并且两个maven依赖项都被标记为"Visible only to test"


Vis*_* D. 11

打开您的pom.xml文件,检查变灰的jar文件的名称

<scope>test</scope>
Run Code Online (Sandbox Code Playgroud)

<scope>compile</scope>
Run Code Online (Sandbox Code Playgroud)


spr*_*nge 5

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

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

<classpathentry kind="src" output="target/test-classes" path="src/test/java/...">
    <attributes>
        <attribute name="test" value="true"/>
    </attributes>
</classpathentry>
Run Code Online (Sandbox Code Playgroud)

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

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

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


小智 5

检查 POM 文件中的依赖范围

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

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

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