【问题标题】:Apply gradle file from different repository应用来自不同存储库的 gradle 文件
【发布时间】:2014-09-02 18:51:38
【问题描述】:

我们有多个用于不同项目的 git 存储库。还有一个用于基础设施目的的 git 存储库。我们在这个基础设施存储库中编写了自定义 gradle 插件,我们在其他存储库中使用这些插件

例子:

buildscript {
    apply from: 'foo/bar/devinfra-buildscript.gradle', to: buildscript
}
apply plugin: 'devinfra'

我们在每个 Git 存储库中都有 buildscript{} 文件 foo/bar/buildscript.gradle。我想知道是否有一种方法可以直接从基础设施存储库应用文件。这样任何更改都可以直接在其他存储库中看到。

【问题讨论】:

    标签: git gradle


    【解决方案1】:

    在这种情况下,您可以将 git subtree Merging(与 git 子树不同)添加到您的每个存储库,参考基础设施存储库。

    git read-tree --prefix=<subdirectory_name>/ –u <shared_library_branch>
    

    您可以在“Managing Nested Libraries Using the GIT Subtree Merge Workflow”中看到一项研究。

    在你的情况下:

    cd /path/to/project
    git remote add infrarepo /url/to/infra/repo
    git fetch infrarepo
    git checkout -b infra infrarepo/master
    
    git checkout master
    git read-tree --prefix=infra/ –u infra
    git commit -m "Add Infra subtree"
    

    使用子树更改更新项目 repo:

    git checkout infra
    git pull
    git checkout master
    git merge --squash –s subtree –-no-commit infra
    git commit -m "update infra"
    

    使用项目 repo 的子树文件夹中的更改来更新子树 repo:

    git checkout infra
    git merge --squash –s subtree --no-commit master
    git commit -m "(infra subtree) nature of changes"
    
    git push infrarepo infra
    

    【讨论】:

      【解决方案2】:

      答案取决于您要达到的具体目标。

      1. 如果您只是想在一个地方更新您的构建文件并“自动”接收所有项目存储库中的更改,那么您可能应该让文件脱离 GIT 控制,例如,只需使用链接或类似的东西到外部位置。
      2. 第二个选项是将所有构建内容移动到一个公共目录中,使该目录成为一个单独的可共享 GIT 存储库,然后将该存储库插入到所有项目存储库中,例如作为submodule 但在这种情况下,您不会收到“自动”更新,因为 git 将子模块内容严格绑定到子模块中的特定提交,并且不需要运行 git submodule update 和随后的 git commit 以接收更新的内容项目回购。该方法的变体是@VonC提出的git subtree merge
      3. 如果您的构建机器足够复杂,您可能需要考虑创建“gradle artifact”并隐藏其中的复杂性。然后,您可以将该工件插入您的项目,而不是依赖工件的特定版本,而是依赖一系列版本

      【讨论】:

      • 插件在工件(gradle artifact)中。但是要提取这些工件,每个消费者 build.gradle(在其他 git 存储库中)都必须添加 buildscript{} 块,以指定存储库 + 依赖项配置。我试图找到一个不需要这样做的解决方案,并且 artifactory 中的 gradle 插件在没有 buildscript 的情况下可用{}
      • 那么解决方案#2(也许是@VonC 的解决方案)可能最适合您的情况。脚本的更改可能很少见,因此用户不必每五分钟更新一次。
      【解决方案3】:

      假设 Git 存储库可通过 HTTP(S) 访问,一种选择是使用 apply from: "http://..."。请注意,通过 HTTP 访问的脚本插件当前未缓存,因此如果无法获取脚本,构建将失败。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-23
        相关资源
        最近更新 更多