|
27 | 27 | import org.gradle.api.artifacts.dsl.DependencyHandler; |
28 | 28 | import org.gradle.api.artifacts.result.DependencyResult; |
29 | 29 | import org.gradle.api.artifacts.result.ResolvedArtifactResult; |
| 30 | +import org.gradle.api.artifacts.result.ResolvedComponentResult; |
30 | 31 | import org.gradle.api.artifacts.result.ResolvedDependencyResult; |
31 | 32 | import org.gradle.api.attributes.Attribute; |
32 | 33 | import org.gradle.api.attributes.Category; |
|
41 | 42 |
|
42 | 43 | import java.util.Collection; |
43 | 44 | import java.util.List; |
| 45 | +import java.util.Map; |
44 | 46 | import java.util.Set; |
45 | 47 | import java.util.stream.Collectors; |
| 48 | +import java.util.stream.Stream; |
46 | 49 |
|
47 | 50 | /** |
48 | 51 | * Entry point of the plugin. |
@@ -124,24 +127,39 @@ private void registerTransform(String fileExtension, Project project, ExtraJavaM |
124 | 127 | javaModulesMergeJars.getIncoming().artifactView(v -> v.lenient(true)).getArtifacts().getArtifacts()); |
125 | 128 | p.getMergeJarIds().set(artifacts.map(new IdExtractor())); |
126 | 129 | p.getMergeJars().set(artifacts.map(new FileExtractor(project.getLayout()))); |
| 130 | + |
127 | 131 | p.getCompileClasspathDependencies().set(project.provider(() -> |
128 | | - sourceSets.stream().flatMap(s -> configurations.getByName(s.getCompileClasspathConfigurationName()).getIncoming().getResolutionResult().getAllComponents().stream()).collect(Collectors.toMap( |
129 | | - c -> ga(c.getId()), |
130 | | - c -> c.getDependencies().stream().map(ExtraJavaModuleInfoPlugin::ga).collect(Collectors.toSet()), |
131 | | - (dependencies1, dependencies2) -> dependencies1 // There can be duplications which are assumed to be the same |
132 | | - )))); |
| 132 | + toStringMap(sourceSets.stream().flatMap(s -> filteredResolutionResult(configurations.getByName(s.getCompileClasspathConfigurationName()), componentsOfInterest(extension)))))); |
133 | 133 | p.getRuntimeClasspathDependencies().set(project.provider(() -> |
134 | | - sourceSets.stream().flatMap(s -> configurations.getByName(s.getRuntimeClasspathConfigurationName()).getIncoming().getResolutionResult().getAllComponents().stream()).collect(Collectors.toMap( |
135 | | - c -> ga(c.getId()), |
136 | | - c -> c.getDependencies().stream().map(ExtraJavaModuleInfoPlugin::ga).collect(Collectors.toSet()), |
137 | | - (dependencies1, dependencies2) -> dependencies1 // There can be duplications which are assumed to be the same |
138 | | - )))); |
| 134 | + toStringMap(sourceSets.stream().flatMap(s -> filteredResolutionResult(configurations.getByName(s.getRuntimeClasspathConfigurationName()), componentsOfInterest(extension)))))); |
139 | 135 | }); |
140 | 136 | t.getFrom().attribute(artifactType, fileExtension).attribute(javaModule, false); |
141 | 137 | t.getTo().attribute(artifactType, "jar").attribute(javaModule, true); |
142 | 138 | }); |
143 | 139 | } |
144 | 140 |
|
| 141 | + private static Set<String> componentsOfInterest(ExtraJavaModuleInfoPluginExtension extension) { |
| 142 | + return extension.getModuleSpecs().get().values().stream().filter(ExtraJavaModuleInfoPlugin::needsDependencies).map(ModuleSpec::getIdentifier).collect(Collectors.toSet()); |
| 143 | + } |
| 144 | + |
| 145 | + private Stream<ResolvedComponentResult> filteredResolutionResult(Configuration configuration, Set<String> componentsOfInterest) { |
| 146 | + if (componentsOfInterest.isEmpty()) { |
| 147 | + return Stream.empty(); |
| 148 | + } |
| 149 | + return configuration.getIncoming().getResolutionResult().getAllComponents().stream().filter(c -> componentsOfInterest.contains(ga(c.getId()))); |
| 150 | + } |
| 151 | + |
| 152 | + private Map<String, Set<String>> toStringMap(Stream<ResolvedComponentResult> result) { |
| 153 | + return result.collect(Collectors.toMap( |
| 154 | + c -> ga(c.getId()), |
| 155 | + c -> c.getDependencies().stream().map(ExtraJavaModuleInfoPlugin::ga).collect(Collectors.toSet()), |
| 156 | + (dependencies1, dependencies2) -> dependencies1)); |
| 157 | + } |
| 158 | + |
| 159 | + private static boolean needsDependencies(ModuleSpec moduleSpec) { |
| 160 | + return moduleSpec instanceof ModuleInfo && ((ModuleInfo) moduleSpec).requireAllDefinedDependencies; |
| 161 | + } |
| 162 | + |
145 | 163 | private static String ga(DependencyResult d) { |
146 | 164 | if (d instanceof ResolvedDependencyResult) { |
147 | 165 | return ga(((ResolvedDependencyResult) d).getSelected().getId()); |
|
0 commit comments