ERROR: Unknown host ‘repo.maven.apache.org: nodename nor servname provided, or not known‘(2020/08)

本文介绍了一种常见的Gradle构建错误,即未知主机错误,并提供了详细的解决方案,通过在build.gradle文件中添加阿里云的Maven镜像源,解决了因默认仓库不可达导致的构建失败问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

今天想运行一下之前做的项目,结果报错:

ERROR: Unknown host 'repo.maven.apache.org: nodename nor servname provided, or not known'. You may need to adjust the proxy settings in Gradle.

Enable Gradle 'offline mode' and sync project
Learn about configuring HTTP proxies in Gradle

 

解决方式

build.gradle(Project: xxx),加上阿里镜像

 

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
//        jcenter()
        google()
        mavenCentral()
        jcenter{url "http://jcenter.bintray.com/"}


    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.3'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
//        jcenter()
        maven { url "https://jitpack.io" }

        google()
        mavenCentral()
        jcenter{url "http://jcenter.bintray.com/"}
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

 

 

 

 

 

 

 

 

 

 

 

D:\apachemaven3.9.9> mvn mvnarchetype:generate [INFO] Scanning for projects... Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-clean-plugin/3.2.0/maven-clean-plugin-3.2.0.pom Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-clean-plugin/3.2.0/maven-clean-plugin-3.2.0.pom (5.3 kB at 5.3 kB/s) Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/35/maven-plugins-35.pom Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/35/maven-plugins-35.pom (9.9 kB at 34 kB/s) Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/maven-parent/35/maven-parent-35.pom Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/maven-parent/35/maven-parent-35.pom (45 kB at 142 kB/s) Downloading from central: https://repo.maven.apache.org/maven2/org/apache/apache/25/apache-25.pom Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/apache/25/apache-25.pom (21 kB at 115 kB/s) Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-clean-plugin/3.2.0/maven-clean-plugin-3.2.0.jar Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-clean-plugin/3.2.0/maven-clean-plugin-3.2.0.jar (36 kB at 76 kB/s) Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-install-plugin/3.1.2/maven-install-plugin-3.1.2.pom Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-install-plugin/3.1.2/maven-install-plugin-3.1.2.pom (8.5 kB at 48 kB/s) Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/42/maven-plugins-42.pom Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/42/maven-plugins-42.pom (7.7 kB at 45 k
最新发布
03-12
<think>好的,用户在执行Maven的`mvn archetype:generate`命令时遇到了问题,特别是依赖下载方面的。我需要先理解用户提供的引用内容,看看有什么线索。 首先看引用[1],用户原本使用的命令参数可能有问题,比如指定了`-DarchetypeVersion=5.0.0`,但不知道是否正确。引用[2]提到用户尝试使用3.0.1版本的archetype插件,但报错,可能和仓库地址或版本兼容性有关。 常见的问题可能包括网络连接问题、Maven配置错误、archetype版本不可用或插件版本不兼容。比如,Maven默认的中央仓库可能无法访问,或者archetype的坐标不正确,导致下载失败。另外,使用旧版本的插件可能不支持某些参数。 解决方法可能包括检查网络连接,确认Mavensettings.xml配置是否正确,尤其是镜像仓库的设置。如果使用的是公司内网,可能需要配置代理。另外,尝试更换archetype的版本,或者明确指定插件版本,例如使用`mvn archetype:generate -DarchetypeCatalog=internal`来强制使用本地缓存,避免远程仓库的问题。如果问题依然存在,手动下载archetype的jar包并安装到本地仓库也是一种可能的解决方案。 还需要注意,用户可能在命令中使用的仓库地址是否正确,比如引用[2]中的`https://repository.apache.org/content/repositories/snapshots/`是否是有效的仓库,是否包含所需的archetype。有时候快照仓库可能需要特定的配置才能访问。 另外,Maven的archetype插件版本过旧可能导致与新的Maven版本不兼容。例如,使用较新的Maven 3.x版本时,可能需要更新archetype插件到兼容的版本。用户提到的3.0.1版本可能已经过时,可以尝试升级到更高版本,或者使用默认的插件版本。 总结可能的解决步骤:1. 检查网络和代理设置;2. 确认archetype坐标和版本正确;3. 使用正确的仓库地址或镜像;4. 指定插件版本;5. 手动安装archetype。同时,生成相关问题需要考虑用户可能遇到的后续问题,比如如何调试Maven构建、如何配置镜像等。</think>关于使用`mvn archetype:generate`创建项目时依赖下载问题的解决方法: ### 一、常见错误原因分析 1. **网络连接问题** Maven默认从中央仓库下载依赖,若网络不通或仓库地址被拦截会导致下载失败。建议先用`mvn -U clean install`强制更新依赖测试网络连通性[^2]。 2. **archetype坐标错误** 检查`-DarchetypeGroupId`和`-DarchetypeArtifactId`参数是否与官方文档一致。例如COLA框架的archetype坐标应为: ```bash -DarchetypeGroupId=com.alibaba.cola \ -DarchetypeArtifactId=cola-framework-archetype-web ``` 3. **仓库配置缺失** 特殊archetype可能不在Maven中央仓库,需在`settings.xml`中添加镜像源: ```xml <mirror> <id>aliyunmaven</id> <mirrorOf>*</mirrorOf> <name>阿里云公共仓库</name> <url>https://maven.aliyun.com/repository/public</url> </mirror> ``` ### 二、解决方案步骤 1. **强制指定仓库目录** 通过`-DarchetypeCatalog=internal`优先使用本地缓存: ```bash mvn archetype:generate -DarchetypeCatalog=internal ``` 2. **明确插件版本** 指定新版archetype插件(推荐3.2.0+): ```bash mvn org.apache.maven.plugins:maven-archetype-plugin:3.2.0:generate ``` 3. **手动安装archetype(终极方案)** 若仍无法下载,可手动下载archetype的JAR包后执行: ```bash mvn install:install-file \ -Dfile=cola-framework-archetype-web-5.0.0.jar \ -DgroupId=com.alibaba.cola \ -DartifactId=cola-framework-archetype-web \ -Dversion=5.0.0 \ -Dpackaging=jar ``` ### 三、验证命令示例 ```bash mvn archetype:generate \ -DarchetypeGroupId=com.alibaba.cola \ -DarchetypeArtifactId=cola-framework-archetype-web \ -DarchetypeVersion=5.0.0 \ -DgroupId=com.example \ -DartifactId=myapp \ -Dversion=1.0.0-SNAPSHOT ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

被开发耽误的大厨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值