简体   繁体   English

无法从Eclipse中的动态Web项目的Maven依赖项导入类

[英]Unable to import classes from maven dependency of dynamic web project in eclipse

I converted a dynamic web project into a maven project by right-clicking on the project and choosing Configure -> Convert to Maven Project. 通过右键单击项目,然后选择配置->转换为Maven项目,我将动态Web项目转换为Maven项目。 I added below dependency jar in pom.xml. 我在pom.xml中添加了依赖项jar。

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

Then, right clicked project -> Run as -> maven install. 然后,右键单击项目->运行方式-> maven安装。 The installation was successful. 安装成功。

Now all the jars and classes are available under the Maven Dependencies section. 现在,所有的jar和类都在Maven Dependencies部分下可用。 Below is screenshot of my project structure. 下面是我的项目结构的屏幕截图。

Project structure 项目结构

Below is the screenshot of maven dependency section. 下面是maven依赖项部分的屏幕截图。

Maven dependencies section Maven依赖项部分

However, I am unable to import any package or class form the dependency jars into any of my Java class (Ex: HttpServices.java) in src folder. 但是,我无法将任何形式的包或类从依赖项jar导入到src文件夹中的任何Java类(例如:HttpServices.java)中。 I am unable to use the jars although they are downloaded. 尽管下载了jar,但我无法使用它们。

I tried updating Maven project (Force clearing snapshots), clean project, build project but nothing worked. 我尝试更新Maven项目(强制清除快照),清理项目,构建项目,但没有任何效果。 I have no errors in the project. 我在项目中没有错误。 Also, when I added the jars directly into the build path (using add external jars ie without maven) it worked perfectly fine. 另外,当我将jars直接添加到构建路径中时(使用添加外部jars,即不使用maven),它可以很好地工作。 Please help in resolving the problem. 请帮助解决问题。

Note: Other Java-maven projects are working fine. 注意:其他Java-maven项目运行正常。 Problem is only with this converted maven project. 问题仅在于此转换的Maven项目。 I am using Eclipse Java EE IDE for Web Developers. 我正在为Web开发人员使用Eclipse Java EE IDE。 Version: Photon Release (4.8.0) and jdk 8 版本:Photon Release(4.8.0)和jdk 8

Below is the generated pom.xml. 下面是生成的pom.xml。 Compiler and war plugins were automatically generated when I converted the project to maven. 当我将项目转换为Maven时,会自动生成编译器和war插件。 I only added the dependency. 我只添加了依赖项。

<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>

Below is the class path entry for maven. 以下是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>

Because of <scope>test</scope> everything of this dependency is available only in test source , not in the main source. 由于<scope>test</scope> ,此依赖项的所有内容仅在测试源中可用 ,而在主源中不可用。

Remove <scope>test</scope> , right-click the project and choose Maven > Update Project... . 删除<scope>test</scope> ,右键单击该项目,然后选择Maven> Update Project...。

More about classpath separation for test sources in Eclipse: 有关Eclipse中测试源的类路径分离的更多信息:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM