java maven 构建错误:包 org.apache.http 不存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38187349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 03:12:33  来源:igfitidea点击:

maven build error: package org.apache.http does not exist

javamavencompiler-errorshttpclient

提问by Andrew

I am attempting to send a simple HTTP post from a java program (for deploying on Heroku). I started with the demo project here. Using mvn packagebuilds the project successfully.

我正在尝试从 Java 程序发送一个简单的 HTTP 帖子(用于在 Heroku 上部署)。我从这里的演示项目开始。使用mvn package成功构建项目。

I then added my own additional file TestPost.java with a few lines of code, added it to the pom.xml, and still built fine.

然后我用几行代码添加了我自己的附加文件 TestPost.java,将它添加到pom.xml.

Then I tried to add the HTTP code from this example(minus the packageline), which uses the Apache HttpClient library.

然后我尝试添加这个示例中的 HTTP 代码(减去该package行),它使用 Apache HttpClient 库。

Using mvn packageresults in the following error:

使用mvn package导致以下错误:

package org.apache.http does not exist 

After searching for solutions I tried including a dependency in the pom.xml:

搜索解决方案后,我尝试在以下内容中包含依赖项pom.xml

<dependencies>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.2</version>
        <scope>compile</scope>
    </dependency>
</dependencies>

My understanding was that this should force a download of the necessary package but no download was shown on the next compile (just the same error), and the package is not visible in my user .m2\repository\ folder.

我的理解是,这应该强制下载必要的包,但在下一次编译时没有显示下载(只是相同的错误),并且该包在我的用户 .m2\repository\ 文件夹中不可见。

I tried inserting this dependency at different points in my pom.xmlwithout success.

我尝试在我的不同点插入此依赖项,pom.xml但没有成功。

Why is the apache library not being downloaded? Please note that I am new to maven.

为什么 apache 库没有被下载?请注意,我是 Maven 的新手。

回答by Arthur Noseda

Here is the pom.xml that you should have if indeed you need to depend on httpclient.

如果确实需要依赖 httpclient,这里是您应该拥有的 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>demo</groupId>
    <artifactId>httpclient-demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>httpclient-demo</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.2</version>
        </dependency>
    </dependencies>
</project>

Now if you put your Java sources in src/main/java, where src and pom.xml are in the same directory, Maven should resolve the dependency from your local repository, and download it, should it not already be there. Your local repository is defined in the conf/settings.xml in your Maven installation directory.

现在,如果您将 Java 源代码放在 src/main/java 中,其中 src 和 pom.xml 位于同一目录中,Maven 应该从您的本地存储库解析依赖项,并下载它(如果它尚未存在)。您的本地存储库在 Maven 安装目录的 conf/settings.xml 中定义。