use of org.mule.test.runner.classification.PatternInclusionsDependencyFilter in project mule by mulesoft.
the class AetherClassPathClassifier method buildPluginUrlClassification.
/**
* Classifies an {@link Artifact} recursively. {@value org.eclipse.aether.util.artifact.JavaScopes#COMPILE} dependencies will be
* resolved for building the {@link URL}'s for the class loader. Once classified the node is added to {@link Map} of
* artifactsClassified.
*
* @param artifactToClassify {@link Artifact} that represents the artifact to be classified
* @param context {@link ClassPathClassifierContext} with settings for the classification process
* @param artifactsClassified {@link Map} that contains already classified plugins
* @param rootArtifactRemoteRepositories remote repositories defined at the root artifact.
*/
private void buildPluginUrlClassification(Artifact artifactToClassify, ClassPathClassifierContext context, Predicate<Dependency> directDependenciesFilter, Set<ArtifactClassificationNode> artifactsClassified, List<RemoteRepository> rootArtifactRemoteRepositories) {
List<URL> urls;
try {
final DependencyFilter dependencyFilter = andFilter(classpathFilter(COMPILE), new PatternExclusionsDependencyFilter(context.getExcludedArtifacts()), orFilter(new PatternExclusionsDependencyFilter("*:*:*:" + MULE_PLUGIN_CLASSIFIER + ":*"), new PatternInclusionsDependencyFilter(toId(artifactToClassify))));
urls = toUrl(dependencyResolver.resolveDependencies(new Dependency(artifactToClassify, COMPILE), emptyList(), emptyList(), dependencyFilter, rootArtifactRemoteRepositories));
} catch (Exception e) {
throw new IllegalStateException("Couldn't resolve dependencies for artifact: '" + artifactToClassify + "' classification", e);
}
List<Dependency> directDependencies;
List<ArtifactClassificationNode> artifactDependencies = newArrayList();
try {
directDependencies = dependencyResolver.getDirectDependencies(artifactToClassify, rootArtifactRemoteRepositories);
} catch (ArtifactDescriptorException e) {
throw new IllegalStateException("Couldn't get direct dependencies for artifact: '" + artifactToClassify + "'", e);
}
logger.debug("Searching for dependencies on direct dependencies of artifact {}", artifactToClassify);
List<Artifact> pluginArtifactDependencies = filterArtifacts(directDependencies, directDependenciesFilter);
logger.debug("Artifacts {} identified a plugin dependencies for plugin {}", pluginArtifactDependencies, artifactToClassify);
pluginArtifactDependencies.stream().map(artifact -> {
Optional<ArtifactClassificationNode> artifactClassificationNodeOptional = findArtifactClassified(artifactsClassified, artifact);
if (!artifactClassificationNodeOptional.isPresent()) {
buildPluginUrlClassification(artifact, context, directDependenciesFilter, artifactsClassified, rootArtifactRemoteRepositories);
} else {
if (!areCompatibleVersions(artifactClassificationNodeOptional.get().getArtifact().getVersion(), artifact.getVersion())) {
throw new IllegalStateException(format("Incompatible version of artifacts found: %s and %s", toId(artifactClassificationNodeOptional.get().getArtifact()), toId(artifact)));
}
logger.debug("Checking for highest version of artifact, already discovered: '{}' versus: '{}'", toId(artifactClassificationNodeOptional.get().getArtifact()), toId(artifact));
if (isHighestVersion(artifact.getVersion(), artifactClassificationNodeOptional.get().getArtifact().getVersion())) {
artifactsClassified.remove(artifactClassificationNodeOptional.get());
buildPluginUrlClassification(artifact, context, directDependenciesFilter, artifactsClassified, rootArtifactRemoteRepositories);
logger.warn("Replacing artifact: '{}' for highest version: '{}'", toId(artifactClassificationNodeOptional.get().getArtifact()), toId(artifact));
}
}
return findArtifactClassified(artifactsClassified, artifact).orElseThrow(() -> new IllegalStateException(format("Should %s be already added to the list of artifacts classified", toId(artifact))));
}).forEach(artifactDependencies::add);
final List<Class> exportClasses = getArtifactExportedClasses(artifactToClassify, context, rootArtifactRemoteRepositories);
resolveSnapshotVersionsToTimestampedFromClassPath(urls, context.getClassPathURLs());
ArtifactClassificationNode artifactUrlClassification = new ArtifactClassificationNode(artifactToClassify, urls, exportClasses, artifactDependencies);
logger.debug("Artifact discovered: {}", toId(artifactUrlClassification.getArtifact()));
artifactsClassified.add(artifactUrlClassification);
}
use of org.mule.test.runner.classification.PatternInclusionsDependencyFilter 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;
}
Aggregations