All configurtation cache related documentation mostly target tasks, but what if plugin does not have any tasks (or only configures other tasks).
Example situation: plugin fixes dependency scopes in maven publication (for maven-publish plugin).
Custom extension to be able to switch off optimization:
public class MyExtension {
boolean fixDependencies
}
And the plugin (simplified for demonstration):
public class MyPlugin implements Plugin<Project> {
@Override
void apply(Project project) {
// do anything only if maven-publish plugin registered
project.plugins.withType(MavenPublishPlugin) {
MyExtension extension = project.extensions.create('my', MyExtension)
// act when extension values configured
project.afterEvaluate {
PublishingExtension publishing = project.extensions.getByType(PublishingExtension)
// for all registered publications
publishing.publications.withType(MavenPublication) { MavenPublication pub ->
// apply required xml changes (and where all configuration cache problems appear)
pub.withXml { XmlProvider xml ->
// not the best example, but I need to understand how to "bypass" such values properly
if (!extension.fixDependencies) {
return
}
// configuration access
Configuration impl = project.configurations.implementation
// pseudo groovy code: changing scope for all implementation dependencies in xml
DependencySet deps = impl.dependencies
xml.asNode().dependencies[0].each { Node dep ->
// for each xml dependency, search it in implemenrtation configuration (simplified)
boolean isImplDep = deps.find { Dependency implDep -> implDep.group == dep.groupId.text()}
.....
}
}
}
}
}
}
}
In order to be compatible with configuration cache I can’t use neither extension object nor resolve configurations inside pub.withXml
block. But how to achieve it properly?
For extensions, its recommended to use task properties - but I don’t have tasks here. Could I use a shared build service to store extension values there? If yes, please descibe how it would work a bit (what would be cached)
For configuration, if I understand correctly, I can pre-process required configuration and pass inside pom.withXml
only a simple (serializable) List<MySimpleDepObject>
. But this way, I will always trigger dependencies resolution (maybe even too early). Maybe there is a way to avoid it. Also, this “pre processing” will not invalidate cache when dependencies change (because cache would contain already post processed collection). Please tell me if I’m wrong
Any other advices of how such logic could be rewritten in configuration cache friendly way are also very welcome.
Thank you in advance.