【问题标题】:Gradle: How to perform git pull through gradle?Gradle:如何通过 gradle 执行 git pull?
【发布时间】:2015-07-08 06:17:49
【问题描述】:

我想在编译开始之前从 git repo 中提取更改。 我找到了这个Gradle: how to clone a git repo in a task?,但它克隆了 repo 而不是只获取更改。 如果 git 服务器不在本地网络上或者 repo 很大,这可能会很耗时。

我找不到如何使用 gradle 或 gradle-git plugin 执行 git pull

【问题讨论】:

    标签: java git build gradle


    【解决方案1】:

    您可以创建Exec 任务并运行任何shell/cmd 命令。简单任务不需要额外的插件依赖。

    task gitPull(type: Exec) {
        description 'Pulls git.'
        commandLine "git", "pull"
    }
    

    用法:gradlew gitPull

    你应该会看到这样的:

    gradlew gitPull 
    Parallel execution is an incubating feature.
    :app:gitPull 
    Already up-to-date.
    
    BUILD SUCCESSFUL
    
    Total time: 9.232 secs
    

    其中Already up-to-date.git pull 命令的输出。

    【讨论】:

      【解决方案2】:

      以下 gradle 脚本应该会有所帮助:

      import org.ajoberstar.grgit.*
      
      buildscript {
         repositories { 
            mavenCentral() 
         }
         dependencies { 
            classpath 'org.ajoberstar:gradle-git:1.1.0' 
         }
      }
      
      task pull << {
         def grgit = Grgit.open(dir: project.file('.'))
         grgit.pull(rebase: false)
      }
      

      【讨论】:

      • 由于我对 gradle 的了解有限,我无法将您的 sn-p 集成到我的脚本中,因此我采用了 'Exec' 方式。此外,在看到 exec 示例后,我看不到使用外部库的意义。不过谢谢你的回答。
      • @Dojo 一个插件可以更高效(加载和运行一个插件比加载和运行另一个可执行文件更快,特别是在 Windows 上),它可以直接返回值(而不是必须对其进行编码,打印到标准输出,调用者必须捕获并解析输出)。与简单的用例无关,但随着需求的增长,用例会变得更加复杂。
      猜你喜欢
      • 2016-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-15
      • 1970-01-01
      • 1970-01-01
      • 2011-09-15
      • 2015-02-11
      相关资源
      最近更新 更多