Plugin task configuration section can't see values from extension object

(Timothy Rupe) #1

I’m new at gradle, and having a problem setting up a Copy task in my custom plugin. I’m using an extension object to configure the into location for my Copy task, and when I execute the script files are copied. However, the destination directory is named ‘null/modules/null’ instead of the value that was configured.

project.task('copySourceToBuild', type: Copy) {
 from "src"
 into "${project.jsConfig.buildWorkingDir}/modules/${project.jsConfig.libraryName}"

If I manually copy the exact same task into my build.gradle file, the value is correctly seen, and directory created. My plugin doesn’t seem to be able to see the value inside the configure section, but can inside of doLast{}.

(Tye Howard) #2

Are you applying your plugin before you applied your extension?

(Peter Niederwieser) #3

It’s an often discussed topic here and on Stack Overflow. Plugins have to defer reading any mutable build model property until at least the end of the configuration phase. There are various techniques for doing so. Best places to learn are earlier discussions and/or the Gradle codebase.

(Timothy Rupe) #4

Thanks Peter, I’ll look for discussions about that.

(Ramesh Pasham) #5

Hi Timothy, This link helpful to you

(Timothy Rupe) #6

I ended up changing it to this, and it is working great:

project.task('copySourceToBuild', type: Copy) {
 project.gradle.projectsEvaluated {
  from "src"
  into "${extension.buildWorkingDir}/modules/${extension.libraryName}"