Java 强制 maven 为 maven central 使用 HTTPS 的正确方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25393298/
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-08-11 00:18:37  来源:igfitidea点击:

What is the correct way of forcing maven to use HTTPS for maven central?

javamaven

提问by Karussell

Recently sonatype enabled maven central to support https (background information). I've now added the following snippet to my pom.xml to force using https everywhere:

最近 sonatype 启用了 maven central 来支持 https(背景资料)。我现在已将以下代码段添加到我的 pom.xml 以强制在任何地方使用 https:

<!-- force https -->
<repositories>
    <repository>
        <id>central</id>
        <url>https://repo1.maven.org/maven2</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>central</id>
        <url>https://repo1.maven.org/maven2</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </pluginRepository>
</pluginRepositories>

Questions:

问题:

  • Is this sufficient? Or will there be still http involved somewhere?
  • Is this the correct way of doing it? As I've read that I should do this in the settings.xml instead. But then others using my (open source) project won't use the secure connection.
  • 这足够了吗?还是会在某处涉及http?
  • 这是正确的做法吗?正如我读到的那样,我应该在 settings.xml 中执行此操作。但是其他人使用我的(开源)项目将不会使用安全连接。

Update

更新

It does not look sufficient as for e.g. the assembly plugin still HTTP is used:

对于例如仍然使用 HTTP 的程序集插件,它看起来还不够:

[INFO] --- maven-assembly-plugin:2.4:single (make-assembly) @ graphhopper-web ---
Downloading: http://repo.maven.apache.org/maven2/org/slf4j/slf4j-jdk14/1.5.6/slf4j-jdk14-1.5.6.jar

采纳答案by Karussell

This is already fixed in latest maven 3.2.3! See the changelogs!

这已经在最新的 maven 3.2.3 中修复了!查看变更日志

So install maven 3.2.3 and do 'rm -rf ~/.m2/repository/*' for a better feeling ;)

所以安装 maven 3.2.3 并执行 'rm -rf ~/.m2/repository/*' 以获得更好的感觉;)

回答by Karussell

You can do the following to force maven use single repo:

您可以执行以下操作来强制 maven 使用单个 repo:

<settings>
  ...
  <mirrors>
    <mirror>
      <id>internal-repository</id>
      <name>Maven Repository Manager running on https://repo1.maven.org/maven2</name>
      <url>https://repo1.maven.org/maven2</url>
      <mirrorOf>*</mirrorOf>
    </mirror>
  </mirrors>
  ...
</settings>

You can find more info here.

您可以在此处找到更多信息。

And also you can use authentication to the repo if you like, the info is here.

如果你愿意,你也可以使用对 repo 的身份验证,信息在这里

回答by Ellrohir

You don't have to place it into all POMs one by one. I'd rather suggest to add the following code into MAVEN_HOME\conf\settings.xmlinto <profiles>section:

您不必将其一一放入所有 POM 中。我宁愿建议将以下代码添加到MAVEN_HOME \的conf \ settings.xml的进入<profiles>部分:

<profile>
    <id>maven-https</id>
    <activation>
        <activeByDefault>true</activeByDefault>
    </activation>
    <repositories>
        <repository>
            <id>central</id>
            <url>https://repo1.maven.org/maven2</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>central</id>
            <url>https://repo1.maven.org/maven2</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories> 
</profile>

This will be always an active setting unless you disbale/override it in your POM when needed.

这将始终是一个活动设置,除非您在需要时在 POM 中取消/覆盖它。

回答by Nizam Mahammad

Add below code in your pom.xml file and no need to remove local cache, It's works like a charm

在您的 pom.xml 文件中添加以下代码,无需删除本地缓存,它就像一个魅力

<distributionManagement>
       <repository>
          <id>Central Maven repository</id>
          <name>Central Maven repository https</name>
          <url>https://repo.maven.apache.org/maven2</url>
       </repository>
    </distributionManagement>

Maven update with terminal

使用终端进行 Maven 更新

mvn -U clean install

回答by Muhammad Faisal

for resolve this error you can add new Repository as https://repo.maven.apache.org/maven2/

要解决此错误,您可以将新的存储库添加为https://repo.maven.apache.org/maven2/

enter image description here

在此处输入图片说明

回答by padmender singh

I was also getting the same issue and tried all the possible ways by changing the proxies mapping but nothing works, finally i got the solution by adding the below code in setting.xml file in .m2 folder resolve the problem.

我也遇到了同样的问题,并通过更改代理映射尝试了所有可能的方法,但没有任何效果,最后我通过在 .m2 文件夹中的 setting.xml 文件中添加以下代码解决了问题。

Note: Working fine for me without enable the proxy in setting.xml.

注意:在没有在 setting.xml 中启用代理的情况下对我来说工作正常。

<settings>
<mirrors>
    <mirror>
        <id>internal-repository</id>
        <name>Maven Repository Manager running on https://repo1.maven.org/maven2</name>
        <url>https://repo1.maven.org/maven2</url>
        <mirrorOf>*</mirrorOf>
    </mirror>
</mirrors>