踩坑记录 | Gradle7.0+仓库配置踩坑

踩坑记录 | Gradle7.0+仓库配置踩坑:Build was configured to prefer settings repositories over project repositories but repository 'maven' was added by build file 'build.gradle'

踩坑

问题描述

新版Gradle从7.0版本开始,新增了 dependencyResolutionManagement 作为推荐的配置仓库方法。

Android Studio也不知道从哪个版本开始,创建新Project时开始使用这种方法作为默认配置仓库的方法,然而这个代码块是放在 settings.gradle 里面的。

这就导致了我想要配置镜像时翻遍了两个 build.gradle 找不到配置仓库的地方,自己加又一直报错:

Build was configured to prefer settings repositories over project repositories but repository 'maven' was added by build file 'build.gradle'

意思是已经加入过这个仓库了,然而提示的重复位置却不是默认的配置而是我们自己加入配置的位置,完全不知道重复在哪了,令人摸不着头脑。

解决方法

删除掉其他地方的 repositories,在 settings.gradle 下配置仓库。

顺便附上我自己修改后的镜像代码:

settings.gradle

pluginManagement {
    repositories {
        gradlePluginPortal()
        maven { url "https://maven.aliyun.com/repository/google" }
        maven { url "https://maven.aliyun.com/repository/public" }
    }
}
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        maven { url "https://maven.aliyun.com/repository/google" }
        maven { url "https://maven.aliyun.com/repository/public" }
        maven { url 'https://www.jitpack.io' }
    }
}
rootProject.name = "Sample"
include ':app'
tasks.named('processResources') {
    expand(project.properties)
}
app:
  name: "${name}"
  description: "${description}"

description="我的描述"
def releaseTime() {
    return new Date().format("yyyyMMddHHmm", TimeZone.getTimeZone("GMT+08:00"))
}

task info {
    println "Hello World"
    println "${new Date().format("yyyy", TimeZone.getTimeZone("GMT+08:00"))}"
    println(project.properties.name)
    println(project.properties.description)
}


test {
    enabled(false)
// 或者 enable = false
}


//for including in the copy task
def dataContent = copySpec {
//    from 'src/data'
//    include '*.data'
}

tasks.register('initConfig', Copy) {
    from('deploy') {
        include '**/*.bat'
        include '**/*.sh'
    }
    from('src/main/resources') {
        exclude '**/*.txt', '**/*.xml'
        include '**/*.yml', '**/*.properties'
    }
//    from('src/main/languages') {
//        rename 'EN_US_(.*)', '$1'
//    }
//    into 'build/target/config'
    into 'build/libs'
    exclude '**/*.bak'
    includeEmptyDirs = false

    with dataContent
}


//    tasks.withType(JavaCompile).tap {
//        configureEach {
//            options.encoding = "UTF-8"
//        }
//    }

//
//subprojects { it ->
//    processResources {
//        expand(it.properties)
//    }
//}

//task info {
//    println "Gradle — 中文版构建工具 система автоматической сборки, построенная на принципах Apache Ant и Apache Maven, но предоставляющая DSL на языке Groovy вместо традиционной XML-образной формы представления конфигурации проекта."
//}

设置build.gradle打包时自动加时间_gradle 获取系统时间戳-CSDN博客,

 

posted @ 2025-01-20 15:58  CharyGao  阅读(432)  评论(0)    收藏  举报