最近,在Jenkins中运行的Maven构建作业失败,出现以下异常,表示它们无法从Maven Central提取依赖项,应该使用HTTPS。我不知道如何将请求从HTTP更改为HTTPS。有人能指导我一下这件事吗?

[ERROR] Unresolveable build extension: Plugin org.apache.maven.wagon:wagon-ssh:2.1 or one of its dependencies could not be resolved: Failed to collect dependencies for org.apache.maven.wagon:wagon-ssh:jar:2.1 (): Failed to read artifact descriptor for org.apache.maven.wagon:wagon-ssh:jar:2.1: Could not transfer artifact org.apache.maven.wagon:wagon-ssh:pom:2.1 from/to central (http://repo.maven.apache.org/maven2): Failed to transfer file: http://repo.maven.apache.org/maven2/org/apache/maven/wagon/wagon-ssh/2.1/wagon-ssh-2.1.pom. Return code is: 501, ReasonPhrase:HTTPS Required. -> [Help 2] Waiting for Jenkins to finish collecting data[ERROR] Plugin org.apache.maven.plugins:maven-clean-plugin:2.4.1 or one of its dependencies could not be resolved: Failed to read artifact descriptor for org.apache.maven.plugins:maven-clean-plugin:jar:2.4.1: Could not transfer artifact org.apache.maven.plugins:maven-clean-plugin:pom:2.4.1 from/to central (http://repo.maven.apache.org/maven2): Failed to transfer file: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-clean-plugin/2.4.1/maven-clean-plugin-2.4.1.pom. Return code is: 501 , ReasonPhrase:HTTPS Required. -> [Help 1]


当前回答

在pom.xml中添加以下存储库。

<project>
...
    <repositories>
        <repository>
            <id>central</id>
            <name>Maven Plugin Repository</name>
            <url>https://repo1.maven.org/maven2</url>
            <layout>default</layout>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
...
</project>

其他回答

最初来自https://stackoverflow.com/a/59796324/32453,虽然这可能有用:

注意,你的父pom也可以(重新)定义存储库,如果它因为某种原因覆盖了central并指定了http,你就需要修复它(修复的地方:~/.m2/settings.xml) 还有父母。

如果你不能在父pom中修复它,你可以覆盖父pom的repo,就像这样,在你的子pom中(从3.6.3默认的超级pom中提取,似乎他们也从repo1中更改了名称):

  <repositories>
    <repository>
      <id>central</id>
      <name>Central Repository</name>
      <url>https://repo.maven.apache.org/maven2</url> <!-- the https you've been looking for -->
      <layout>default</layout>
      <snapshots>
        <enabled>false</enabled> <!-- or set to true if desired, default is false -->
      </snapshots>
    </repository>
  </repositories>

我目前的环境不支持HTTPS,所以添加不安全版本的repo解决了我的问题:http://insecure.repo1.maven.org按照Sonatype

    <repositories>
       <repository>
          <id>Central Maven repository</id>
          <name>Central Maven repository insecure</name>
          <url>http://insecure.repo1.maven.org</url>
       </repository>
    </repositories>

我在最新版本(2020年8月)遇到了这个问题(在这台机器上很久没有使用Maven了),在阅读这些答案后,为什么它仍然是一个问题,我摸不着头脑。

原来我在我的主目录的.m2/文件夹里有一个旧的settings.xml,其中有一些多年前的自定义。

然而,即使删除了那个文件,我也无法解决这个问题。我最终删除了整个。m2文件夹。 我认为除了下载的资源,里面没有其他东西。也许仅仅删除repository/org/apache/maven/ prototype这样的文件夹就足够了。

分享这些,以防有人需要:

旧的Gradle配置(没有Gitlab, Docker部署,用于简单项目)

repositories {
google()
jcenter()

maven { url "http://dl.bintray.com/davideas/maven" }
maven { url 'https://plugins.gradle.org/m2/' }
maven { url 'http://repo1.maven.org/maven2' }
maven { url 'http://jcenter.bintray.com' }
}

新配置:

repositories {
google()
jcenter()

maven { url "https://dl.bintray.com/davideas/maven" }
maven { url 'https://plugins.gradle.org/m2/' }
maven { url 'https://repo1.maven.org/maven2' }
maven { url 'https://jcenter.bintray.com' }
}

注意https。快乐编码:)

我添加了下面的代码段设置。xml,它解决了这个问题,

<mirrors>
    <mirror>
        <id>maven-mirror</id>
        <name>Maven Mirror</name>
        <url>https://repo.maven.apache.org/maven2</url>
        <mirrorOf>central</mirrorOf>
    </mirror>
</mirrors>