如果你的Maven依赖一直无法下载并且你的仓库地址使用了HTTP,那么是因为Maven 默认情况下使用 HTTPS 来确保依赖项的安全下载,但在某些情况下(例如,内部仓库不支持 HTTPS),为了在 Maven 中允许不安全的 HTTP 下载。你需要启用 HTTP 下载。
方法一:修改 settings.xml 文件
你可以通过配置一个镜像来覆盖默认的阻止 HTTP 的镜像,从而允许不安全的 HTTP 下载。
在你的 Maven settings.xml 文件中添加以下配置:
<settings>
<mirrors>
<mirror>
<id>maven-default-http-blocker</id>
<mirrorOf>dummy</mirrorOf>
<name>Dummy mirror to override default blocking mirror that blocks http</name>
<url>http://0.0.0.0/</url>
<blocked>false</blocked>
</mirror>
</mirrors>
<profiles>
<profile>
<id>allow-http</id>
<repositories>
<repository>
<id>internal-repo</id>
<url>http://your.internal.repo/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>internal-repo-plugins</id>
<url>http://your.internal.repo/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>allow-http</activeProfile>
</activeProfiles>
</settings>
详细解释
-
mirrors:通过配置一个
dummy镜像来覆盖默认的阻止 HTTP 的镜像。id:maven-default-http-blocker是默认阻止 HTTP 的镜像 ID。mirrorOf:dummy表示这个镜像是一个占位符。url:http://0.0.0.0/是一个无效的 URL,用来占位。blocked:false表示不阻止这个镜像。
-
profiles:定义一个名为
allow-http的配置文件,包含允许 HTTP 的仓库配置。repositories:配置允许 HTTP 的仓库。pluginRepositories:配置允许 HTTP 的插件仓库。
-
activeProfiles:激活
allow-http配置文件。
验证配置
完成配置后,你可以通过以下命令验证配置是否生效:
mvn help:effective-settings
这将显示当前有效的 Maven 设置,确保你的 HTTP 配置已正确应用。
方法二:在 pom.xml 中配置仓库
你可以在项目的 pom.xml 文件中直接配置仓库,允许使用 HTTP 协议。
示例配置
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>my-project</artifactId>
<version>1.0.0</version>
<repositories>
<repository>
<id>internal-repo</id>
<url>http://your.internal.repo/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</project>
方法三:使用命令行参数激活配置文件
你可以通过命令行参数激活允许 HTTP 的配置文件,而不需要修改 settings.xml 或 pom.xml 文件。
示例命令
mvn clean install -Pallow-http
配置文件示例
在 settings.xml 或 pom.xml 中定义 allow-http 配置文件:
<profiles>
<profile>
<id>allow-http</id>
<repositories>
<repository>
<id>internal-repo</id>
<url>http://your.internal.repo/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>