首页 > 解决方案 > 如何在 gradle 项目中包含 github 存储库

问题描述

我正在尝试使用 gradle 创建一个库,并将这个库包含在另一个使用 gradle 和 github 链接的项目中。

我在 settings.gradle 中尝试这个:

sourceControl {
    gitRepository(URI.create("https://github.com/fonnes/JsonKay.git")) {
        producesModule("JsonKay:JsonKay")
    }
}

这在 build.gradle 中:

implementation('JsonKay:JsonKay:1.0') {
    version {
        branch = 'main'
    }
}

在尝试构建 gradle 时,我得到:Could not find any version that matches fonnes.JsonKay:JsonKay:{require 1.0; branch master}.

是我在本地项目或这个库中做错了什么吗?https://github.com/fonnes/JsonKay.git

编辑:我已将其从主分支更改为主分支。目前我无法在库中导入 Kotlin 文件。我确实在 intellij 的项目结构中看到了该库,但在依赖项列表中没有看到。

标签: kotlingithubgradle

解决方案


首先,您没有分支master,但是main. 所以,这已经可以了:

implementation('fonnes.JsonKay:JsonKay:1.0') {
    version {
        branch = 'main'
    }
}

但是,您应该定义版本 ( 1.0) 或 a branch,而不是同时定义两者。该版本1.0将被解析为一个 Git 标签,并且定义branch将指示 Gradle 签出相应的分支。

更好的:

implementation('fonnes.JsonKay:JsonKay') {
    version {
        branch = 'main'
    }
}

1.0版本:

// settings.gradle.kts
sourceControl {
    gitRepository(URI.create("https://github.com/fonnes/JsonKay.git")) {
        producesModule("JsonKay:JsonKay")
    }
}

// build.gradle.kts
implementation('JsonKay:JsonKay:1.0')

推荐阅读