name.remal.gradleplugins.toolkit.buildlogic.gradle-plugin-collect-api-dependencies.gradle Maven / Gradle / Ivy
allprojects {
pluginManager.withPlugin('java-gradle-plugin') {
project.ext.nonJavaApiDependencies = project.objects.setProperty(String)
Configuration gradlePluginApiDependenciesConf = configurations.create('gradlePluginApiDependencies') { Configuration conf ->
conf.extendsFrom(configurations.api)
conf.extendsFrom(configurations.indirectApi)
}
tasks.register('collectGradlePluginApiDependencies') { Task task ->
SetProperty nonJavaApiDependencies = project.nonJavaApiDependencies
inputs.property('nonJavaApiDependencies', nonJavaApiDependencies)
List inputConfs = [
gradlePluginApiDependenciesConf,
]
inputConfs.forEach { inputConf ->
task.inputs.files(inputConf)
.optional()
.withNormalizer(ClasspathNormalizer)
.withPathSensitivity(PathSensitivity.RELATIVE)
.withPropertyName(inputConf.name)
}
SetProperty platformDependencyNotations = project.objects.setProperty(String)
platformDependencyNotations.addAll(
project.provider {
Collection notation = new TreeSet()
inputConfs.forEach { Configuration inputConf ->
inputConf.allDependencies.forEach { Dependency dep ->
notation.add("${dep.group}:${dep.name}:${dep.version}")
}
}
return notation
}
)
task.inputs.property('platformDependencyNotations', platformDependencyNotations)
File outputFile = project.file("${project.buildDir}/gradle-plugin-api-dependencies.txt")
outputs.file(outputFile).withPropertyName('outputFile')
task.ext.outputFile = outputFile
task.doFirst { outputFile.delete() }
task.doLast {
outputFile.parentFile.mkdirs()
Collection dependencies = new TreeSet()
Collection resolvedArtifacts = gradlePluginApiDependenciesConf.resolvedConfiguration.resolvedArtifacts
Closure isExcludedCategory = { ResolvedDependencyResult resolvedDependency ->
return isPlatformDependency(resolvedDependency) || isDocumentationDependency(resolvedDependency) || isVerificationDependency(resolvedDependency)
}
Closure processResolvedDependency = { ResolvedDependencyResult resolvedDependency ->
ComponentIdentifier componentIdentifierUntyped = resolvedDependency.selected.id
if (!(componentIdentifierUntyped instanceof ModuleComponentIdentifier)) {
return
}
ModuleComponentIdentifier componentIdentifier = (ModuleComponentIdentifier) componentIdentifierUntyped
if (isPlatformDependency(resolvedDependency)) {
String notation = "${componentIdentifier.group}:${componentIdentifier.module}:${componentIdentifier.version}"
dependencies.add(notation)
return
}
Collection dependencyArtifacts = resolvedArtifacts.findAll { it.id.componentIdentifier == componentIdentifier }
dependencyArtifacts.forEach { dependencyArtifact ->
String notation = "${componentIdentifier.group}:${componentIdentifier.module}:${componentIdentifier.version}:${dependencyArtifact.classifier ?: ''}@${dependencyArtifact.extension}"
notation = notation.replaceFirst(/:?@(jar)?$/, '')
dependencies.add(notation)
}
}
gradlePluginApiDependenciesConf.incoming.resolutionResult
.root
.dependencies
.findAll { it instanceof ResolvedDependencyResult }
.collect { (ResolvedDependencyResult) it }
.findAll { !isExcludedCategory(it) }
.forEach { processResolvedDependency(it) }
gradlePluginApiDependenciesConf.incoming.resolutionResult
.allDependencies
.findAll { it instanceof ResolvedDependencyResult }
.collect { (ResolvedDependencyResult) it }
.findAll { !isExcludedCategory(it) }
.findAll { !it.selected.selectionReason.expected }
.forEach { processResolvedDependency(it) }
nonJavaApiDependencies.get().forEach { dependencies.add(it) }
outputFile.setText(dependencies.join('\n') + '\n', 'UTF-8')
}
}
}
}
tasks.create('collectAllGradlePluginApiDependencies') { Task task ->
Closure> getCollectGradlePluginApiDependenciesTasks = {
project.allprojects
.findAll { it.pluginManager.hasPlugin('java-gradle-plugin') }
.collect { it.tasks.getByName('collectGradlePluginApiDependencies') }
}.memoize()
task.dependsOn(project.provider { getCollectGradlePluginApiDependenciesTasks() })
Closure> getGradlePluginApiDependenciesFiles = {
getCollectGradlePluginApiDependenciesTasks().collect { it.outputFile }
}.memoize()
task.inputs.files(project.provider { getGradlePluginApiDependenciesFiles() })
.optional()
.withPathSensitivity(PathSensitivity.RELATIVE)
.withPropertyName('gradlePluginApiDependenciesFiles')
File outputFile = project.file("gradle-plugin-api-dependencies.txt")
task.outputs.file(outputFile).withPropertyName('outputFile')
task.ext.outputFile = outputFile
task.doFirst { outputFile.delete() }
task.doLast {
outputFile.parentFile.mkdirs()
Collection dependencies = new TreeSet()
getGradlePluginApiDependenciesFiles().forEach { file ->
file.getText('UTF-8').split(/\n/)
.collect { it.replaceFirst(/#.*/, '') }
.collect { it.trim() }
.findAll { !it.isEmpty() }
.forEach { dependencies.add(it) }
}
outputFile.setText(
[
"# This file is generated automatically by `${it.name}` Gradle task.",
'# Do not modify it yourself unless you know what you are doing.',
'',
dependencies.join('\n'),
'',
].join('\n'),
'UTF-8'
)
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy