Skip to content

Commit

Permalink
Avoid resolving project dependencies in 'resolveAllDependencies' task
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-vieira committed Oct 29, 2024
1 parent b6d2d4b commit 7449d6d
Showing 1 changed file with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
import org.elasticsearch.gradle.VersionProperties;
import org.gradle.api.DefaultTask;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.FileCollectionDependency;
import org.gradle.api.artifacts.component.ModuleComponentIdentifier;
import org.gradle.api.file.FileCollection;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.provider.ProviderFactory;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.TaskAction;
Expand All @@ -23,12 +26,8 @@

import java.util.Collection;
import java.util.stream.Collectors;

import javax.inject.Inject;

import static org.elasticsearch.gradle.DistributionDownloadPlugin.DISTRO_EXTRACTED_CONFIG_PREFIX;
import static org.elasticsearch.gradle.internal.test.rest.compat.compat.LegacyYamlRestCompatTestPlugin.BWC_MINOR_CONFIG_NAME;

public abstract class ResolveAllDependencies extends DefaultTask {

private boolean resolveJavaToolChain = false;
Expand All @@ -37,18 +36,32 @@ public abstract class ResolveAllDependencies extends DefaultTask {
protected abstract JavaToolchainService getJavaToolchainService();

private final ObjectFactory objectFactory;
private final ProviderFactory providerFactory;

private Collection<Configuration> configs;

@Inject
public ResolveAllDependencies(ObjectFactory objectFactory) {
public ResolveAllDependencies(ObjectFactory objectFactory, ProviderFactory providerFactory) {
this.objectFactory = objectFactory;
this.providerFactory = providerFactory;
}

@InputFiles
public FileCollection getResolvedArtifacts() {
return objectFactory.fileCollection()
.from(configs.stream().filter(ResolveAllDependencies::canBeResolved).collect(Collectors.toList()));
.from(configs.stream()
.filter(ResolveAllDependencies::canBeResolved)
.map(c -> {
// Make a copy of the configuration, omitting file collection dependencies to avoid building project artifacts
Configuration copy = c.copyRecursive(d -> d instanceof FileCollectionDependency == false);
copy.setCanBeConsumed(false);
return copy;
})
// Include only module dependencies, ignoring things like project dependencies so we don't unnecessarily build stuff
.map(c -> c.getIncoming().artifactView(v -> v.lenient(true).componentFilter(i -> i instanceof ModuleComponentIdentifier)))
.map(artifactView -> providerFactory.provider(artifactView::getFiles))
.collect(Collectors.toList())
);
}

@TaskAction
Expand Down Expand Up @@ -95,8 +108,8 @@ private static boolean canBeResolved(Configuration configuration) {
return false;
}
}
return configuration.getName().startsWith(DISTRO_EXTRACTED_CONFIG_PREFIX) == false
&& configuration.getName().equals(BWC_MINOR_CONFIG_NAME) == false;

return true;
}

}

0 comments on commit 7449d6d

Please sign in to comment.