buildscript.configurations.classpath not available in apply from scripts


(Justin Ryan) #1

I’m trying to use a class available in the buildscript.configurations.classpath. This works fine in the main build.gradle, but when I move the code to a apply from: ‘file.gradle’ it is no longer available.

This works:

buildscript {
  repositories { mavenCentral() }
  dependencies { classpath 'org.ajoberstar:gradle-git:0.2.2' }
}
import org.ajoberstar.gradle.git.tasks.*
task add(type: GitAdd) {
}

This does not work:

buildscript {
  repositories { mavenCentral() }
  dependencies { classpath 'org.ajoberstar:gradle-git:0.2.2' }
}
  apply from: 'git.gradle'

git.gradle:

import org.ajoberstar.gradle.git.tasks.*
task add(type: GitAdd) {
}

Reading the Gradle User Guide makes very few references to apply from. If I print out buildscript.configurations.classpath in the git.gradle file, it shows an empty list of dependencies, while it’s looks perfectly find in the build.gradle.


(spencer_allain) #2

Applied scripts do not inherit anything from their applying script(s) to my knowledge, so the buildscript section of the build.gradle file is not seen by git.gradle.

If you copy the buildscript section from build.gradle into git.gradle, it should work fine. You probably will not even need that buildscript section to remain in build.gradle at all, depending upon whether there are other things you wish to pull directly from your gradle-git classes.

Maybe in the future, there will be some way to apply and have the ability to pre-inject buildscript object configuration details into a script being applied. The dependencies are usually unique, but often the repositories need to be set differently to work in all desired target environments.

-Spencer


(Luke Daley) #3

Spencer is right, script plugins are classloader isolated. Your script plugin has its own ‘buildscript’ block.

If you need to “export” types from the script plugin to the applier, you can add them as variables:

// git.gradle
buildscript { … }
ext.GitAdd = GitAdd
  // build.gradle
apply from: "git.gradle"
task add(type: GitAdd) {}

It’s hackish, but works. We are aware that there’s some awkwardness here and will address it at some point.