【问题标题】:maven build succeeds, but eclipse build failsmaven构建成功,但eclipse构建失败
【发布时间】:2019-05-10 13:47:16
【问题描述】:

我有一个 Eclipse / Maven 项目:使用 Maven 构建成功,但 Eclipse 确实存在编译时错误并且无法构建。 我的 Eclipse 是 Neon (4.6.3),我使用的是内置的 Maven 和 JDK 1.8

你能帮我解决这个问题吗?

我的 pom.xml:

<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>seller</groupId>
  <artifactId>home.digest</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>home.digest Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.8</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpcore</artifactId>
        <version>4.4.11</version>
    </dependency>
    <dependency>
        <!-- jsoup HTML parser library @ https://jsoup.org/ -->
        <groupId>org.jsoup</groupId>
        <artifactId>jsoup</artifactId>
        <version>1.11.3</version>
    </dependency>
  </dependencies>

  <build>
    <finalName>home.digest</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

Eclipse 无法识别类中的以下导入:

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.protocol.HttpContext;

Eclipse 项目具有以下属性:

【问题讨论】:

  • 您没有告诉我们您看到了什么错误。没有它就很难提供帮助。
  • 正如我上面所说:Eclipse 确实存在编译时错误(不识别上面提到的三个导入,即 Eclipse 在导入部分中找不到类
  • 首先检查请尝试在命令行上构建...

标签: java eclipse maven apache-httpclient-4.x


【解决方案1】:

问题是,jar 文件已损坏:

[ERROR] 读取 C:\Users\User.m2\repository\org\apache\httpcomponents\httpcore\4.4.11\httpcore-4.4.11.jar 时出错;无效的 LOC 标头(错误的签名)

我通过替换依赖解决了这个问题

<dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpcore</artifactId>
        <version>4.4.11</version>
    </dependency>

依赖于早期版本:

4.4.10

【讨论】:

  • 删除文件C:\Users\User.m2\repository\org\apache\httpcomponents\httpcore\4.4.11\httpcore-4.4.11.jar,右键单击项目并选择Maven > Update Project... 应该也可以。据我所知,有一个错误已被修复,并且经常导致 Maven 下载损坏。 Please upgrade。你落后了五个版本。
  • 感谢您的建议。下次我会记住的。实际上,在这种情况下,非常奇怪的是 Maven 构建成功,尽管有错误消息。怎么可能?!
  • 我不知道为什么 Maven 构建会成功。如果您在命令行而不是在 Eclipse 内部运行 Maven,则可能使用了不同的本地 Maven 存储库。
【解决方案2】:

你好

首先,您应该检查在您的 maven 本地文件夹中包含 apache jar 文件。某些版本文件夹位于您的本地 Maven 文件夹中。但是没有jar文件。

此行代码添加到您的 pom.xml 文件中。

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.6</version>
</dependency>

【讨论】:

  • 您好,apache jar 文件已经在本地存储库中。而且,是的,我的 pom.xml 文件中确实有上述依赖项(见上文)
  • @AlexMi org.apache.httpcomponentshttpcore4.4.10 你用过吗是吗?