首页 > 解决方案 > 在 Gradle 中为所有项目配置存储库

问题描述

我正在尝试为所有子项目配置存储库。

我有主要的build.gradle

buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
        google()
        jcenter()
        ...
    }
    dependencies {
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

plugins {
    id 'base'
}

allprojects {
    apply plugin: 'base'

    repositories {
        mavenLocal()
        mavenCentral()
        google()
        jcentre()
        ...
    }

    wrapper{
        gradleVersion = '6.5.1'
        distributionType = Wrapper.DistributionType.ALL
    }

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

在子项目中build.gradle,我只有:

...

dependencies {
    implementation ....
}

我正进入(状态:

Execution failed for task ':compileJava'.
> Could not resolve all files for configuration ':compileClasspath'.
   > Cannot resolve external dependency .... because no repositories are defined.
     Required by:
         project :

我想repositories在主文件中定义一次,因为这些在子项目中不会改变。


settings.gradle我的主要项目中:

rootProject.name = 'main-project-name'

include 'sub-project-name'

settings.gradle子项目中,我有:

rootProject.name = 'sub-project-name'

标签: gradle

解决方案


Gradle 中的多项目构建可能有多个build.gradle文件,但只有一个settings.gradle文件(通常在项目根目录中)。您的第二个settings.gradle文件定义了仅包含单个项目的第二个设置。您可以通过运行来检查这一点gradle projects。只需删除第二个settings.gradle文件即可解决您的问题。

通常你可以简单地通过命名相应的目录然后调用来定义你的子项目的名称include。目录的名称rootProject可能会在内部定义settings.gradle,因为目录的名称通常不会存储在 Git 等版本控制系统中。开发人员可能会将存储库检出到不同的目录,从而导致 Gradle 为根项目使用不同的名称。如果您希望子项目具有与其包含目录不同的名称,请使用include所需的名称,然后通过project(':foo').projectDir = file('path/to/foo').


推荐阅读