|
| 1 | +package de.jjohannes.gradle.moduledependencies.tasks; |
| 2 | + |
| 3 | +import de.jjohannes.gradle.moduledependencies.JavaModuleDependenciesExtension; |
| 4 | +import org.gradle.api.DefaultTask; |
| 5 | +import org.gradle.api.Project; |
| 6 | +import org.gradle.api.artifacts.Configuration; |
| 7 | +import org.gradle.api.artifacts.ConfigurationContainer; |
| 8 | +import org.gradle.api.artifacts.component.ComponentIdentifier; |
| 9 | +import org.gradle.api.artifacts.component.ModuleComponentIdentifier; |
| 10 | +import org.gradle.api.artifacts.component.ProjectComponentIdentifier; |
| 11 | +import org.gradle.api.artifacts.result.ResolvedArtifactResult; |
| 12 | +import org.gradle.api.tasks.SourceSet; |
| 13 | +import org.gradle.api.tasks.SourceSetContainer; |
| 14 | +import org.gradle.api.tasks.TaskAction; |
| 15 | +import org.gradle.internal.jvm.JavaModuleDetector; |
| 16 | + |
| 17 | +import javax.inject.Inject; |
| 18 | +import java.util.Set; |
| 19 | +import java.util.TreeSet; |
| 20 | + |
| 21 | +public abstract class AnalyzeModulePathReportTask extends DefaultTask { |
| 22 | + |
| 23 | + private final ConfigurationContainer configurations; |
| 24 | + private final SourceSetContainer sourceSets; |
| 25 | + private final JavaModuleDependenciesExtension javaModuleDependencies; |
| 26 | + |
| 27 | + @Inject |
| 28 | + public AnalyzeModulePathReportTask(Project project) { |
| 29 | + this.configurations = project.getConfigurations(); |
| 30 | + this.sourceSets = project.getExtensions().getByType(SourceSetContainer.class); |
| 31 | + this.javaModuleDependencies = project.getExtensions().getByType(JavaModuleDependenciesExtension.class); |
| 32 | + } |
| 33 | + |
| 34 | + @Inject |
| 35 | + protected abstract JavaModuleDetector getJavaModuleDetector(); |
| 36 | + |
| 37 | + @TaskAction |
| 38 | + public void report() { |
| 39 | + Set<String> usedMappings = new TreeSet<>(); |
| 40 | + Set<String> nonModules = new TreeSet<>(); |
| 41 | + Set<String> missingMappings = new TreeSet<>(); |
| 42 | + Set<String> wrongMappings = new TreeSet<>(); |
| 43 | + |
| 44 | + for (SourceSet sourceSet : sourceSets) { |
| 45 | + collect(configurations.getByName(sourceSet.getCompileClasspathConfigurationName()), usedMappings, nonModules, missingMappings, wrongMappings); |
| 46 | + collect(configurations.getByName(sourceSet.getRuntimeClasspathConfigurationName()), usedMappings, nonModules, missingMappings, wrongMappings); |
| 47 | + } |
| 48 | + |
| 49 | + p(""); |
| 50 | + p("All Java Modules required by this project"); |
| 51 | + p("========================================="); |
| 52 | + for (String entry : usedMappings) { |
| 53 | + p(entry); |
| 54 | + } |
| 55 | + |
| 56 | + if (!nonModules.isEmpty()) { |
| 57 | + p(""); |
| 58 | + p("Components that are NOT Java Modules"); |
| 59 | + p("===================================="); |
| 60 | + for (String entry : nonModules) { |
| 61 | + p(entry); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + if (!missingMappings.isEmpty()) { |
| 66 | + p(""); |
| 67 | + p("[WARN] Java Modules without Name Mapping"); |
| 68 | + p("========================================"); |
| 69 | + for (String entry : missingMappings) { |
| 70 | + p(entry); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + if (!wrongMappings.isEmpty()) { |
| 75 | + p(""); |
| 76 | + p("[WARN] Wrong Mappings: Components are not Modules"); |
| 77 | + p("================================================="); |
| 78 | + for (String entry : wrongMappings) { |
| 79 | + p(entry); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private void collect(Configuration configuration, Set<String> usedMappings, Set<String> nonModules, Set<String> missingMappings, Set<String> wrongMappings) { |
| 85 | + for (ResolvedArtifactResult result : configuration.getIncoming().getArtifacts()) { |
| 86 | + ComponentIdentifier id = result.getId().getComponentIdentifier(); |
| 87 | + |
| 88 | + String moduleName; |
| 89 | + String version; |
| 90 | + String ga; |
| 91 | + |
| 92 | + if (id instanceof ProjectComponentIdentifier) { |
| 93 | + String projectName = ((ProjectComponentIdentifier) id).getProjectName(); |
| 94 | + ga = id.getDisplayName(); |
| 95 | + version = ""; |
| 96 | + moduleName = javaModuleDependencies.getOwnModuleNamesPrefix().get() + "." + projectName; |
| 97 | + } else if (id instanceof ModuleComponentIdentifier){ |
| 98 | + ModuleComponentIdentifier moduleVersion = (ModuleComponentIdentifier) id; |
| 99 | + ga = moduleVersion.getGroup() + ":" + moduleVersion.getModule(); |
| 100 | + version = " (" + moduleVersion.getVersion() + ")"; |
| 101 | + moduleName = javaModuleDependencies.moduleName(ga); |
| 102 | + } else { |
| 103 | + ga = ""; |
| 104 | + version = ""; |
| 105 | + moduleName = null; |
| 106 | + } |
| 107 | + |
| 108 | + boolean isModuleForReal = getJavaModuleDetector().isModule(true, result.getFile()); |
| 109 | + |
| 110 | + if (moduleName != null && isModuleForReal) { |
| 111 | + usedMappings.add(moduleName + " -> " + ga + version); |
| 112 | + } |
| 113 | + if (moduleName == null && !isModuleForReal) { |
| 114 | + nonModules.add(ga + version); |
| 115 | + } |
| 116 | + if (moduleName == null && isModuleForReal) { |
| 117 | + missingMappings.add("??? -> " + ga + version); |
| 118 | + } |
| 119 | + if (moduleName != null && !isModuleForReal) { |
| 120 | + wrongMappings.add(moduleName + " -> " + ga + version); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + private void p(String toPrint) { |
| 126 | + System.out.println(toPrint); |
| 127 | + } |
| 128 | + |
| 129 | +} |
0 commit comments