Greetings,
I am developing gradle plugin which adds some dependencies and I’d like to pass versions through extension property, so I have the following extension:
public class CustomVersionExtension
{
final Property someDependencyVersion;
public CustomVersionExtension(ObjectFactory objects)
{
someDependencyVersion = objects.property(String.class);
}
public Property<String> getSomeDependencyVersion()
{
return someDependencyVersion;
}
}
Configuration in build.gradle of project which uses this plugin:
customVersion {
someDependencyVersion.set(‘3.+’)
}
And code of plugin:
@Override
public void apply(Project project)
{
CustomVersionExtension customVersionExtension = project.getExtensions().create("customVersion",
CustomVersionExtension.class);
project.getPlugins().withType(JavaPlugin.class, javaPlugin -> {
JavaPluginConvention javaConvention = project.getConvention().getPlugin(JavaPluginConvention.class);
SourceSetContainer sourceSets = javaConvention.getSourceSets();
SourceSet testSourceSet = sourceSets.named(SourceSet.TEST_SOURCE_SET_NAME).get();
SourceSet testApiSourceSet = sourceSets.register("testApi", sourceSet -> {
SourceSet mainSourceSet = sourceSets.named(SourceSet.MAIN_SOURCE_SET_NAME).get();
FileCollection mainAndTestOutput = mainSourceSet.getOutput().plus(testSourceSet.getOutput());
sourceSet.setCompileClasspath(sourceSet.getCompileClasspath().plus(mainAndTestOutput));
sourceSet.setRuntimeClasspath(sourceSet.getRuntimeClasspath().plus(mainAndTestOutput));
}).get();
// Cannot query the value of extension 'customVersion' property 'someDependencyVersion' because it has no value available
// String versionOutside = customVersionExtension.getSomeDependencyVersion().get();
ConfigurationContainer configurations = project.getConfigurations();
configurations.named(testApiSourceSet.getImplementationConfigurationName()).configure(configuration -> {
configuration.extendsFrom(
configurations.named(testSourceSet.getImplementationConfigurationName()).get());
configuration.getDependencies().add(
project.getDependencies()
.create("org.apache.commons:commons-lang3:" + "3.+"));
});
configurations.named(testApiSourceSet.getRuntimeOnlyConfigurationName()).configure(configuration ->
configuration
.extendsFrom(configurations.named(testSourceSet.getRuntimeOnlyConfigurationName()).get()));
project.getTasks().register("testApi", Test.class, apiTest -> {
apiTest.setGroup(JavaBasePlugin.VERIFICATION_GROUP);
apiTest.setTestClassesDirs(testApiSourceSet.getOutput().getClassesDirs());
apiTest.setClasspath(testApiSourceSet.getRuntimeClasspath());
apiTest.useJUnitPlatform();
String versionInside = customVersionExtension.getSomeDependencyVersion().get(); //3.+
System.out.println(versionInside);
});
});
}
versionOutside - ‘Cannot query the value of extension ‘customVersion’ property ‘someDependencyVersion’ because it has no value available’, but version inside - have correct value. Is it possible to get extension properties outside task closure?