代码之家  ›  专栏  ›  技术社区  ›  CustardBun

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

  •  8
  • CustardBun  · 技术社区  · 6 年前

    我正在帮助一个朋友首次使用M2Eclipse配置Maven项目。我们都非常不熟悉它,并且遇到了这样一个问题:即使依赖jar在项目目录的“maven dependencies”下显示了它中的包,如果我们试图从该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>
    

    我不知道我在这里遗漏了什么

    5 回复  |  直到 6 年前
        1
  •  8
  •   CustardBun    6 年前

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

        2
  •  5
  •   Vishnu M. D.    6 年前

    打开pom.xml文件 检查灰色JAR文件的名称 改变

    <scope>test</scope>
    

    <scope>compile</scope>
    
        3
  •  1
  •   Harish kumar NG    6 年前

    在pom文件中检查依赖范围

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

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

    如果您的依赖关系是针对测试范围的,那么该依赖关系在应用程序中不能正常使用,而编译范围则在项目的类路径中设置该依赖关系。

        4
  •  0
  •   Ashvini_Sharma    6 年前

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

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

        5
  •  0
  •   eckad158    6 年前

    当我使用 <scope>test</scope> 在马文波姆。

    似乎新的Eclipse/Java版本确实有一个新属性:

    <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

    启用后,我消除了所有编译器错误。