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

Kar*_*ell 6 java maven

最近,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>
Run Code Online (Sandbox Code Playgroud)

问题:

  • 这够了吗?或者还会在某处涉及到http吗?
  • 这是正确的做法吗?正如我读到的那样,我应该在settings.xml中执行此操作.但是其他人使用我的(开源)项目将不会使用安全连接.

更新

它看起来不够,例如组件插件仍然使用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
Run Code Online (Sandbox Code Playgroud)

Ell*_*hir 42

您不必将其一一放入所有 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>
Run Code Online (Sandbox Code Playgroud)

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


小智 14

您可以执行以下操作来强制 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>
Run Code Online (Sandbox Code Playgroud)

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

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


Kar*_*ell 10

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

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

  • 我没有更好的感觉移除本地缓存 `~/.m2/repository` 会带来什么。 (5认同)

Niz*_*mad 7

在您的 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>
Run Code Online (Sandbox Code Playgroud)

使用终端进行 Maven 更新

mvn -U clean install
Run Code Online (Sandbox Code Playgroud)