为什么导入的Gradle脚本不能导入工作?

时间:2016-04-03 12:42:11

标签: gradle build.gradle

除非将Grgit的内容复制到git.gradle,否则

Gradle(OSX上的v2.12)无法在以下示例中找到build.gradle的定义。如何在不丢失git.gradle的情况下修复此错误?

的build.gradle

buildscript {
  repositories {
    jcenter()
  }
  dependencies {
    classpath 'org.ajoberstar:gradle-git:1.4.2'
  }
}

apply from: 'git.gradle'

git.gradle

import org.ajoberstar.grgit.* 

task clone << {
  Grgit.clone(dir: file('build/repo'), uri: 'git@github.com:user/repo.git')
}

这是输出:

$ gradle clone
...
Execution failed for task ':clone'.
> Could not find property 'Grgit' on task ':clone'.

1 个答案:

答案 0 :(得分:2)

找到解决方法:将Grgit添加为ExtraPropertiesExtension project.ext,以将其公开给导入的Gradle脚本。 (不确定是否有更简洁的方法......)

<强>的build.gradle

buildscript {
  repositories {
    jcenter()
  }
  dependencies {
    classpath 'org.ajoberstar:gradle-git:1.4.2'
  }
}

// expose Grgit to `git.gradle`
project.ext.Grgit = org.ajoberstar.grgit.Grgit

apply from: 'git.gradle'

<强> git.gradle

// no import needed here

task clone << {
  Grgit.clone(dir: file('build/repo'), uri: 'git@github.com:user/repo.git')
}

旁白:自动应用gradle-git插件 opens the Git directory of the project's root, and assigns the result to project.ext.grgit。如果您只需要使用当前项目的Git仓库,则无需公开Grgit {/ 1}}和 git.gradle 可能看起来像这样:

build.gradle