Skip to content

Commit afc5586

Browse files
committed
Only pass necessary dependency information to transform
This avoids moving a lot of data in memory when having large classpaths. It also avoids potential performance regressions in scenarios where 'requireAllDefinedDependencies' is not used at all. #40
1 parent 3824c87 commit afc5586

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

src/main/java/org/gradlex/javamodule/moduleinfo/ExtraJavaModuleInfoPlugin.java

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.gradle.api.artifacts.dsl.DependencyHandler;
2828
import org.gradle.api.artifacts.result.DependencyResult;
2929
import org.gradle.api.artifacts.result.ResolvedArtifactResult;
30+
import org.gradle.api.artifacts.result.ResolvedComponentResult;
3031
import org.gradle.api.artifacts.result.ResolvedDependencyResult;
3132
import org.gradle.api.attributes.Attribute;
3233
import org.gradle.api.attributes.Category;
@@ -41,8 +42,10 @@
4142

4243
import java.util.Collection;
4344
import java.util.List;
45+
import java.util.Map;
4446
import java.util.Set;
4547
import java.util.stream.Collectors;
48+
import java.util.stream.Stream;
4649

4750
/**
4851
* Entry point of the plugin.
@@ -124,24 +127,39 @@ private void registerTransform(String fileExtension, Project project, ExtraJavaM
124127
javaModulesMergeJars.getIncoming().artifactView(v -> v.lenient(true)).getArtifacts().getArtifacts());
125128
p.getMergeJarIds().set(artifacts.map(new IdExtractor()));
126129
p.getMergeJars().set(artifacts.map(new FileExtractor(project.getLayout())));
130+
127131
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))))));
133133
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))))));
139135
});
140136
t.getFrom().attribute(artifactType, fileExtension).attribute(javaModule, false);
141137
t.getTo().attribute(artifactType, "jar").attribute(javaModule, true);
142138
});
143139
}
144140

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+
145163
private static String ga(DependencyResult d) {
146164
if (d instanceof ResolvedDependencyResult) {
147165
return ga(((ResolvedDependencyResult) d).getSelected().getId());

0 commit comments

Comments
 (0)