【问题标题】:Unable to import classes from maven dependency of dynamic web project in eclipse无法从 Eclipse 中动态 Web 项目的 maven 依赖项导入类
【发布时间】:2019-09-09 13:46:17
【问题描述】:

我通过右键单击项目并选择“配置”->“转换为 Maven 项目”将动态 Web 项目转换为 Maven 项目。我在 pom.xml 中添加了下面的依赖 jar。

<dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
            <version>3.1.0</version>
            <scope>test</scope>
        </dependency>

然后,右键单击项目-> 运行方式-> maven 安装。安装成功。

现在所有的 jars 和类都在 Maven Dependencies 部分下可用。 下面是我的项目结构的截图。

Project structure

下面是maven依赖部分的截图。

Maven dependencies section

但是,我无法将依赖 jar 中的任何包或类导入 src 文件夹中的任何 Java 类(例如:HttpServices.java)。尽管下载了这些 jar,但我无法使用它们。

我尝试更新 Maven 项目(强制清除快照)、清理项目、构建项目但没有任何效果。我在项目中没有错误。此外,当我将罐子直接添加到构建路径中时(使用添加外部罐子,即没有 maven)它工作得非常好。请帮助解决问题。

注意:其他 Java-maven 项目运行良好。问题仅在于这个转换后的 Maven 项目。 我正在为 Web 开发人员使用 Eclipse Java EE IDE。版本:Photon Release (4.8.0) 和 jdk 8

下面是生成的 pom.xml。当我将项目转换为 maven 时,会自动生成编译器和 war 插件。我只添加了依赖项。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>executeAutomation</groupId>
  <artifactId>executeAutomation</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.7.0</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.2.1</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
            <version>3.1.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

下面是maven的类路径入口。

<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
        <attributes>
            <attribute name="maven.pomderived" value="true"/>
            <attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
        </attributes>
    </classpathentry>

【问题讨论】:

  • 因为&lt;scope&gt;test&lt;/scope&gt; 这个依赖的所有东西都只在测试源中可用,在主源中不可用。删除 &lt;scope&gt;test&lt;/scope&gt; 并右键单击项目并 Maven > Update Project... 是否可以解决您的问题?

标签: java eclipse maven jar web-project


【解决方案1】:

由于 &lt;scope&gt;test&lt;/scope&gt;,此依赖项的所有内容仅在测试源中可用,而不在主源中。

删除&lt;scope&gt;test&lt;/scope&gt;,右键单击项目并选择Maven > 更新项目...

更多关于 Eclipse 中测试源的类路径分离:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-03
    • 2015-08-11
    • 2012-03-31
    • 1970-01-01
    • 2021-09-05
    • 1970-01-01
    • 2014-12-27
    相关资源
    最近更新 更多