use of org.eclipse.aether.graph.DependencyFilter in project mule by mulesoft.
the class AetherClassPathClassifier method buildTestRunnerUrlClassification.
/**
* Application classification is being done by resolving the direct dependencies with scope
* {@value org.eclipse.aether.util.artifact.JavaScopes#TEST} for the rootArtifact. Due to Eclipse Aether resolution excludes by
* {@value org.eclipse.aether.util.artifact.JavaScopes#TEST} dependencies an imaginary pom will be created with these
* dependencies as {@value org.eclipse.aether.util.artifact.JavaScopes#COMPILE} so the dependency graph can be resolved (with
* the same results as it will be obtained from Maven).
* <p/>
* If the rootArtifact was classified as plugin its {@value org.eclipse.aether.util.artifact.JavaScopes#COMPILE} will be changed
* to {@value org.eclipse.aether.util.artifact.JavaScopes#PROVIDED} in order to exclude them from the dependency graph.
* <p/>
* Filtering logic includes the following pattern to includes the patterns defined at
* {@link ClassPathClassifierContext#getTestInclusions()}. It also excludes
* {@link ClassPathClassifierContext#getExcludedArtifacts()}, {@link ClassPathClassifierContext#getTestExclusions()}.
* <p/>
* If the application artifact has not been classified as plugin its going to be resolved as {@value #JAR_EXTENSION} in order to
* include this its compiled classes classification.
*
* @param context {@link ClassPathClassifierContext} with settings for the classification process
* @param directDependencies {@link List} of {@link Dependency} with direct dependencies for the rootArtifact
* @param rootArtifactType {@link ArtifactClassificationType} for rootArtifact @return {@link URL}s for application class loader
* @param rootArtifactRemoteRepositories remote repositories defined at the rootArtifact
*/
private List<URL> buildTestRunnerUrlClassification(ClassPathClassifierContext context, List<Dependency> directDependencies, ArtifactClassificationType rootArtifactType, List<RemoteRepository> rootArtifactRemoteRepositories) {
logger.debug("Building application classification");
Artifact rootArtifact = context.getRootArtifact();
DependencyFilter dependencyFilter = new PatternInclusionsDependencyFilter(context.getTestInclusions());
logger.debug("Using filter for dependency graph to include: '{}'", context.getTestInclusions());
List<File> appFiles = newArrayList();
List<String> exclusionsPatterns = newArrayList();
if (APPLICATION.equals(rootArtifactType)) {
logger.debug("RootArtifact identified as {} so is going to be added to application classification", APPLICATION);
File rootArtifactOutputFile = resolveRootArtifactFile(rootArtifact);
if (rootArtifactOutputFile != null) {
appFiles.add(rootArtifactOutputFile);
} else {
logger.warn("rootArtifact '{}' identified as {} but doesn't have an output {} file", rootArtifact, rootArtifactType, JAR_EXTENSION);
}
} else {
logger.debug("RootArtifact already classified as plugin or module, excluding it from application classification");
exclusionsPatterns.add(rootArtifact.getGroupId() + MAVEN_COORDINATES_SEPARATOR + rootArtifact.getArtifactId() + MAVEN_COORDINATES_SEPARATOR + "*" + MAVEN_COORDINATES_SEPARATOR + "*" + MAVEN_COORDINATES_SEPARATOR + rootArtifact.getVersion());
}
directDependencies = directDependencies.stream().map(toTransform -> {
if (toTransform.getScope().equals(TEST)) {
if (TESTS_CLASSIFIER.equals(toTransform.getArtifact().getClassifier())) {
// Exclude transitive dependencies of test-jar artifacts
return toTransform.setScope(COMPILE).setExclusions(singleton(new Exclusion("*", "*", "*", "*")));
} else {
return toTransform.setScope(COMPILE);
}
}
if ((PLUGIN.equals(rootArtifactType) || MULE_PLUGIN_CLASSIFIER.equals(toTransform.getArtifact().getClassifier()) || MULE_SERVICE_CLASSIFIER.equals(toTransform.getArtifact().getClassifier())) && toTransform.getScope().equals(COMPILE)) {
return toTransform.setScope(PROVIDED);
}
if (rootArtifactType == MODULE && toTransform.getScope().equals(COMPILE)) {
return toTransform.setScope(PROVIDED);
}
Artifact artifact = toTransform.getArtifact();
if (context.getApplicationSharedLibCoordinates().contains(artifact.getGroupId() + ":" + artifact.getArtifactId())) {
return toTransform.setScope(COMPILE);
}
return toTransform;
}).collect(toList());
logger.debug("OR exclude: {}", context.getExcludedArtifacts());
exclusionsPatterns.addAll(context.getExcludedArtifacts());
if (!context.getTestExclusions().isEmpty()) {
logger.debug("OR exclude application specific artifacts: {}", context.getTestExclusions());
exclusionsPatterns.addAll(context.getTestExclusions());
}
try {
List<Dependency> managedDependencies = newArrayList(dependencyResolver.readArtifactDescriptor(rootArtifact).getManagedDependencies());
managedDependencies.addAll(directDependencies.stream().filter(directDependency -> !directDependency.getScope().equals(TEST)).collect(toList()));
logger.debug("Resolving dependency graph for '{}' scope direct dependencies: {} and managed dependencies {}", TEST, directDependencies, managedDependencies);
final Dependency rootTestDependency = new Dependency(new DefaultArtifact(rootArtifact.getGroupId(), rootArtifact.getArtifactId(), TESTS_CLASSIFIER, JAR_EXTENSION, rootArtifact.getVersion()), TEST);
List<File> urls = dependencyResolver.resolveDependencies(rootTestDependency, directDependencies, managedDependencies, orFilter(dependencyFilter, new PatternExclusionsDependencyFilter(exclusionsPatterns)), rootArtifactRemoteRepositories);
appFiles.addAll(urls);
} catch (Exception e) {
throw new IllegalStateException("Couldn't resolve dependencies for application '" + context.getRootArtifact() + "' classification", e);
}
List<URL> testRunnerLibUrls = newArrayList(toUrl(appFiles));
logger.debug("Appending URLs to test runner plugin: {}", context.getTestRunnerPluginUrls());
testRunnerLibUrls.addAll(context.getTestRunnerPluginUrls());
resolveSnapshotVersionsToTimestampedFromClassPath(testRunnerLibUrls, context.getClassPathURLs());
return testRunnerLibUrls;
}
use of org.eclipse.aether.graph.DependencyFilter in project mule by mulesoft.
the class AetherClassPathClassifier method buildContainerUrlClassification.
/**
* Container classification is being done by resolving the {@value org.eclipse.aether.util.artifact.JavaScopes#PROVIDED} direct
* dependencies of the rootArtifact. Is uses the exclusions defined in
* {@link ClassPathClassifierContext#getProvidedExclusions()} to filter the dependency graph plus
* {@link ClassPathClassifierContext#getExcludedArtifacts()}.
* <p/>
* In order to resolve correctly the {@value org.eclipse.aether.util.artifact.JavaScopes#PROVIDED} direct dependencies it will
* get for each one the manage dependencies and use that list to resolve the graph.
*
* @param context {@link ClassPathClassifierContext} with settings for the classification process
* @param pluginUrlClassifications {@link PluginUrlClassification}s to check if rootArtifact was classified as plugin
* @param rootArtifactType {@link ArtifactClassificationType} for rootArtifact
* @param rootArtifactRemoteRepositories remote repositories defined at the rootArtifact
* @return {@link List} of {@link URL}s for the container class loader
*/
private List<URL> buildContainerUrlClassification(ClassPathClassifierContext context, List<Dependency> directDependencies, List<ArtifactUrlClassification> serviceUrlClassifications, List<PluginUrlClassification> pluginUrlClassifications, ArtifactClassificationType rootArtifactType, List<RemoteRepository> rootArtifactRemoteRepositories) {
directDependencies = directDependencies.stream().filter(getContainerDirectDependenciesFilter(rootArtifactType)).filter(dependency -> {
Artifact artifact = dependency.getArtifact();
return (!serviceUrlClassifications.stream().filter(artifactUrlClassification -> artifactUrlClassification.getArtifactId().equals(toId(artifact))).findAny().isPresent() && !pluginUrlClassifications.stream().filter(artifactUrlClassification -> artifactUrlClassification.getArtifactId().equals(toId(artifact))).findAny().isPresent());
}).map(depToTransform -> depToTransform.setScope(COMPILE)).collect(toList());
// Add logging dependencies to avoid every module from having to declare this dependency.
// This brings the slf4j bridges required by transitive dependencies of the container to its classpath
// TODO MULE-10837 Externalize this dependency along with the other commonly used container dependencies.
directDependencies.add(new Dependency(new DefaultArtifact(RUNTIME_GROUP_ID, LOGGING_ARTIFACT_ID, JAR_EXTENSION, muleVersion), COMPILE));
logger.debug("Selected direct dependencies to be used for resolving container dependency graph (changed to compile in " + "order to resolve the graph): {}", directDependencies);
Set<Dependency> managedDependencies = selectContainerManagedDependencies(context, directDependencies, rootArtifactType, rootArtifactRemoteRepositories);
logger.debug("Collected managed dependencies from direct provided dependencies to be used for resolving container " + "dependency graph: {}", managedDependencies);
List<String> excludedFilterPattern = newArrayList(context.getProvidedExclusions());
excludedFilterPattern.addAll(context.getExcludedArtifacts());
if (!pluginUrlClassifications.isEmpty()) {
excludedFilterPattern.addAll(pluginUrlClassifications.stream().map(pluginUrlClassification -> pluginUrlClassification.getArtifactId()).collect(toList()));
}
if (!serviceUrlClassifications.isEmpty()) {
excludedFilterPattern.addAll(serviceUrlClassifications.stream().map(serviceUrlClassification -> serviceUrlClassification.getArtifactId()).collect(toList()));
}
logger.debug("Resolving dependencies for container using exclusion filter patterns: {}", excludedFilterPattern);
final DependencyFilter dependencyFilter = new PatternExclusionsDependencyFilter(excludedFilterPattern);
List<URL> containerUrls;
try {
containerUrls = toUrl(dependencyResolver.resolveDependencies(null, directDependencies, newArrayList(managedDependencies), dependencyFilter, rootArtifactRemoteRepositories));
} catch (Exception e) {
throw new IllegalStateException("Couldn't resolve dependencies for Container", e);
}
containerUrls = containerUrls.stream().filter(url -> {
String file = toFile(url).getAbsolutePath();
return !(endsWithIgnoreCase(file, POM_XML) || endsWithIgnoreCase(file, POM_EXTENSION) || endsWithIgnoreCase(file, ZIP_EXTENSION));
}).collect(toList());
if (MODULE.equals(rootArtifactType)) {
File rootArtifactOutputFile = resolveRootArtifactFile(context.getRootArtifact());
if (rootArtifactOutputFile == null) {
throw new IllegalStateException("rootArtifact (" + context.getRootArtifact() + ") identified as MODULE but doesn't have an output");
}
containerUrls.add(0, toUrl(rootArtifactOutputFile));
}
resolveSnapshotVersionsToTimestampedFromClassPath(containerUrls, context.getClassPathURLs());
return containerUrls;
}
use of org.eclipse.aether.graph.DependencyFilter in project storm by apache.
the class DependencyResolver method resolve.
/**
* Resolve dependencies and return downloaded information of artifacts.
*
* @param dependencies the list of dependency
* @return downloaded information of artifacts
* @throws DependencyResolutionException If the dependency tree could not be built or any dependency
* artifact could not be resolved.
* @throws ArtifactResolutionException If the artifact could not be resolved.
*/
public List<ArtifactResult> resolve(List<Dependency> dependencies) throws DependencyResolutionException, ArtifactResolutionException {
if (dependencies.size() == 0) {
return Collections.EMPTY_LIST;
}
CollectRequest collectRequest = new CollectRequest();
collectRequest.setRoot(dependencies.get(0));
for (int idx = 1; idx < dependencies.size(); idx++) {
collectRequest.addDependency(dependencies.get(idx));
}
for (RemoteRepository repository : remoteRepositories) {
collectRequest.addRepository(repository);
}
DependencyFilter classpathFilter = DependencyFilterUtils.classpathFilter(JavaScopes.COMPILE, JavaScopes.RUNTIME);
DependencyRequest dependencyRequest = new DependencyRequest(collectRequest, classpathFilter);
return system.resolveDependencies(session, dependencyRequest).getArtifactResults();
}
use of org.eclipse.aether.graph.DependencyFilter in project pinpoint by naver.
the class DependencyResolver method resolveArtifactsAndDependencies.
public List<File> resolveArtifactsAndDependencies(List<Artifact> artifacts) throws DependencyResolutionException {
List<Dependency> dependencies = new ArrayList<>();
for (Artifact artifact : artifacts) {
dependencies.add(new Dependency(artifact, JavaScopes.RUNTIME));
}
CollectRequest collectRequest = new CollectRequest((Dependency) null, dependencies, repositories);
DependencyFilter classpathFilter = DependencyFilterUtils.classpathFilter(JavaScopes.RUNTIME);
DependencyRequest dependencyRequest = new DependencyRequest(collectRequest, classpathFilter);
DependencyResult result = system.resolveDependencies(session, dependencyRequest);
List<File> files = new ArrayList<>();
for (ArtifactResult artifactResult : result.getArtifactResults()) {
files.add(artifactResult.getArtifact().getFile());
}
return files;
}
Aggregations