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