Skip to content

Commit

Permalink
Download source artifacts in parallel (#1232)
Browse files Browse the repository at this point in the history
  • Loading branch information
modmuss50 authored Dec 23, 2024
1 parent 40d17ba commit 2ba633b
Showing 1 changed file with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;

import com.google.common.collect.ImmutableMap;
Expand All @@ -43,6 +44,8 @@
import org.gradle.api.artifacts.FileCollectionDependency;
import org.gradle.api.artifacts.MutableVersionConstraint;
import org.gradle.api.artifacts.ResolvedArtifact;
import org.gradle.api.artifacts.component.ComponentArtifactIdentifier;
import org.gradle.api.artifacts.component.ComponentIdentifier;
import org.gradle.api.artifacts.dsl.DependencyHandler;
import org.gradle.api.artifacts.query.ArtifactResolutionQuery;
import org.gradle.api.artifacts.result.ArtifactResult;
Expand Down Expand Up @@ -243,7 +246,10 @@ private static void createConstraints(ArtifactRef artifact, Configuration target
private static List<ArtifactRef> resolveArtifacts(Project project, Configuration configuration) {
final List<ArtifactRef> artifacts = new ArrayList<>();

for (ResolvedArtifact artifact : configuration.getResolvedConfiguration().getResolvedArtifacts()) {
final Set<ResolvedArtifact> resolvedArtifacts = configuration.getResolvedConfiguration().getResolvedArtifacts();
downloadAllSources(project, resolvedArtifacts);

for (ResolvedArtifact artifact : resolvedArtifacts) {
final Path sources = findSources(project, artifact);
artifacts.add(new ArtifactRef.ResolvedArtifactRef(artifact, sources));
}
Expand All @@ -270,6 +276,27 @@ private static String getNameWithoutExtension(Path file) {
return (dotIndex == -1) ? fileName : fileName.substring(0, dotIndex);
}

private static void downloadAllSources(Project project, Set<ResolvedArtifact> resolvedArtifacts) {
if (isCIBuild()) {
return;
}

final DependencyHandler dependencies = project.getDependencies();

List<ComponentIdentifier> componentIdentifiers = resolvedArtifacts.stream()
.map(ResolvedArtifact::getId)
.map(ComponentArtifactIdentifier::getComponentIdentifier)
.toList();

//noinspection unchecked
ArtifactResolutionQuery query = dependencies.createArtifactResolutionQuery()
.forComponents(componentIdentifiers)
.withArtifacts(JvmLibrary.class, SourcesArtifact.class);

// Run a single query for all of the artifacts, this will allow them to be resolved in parallel before they are queried individually
query.execute();
}

@Nullable
public static Path findSources(Project project, ResolvedArtifact artifact) {
if (isCIBuild()) {
Expand Down

0 comments on commit 2ba633b

Please sign in to comment.