|
| 1 | +package plugins |
| 2 | + |
| 3 | +import org.gradle.api.DefaultTask |
| 4 | +import org.gradle.api.Plugin |
| 5 | +import org.gradle.api.Project |
| 6 | +import org.gradle.api.artifacts.component.ComponentArtifactIdentifier |
| 7 | +import org.gradle.api.artifacts.result.ResolvedArtifactResult |
| 8 | +import org.gradle.api.artifacts.result.ResolvedVariantResult |
| 9 | +import org.gradle.api.file.RegularFile |
| 10 | +import org.gradle.api.file.RegularFileProperty |
| 11 | +import org.gradle.api.provider.ListProperty |
| 12 | +import org.gradle.api.tasks.* |
| 13 | +import org.gradle.kotlin.dsl.* |
| 14 | + |
| 15 | +/** |
| 16 | + * The plugin to resolve runtime dependencies and generate reports. |
| 17 | + * |
| 18 | + * [Accessing the resolution result |
| 19 | + * programmatically](https://docs.gradle.org/current/userguide/dependency_resolution.html#sec:programmatic_api) |
| 20 | + */ |
| 21 | +class DepReportsPlugin : Plugin<Project> { |
| 22 | + override fun apply(target: Project) = |
| 23 | + with(target) { |
| 24 | + pluginManager.withPlugin("java-base") { |
| 25 | + val listResolvedArtifacts by |
| 26 | + tasks.registering(ListResolvedArtifacts::class) { |
| 27 | + // Get the runtime resolved artifacts |
| 28 | + val runtimeClasspath by target.configurations |
| 29 | + val resolvedArtifacts = runtimeClasspath.incoming.artifacts.resolvedArtifacts |
| 30 | + |
| 31 | + // Transform the artifacts |
| 32 | + artifactIds.set(resolvedArtifacts.map { it.map(ResolvedArtifactResult::getId) }) |
| 33 | + artifactVariants.set( |
| 34 | + resolvedArtifacts.map { it.map(ResolvedArtifactResult::getVariant) }) |
| 35 | + artifactFiles.set( |
| 36 | + resolvedArtifacts.map { |
| 37 | + it.map { resolvedArtifactResult -> |
| 38 | + layout.projectDirectory.file(resolvedArtifactResult.file.absolutePath) |
| 39 | + } |
| 40 | + }) |
| 41 | + outputFile.convention(layout.buildDirectory.file("resolved-artifacts.txt")) |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +@CacheableTask |
| 48 | +abstract class ListResolvedArtifacts : DefaultTask() { |
| 49 | + |
| 50 | + @get:Input abstract val artifactIds: ListProperty<ComponentArtifactIdentifier> |
| 51 | + |
| 52 | + @get:Input abstract val artifactVariants: ListProperty<ResolvedVariantResult> |
| 53 | + |
| 54 | + @get:InputFiles |
| 55 | + @get:PathSensitive(PathSensitivity.RELATIVE) |
| 56 | + abstract val artifactFiles: ListProperty<RegularFile> |
| 57 | + |
| 58 | + @get:OutputFile abstract val outputFile: RegularFileProperty |
| 59 | + |
| 60 | + @TaskAction |
| 61 | + fun execute() { |
| 62 | + val ids = artifactIds.get() |
| 63 | + val variants = artifactVariants.get() |
| 64 | + val files = artifactFiles.get() |
| 65 | + val outFile = outputFile.asFile.get() |
| 66 | + |
| 67 | + outFile.bufferedWriter().use { |
| 68 | + ids.forEachIndexed { idx, id -> |
| 69 | + val variant = variants[idx] |
| 70 | + val file = files[idx] |
| 71 | + it.appendLine("FILE ${file.asFile.name}") |
| 72 | + it.appendLine(" id: ${id.displayName}") |
| 73 | + it.appendLine(" variant: ${variant.displayName}") |
| 74 | + it.appendLine(" size: ${file.asFile.length()}") |
| 75 | + it.appendLine() |
| 76 | + } |
| 77 | + } |
| 78 | + outFile.forEachLine { println(it) } |
| 79 | + } |
| 80 | +} |
0 commit comments