gpt4 book ai didi

java - Gradle - 导入 GitHub 存储库并使其成为子项目依赖项?

转载 作者:搜寻专家 更新时间:2023-11-01 03:50:32 24 4
gpt4 key购买 nike

我试图通过部署 JAR 文件依赖项来避免多项目构建,但这确实破坏了我灵活的工作流程,我需要一种更快的方法来让我的 GIT 存储库动态地相互添加依赖项。我确信我可以使用 JAR 文件编写一些创造性的部署过程,但我喜欢对一个项目的更改立即波及到其他相关项目,而无需中间部署。

我查看了 grgit 文档,发现它允许分支克隆操作。但是,假设 repo 本身有自己的 gradle.build 脚本,我如何在克隆之后提取工作树并将其作为依赖项包含在内?

repositories {
mavenCentral()
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'

dependencies {
compile group: 'com.google.guava', name: 'guava', version: '18.0'
compile 'org.xerial:sqlite-jdbc:3.8.7'
compile 'net.jcip:jcip-annotations:1.0'
compile 'joda-time:joda-time:2.7'
compile files('path/to/my/repo') //not quite sure what to do here???
}

task downloadGitDependency {
def grgit = Grgit.clone(dir: 'path/to/my/repo', uri: 'git@github.com:ajoberstar/grgit.git')
}
build.dependsOn downloadGitDependency

sourceSets {
main.java.srcDir "src/main/java"
}

jar {
from configurations.compile.collect { entry -> zipTree(entry) }
}

最佳答案

如果子项目列表(相当)静态并且每个子项目都有自己的 build.gradle ,您可以将它们添加为 git 子模块。在您的项目目录中,执行:

git submodule add git@github.com:ajoberstar/grgit.git path/to/my/repo
cd path/to/my/repo
git fetch

# then either (to switch to a different branch):
git checkout -b mybranch origin/mybranch

# or (to check out a particular ref on the current branch):
git checkout deadc0de

# or both of the above (to check out a particular ref on a different branch)

(注意:您在示例中使用的 git URL 会将 grgit 添加为子项目——除非这是您想要的,否则您将在此处为您的子项目使用远程存储库的 URL。path/tp/my/repo 是其中的任意路径您的项目树将克隆子项目。)

然后,将您的子项目添加到主项目的构建配置中:import它在 settings.gradle并将其定义为 build.gradle 中的依赖项.

Git 会将主模块的提交链接到子模块的特定提交。

然而,请注意,在 git 中使用子模块并非没有陷阱:不要提交子模块(始终提交上游),并确保运行 git submodule update每次你的 HEAD 发生变化时( pull 、 merge 、 checkout 等)。一篇关于 git 子模块陷阱的好文章位于:https://codingkilledthecat.wordpress.com/2012/04/28/why-your-company-shouldnt-use-git-submodules/

关于java - Gradle - 导入 GitHub 存储库并使其成为子项目依赖项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29871484/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com