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

时间:2018-07-17 03:07:23

标签: eclipse maven m2eclipse

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

我注意到有问题的罐子是灰色的,不像其余罐子那样不透明。

奇怪的是,如果您在导入中悬停了类名,它会显示该类的简短说明(来自jar中的文档!),但不会让我导入它。所有其他的Maven依赖项都可以导入。有任何想法吗?我们似乎甚至找不到深色图标的含义。

enter image description here enter image description here

此外,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>

我不确定这里缺少什么

6 个答案:

答案 0 :(得分:8)

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

答案 1 :(得分:5)

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

<scope>test</scope>

<scope>compile</scope>

答案 2 :(得分:1)

检查POM文件中的依赖项范围

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

test->编译会将依赖项从灰色更改为白色。

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

答案 3 :(得分:0)

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

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

答案 4 :(得分:0)

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

似乎新的Eclipse / Java版本确实具有新的Attribute:

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

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

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

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

答案 5 :(得分:0)

只需删除 Scope 即可。我尝试如下:

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>5.8.0-M1</version>
</dependency>