Should code loaded from the buildSrc directory and code imported using the buildscript closure have equivalent class loader structures?


(thoward) #1

Should code loaded from the buildSrc directory and code imported using the buildscript closure have equivalent class loader structures?

I’m asking because this does not appear to be the case when loading jars via the settings.gradle file. The following example code demonstrates the differing behaviors:

from settings.gradle:

import Pojo2;
  buildscript {
        dependencies {
                classpath files('Pojo.jar')
        }
}
  println "Setting Pojo to 'Settings test'"
Pojo.p = "Settings test"
println "Pojo's value:[" + Pojo.p + "]"
  println "Setting Pojo2 to 'Settings test2'"
Pojo2.p = "Settings test2"
println "Pojo2's value:[" + Pojo2.p + "]"

from build.gradle:

buildscript {
        dependencies {
                classpath files('Pojo.jar')
        }
}
  task stuff << {
        println "Pojo's value in build[" + Pojo.p + "]"
        println "Pojo2's value in build[" + Pojo2.p + "]"
}

Pojo and Pojo2 are just Objects with a static string called ‘p’. Pojo is loaded via a previously compiled jar while Pojo 2 is located in the buildSrc directory.

When the code is run the following output is observed:

gradle stuff :buildSrc:clean :buildSrc:compileJava :buildSrc:compileGroovy UP-TO-DATE :buildSrc:processResources UP-TO-DATE :buildSrc:classes :buildSrc:jar :buildSrc:assemble :buildSrc:compileTestJava UP-TO-DATE :buildSrc:compileTestGroovy UP-TO-DATE :buildSrc:processTestResources UP-TO-DATE :buildSrc:testClasses UP-TO-DATE :buildSrc:test :buildSrc:check :buildSrc:build Setting Pojo to ‘Settings test’ Pojo’s value:[Settings test] Setting Pojo2 to ‘Settings test2’ Pojo2’s value:[Settings test2] :stuff Pojo’s value in build[] Pojo2’s value in build[Settings test2]

BUILD SUCCESSFUL

Total time: 4.141 secs


(Luke Daley) #2

Hi Tye,

I’m not sure that they should. It makes sense to me to isolate settings.gradle and buildscripts in this way, at least for explicitly loading dependencies (i.e. Pojo.jar in your case).

I think you could make a case for having the same behaviour with the content from buildSrc as well. I’ll wait for the other devs to weigh in as well.

Is this behaviour preventing you from achieving something? A use case might help illuminate the desirable behaviour here.


(thoward) #3

Hi Luke,

We’re trying to segregate our build source from our production source. As such we’d like to put the compiled jar for buildSrc into a repository and pull it at the beginning of the build. The problem occurs when some of our static configuration objects are initialized in the settings.gradle file, but then the values are “cleared” once the build.gradle file loads. I’ve got some potential options to work around the problem, but the disparate behavior confused me.

Mostly I just wanted to know : Is the deviation intentional? If not, which pattern is proper? If so, what’s the intent behind the differences?

Thanks!