git 应用来自不同存储库的 gradle 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24709704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Apply gradle file from different repository
提问by Sundeep Gupta
We have multiple git repositories for different projects. There is also a git repository for infrastructure purpose. We have custom gradle plugins written in this infrastructure repository which we use in other repositories
我们有多个用于不同项目的 git 存储库。还有一个用于基础设施目的的 git 存储库。我们在这个基础设施存储库中编写了自定义 gradle 插件,我们在其他存储库中使用了这些插件
Example:
例子:
buildscript {
apply from: 'foo/bar/devinfra-buildscript.gradle', to: buildscript
}
apply plugin: 'devinfra'
Here we are having the buildscript{} file, foo/bar/buildscript.gradle in every Git repository. I want to know if there is a way where we can apply the file directly from a infrastructure repository. So that any change is visible across other repositories directly.
这里我们在每个 Git 存储库中都有 buildscript{} 文件 foo/bar/buildscript.gradle。我想知道是否有办法直接从基础架构存储库应用文件。这样可以直接在其他存储库中看到任何更改。
回答by VonC
In that case, you could add a git subtree Merging(different to git subtree) to each of your repo, referring to the infra repo.
在这种情况下,您可以向每个存储库添加一个git subtree Merging(与 git subtree 不同),参考 infra 存储库。
git read-tree --prefix=<subdirectory_name>/ –u <shared_library_branch>
You can see a study doing that in "Managing Nested Libraries Using the GIT Subtree Merge Workflow".
您可以在“使用 GIT 子树合并工作流管理嵌套库”中看到一项研究。
In your case:
在你的情况下:
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"
To update the project repo with subtree changes:
要使用子树更改更新项目存储库:
git checkout infra
git pull
git checkout master
git merge --squash –s subtree –-no-commit infra
git commit -m "update infra"
To update the subtree repo with change from the subtree folder of the project repo:
要使用项目存储库的子树文件夹中的更改更新子树存储库:
git checkout infra
git merge --squash –s subtree --no-commit master
git commit -m "(infra subtree) nature of changes"
git push infrarepo infra
回答by user3159253
The answer depends on what exactly you're going to achieve.
答案取决于您究竟要实现什么目标。
- If you simply want to update your build file in one place and "automagically" receive the changes in all project repos, then you probably should take the file out of GIT control and, for example, simply use a link or something like that to an outer location.
- The second option is to move all the build stuff into a common directory, make that directory a separated shareable GIT repository and then plug-in that repository in all your projects repos, e.g. as a submoduleBut in this case you won't receive "automagical" updates because git strictly binds submodule content to a particular commit in the submodule and une need to run
git submodule update
and subsequentgit commit
to receive updated contents in your project repos. The variant of this method isgit subtree merge
proposed by @VonC - If your build machinery is complex enough you may want to consider creating a "gradle artifact" and hide the complexity there. Then you may plug in that artifact to your projects and rely not on a particular version of the artifact but rather on a range of versions
- 如果您只是想在一个地方更新您的构建文件并“自动”接收所有项目存储库中的更改,那么您可能应该将该文件从 GIT 控制中移除,例如,只需使用一个链接或类似的东西到外部位置。
- 第二种选择是将所有构建的东西移动到一个公共目录中,使该目录成为一个单独的可共享 GIT 存储库,然后将该存储库插入到所有项目存储库中,例如作为子模块,但在这种情况下,您将不会收到“ automagical" 更新,因为 git 将子模块内容严格绑定到子模块中的特定提交,并且需要运行
git submodule update
并随后git commit
在您的项目存储库中接收更新的内容。该方法的变体git subtree merge
由@VonC 提出 - 如果您的构建机器足够复杂,您可能需要考虑创建一个“gradle artifact”并将复杂性隐藏在那里。然后,您可以将该工件插入到您的项目中,并且不依赖于工件的特定版本,而是依赖于一系列版本
回答by Peter Niederwieser
Assuming the Git repository is accessible over HTTP(S), one option is to use apply from: "http://..."
. Note that script plugins accessed over HTTP aren't currently cached, so the build will fail if the script cannot be fetched.
假设可以通过 HTTP(S) 访问 Git 存储库,一种选择是使用apply from: "http://..."
. 请注意,通过 HTTP 访问的脚本插件当前未缓存,因此如果无法获取脚本,构建将失败。