Search in sources :

Example 96 with DefaultArtifact

use of org.eclipse.aether.artifact.DefaultArtifact in project mule by mulesoft.

the class AetherClassPathClassifier method discoverDependency.

/**
 * Discovers the {@link Dependency} from the list of directDependencies using the artifact coordiantes in format of:
 *
 * <pre>
 * groupId:artifactId
 * </pre>
 * <p/>
 * If the coordinates matches to the rootArtifact it will return a {@value org.eclipse.aether.util.artifact.JavaScopes#COMPILE}
 * {@link Dependency}.
 *
 * @param artifactCoords Maven coordinates that define the artifact dependency
 * @param rootArtifact {@link Artifact} that defines the current artifact that requested to build this class loaders
 * @param directDependencies {@link List} of {@link Dependency} with direct dependencies for the rootArtifact
 * @return {@link Dependency} representing the artifact if declared as direct dependency or rootArtifact if they match it or
 *         {@link Optional#EMPTY} if couldn't found the dependency.
 * @throws {@link IllegalArgumentException} if artifactCoords are not in the expected format
 */
private Optional<Dependency> discoverDependency(String artifactCoords, Artifact rootArtifact, List<Dependency> directDependencies) {
    final String[] artifactCoordsSplit = artifactCoords.split(MAVEN_COORDINATES_SEPARATOR);
    if (artifactCoordsSplit.length < 2 || artifactCoordsSplit.length > 3) {
        throw new IllegalArgumentException("Artifact coordinates should be in format of groupId:artifactId or groupId:artifactId:classifier, '" + artifactCoords + "' is not a valid format");
    }
    String groupId = artifactCoordsSplit[0];
    String artifactId = artifactCoordsSplit[1];
    Optional<String> classifier = artifactCoordsSplit.length > 2 ? of(artifactCoordsSplit[2]) : empty();
    if (rootArtifact.getGroupId().equals(groupId) && rootArtifact.getArtifactId().equals(artifactId)) {
        logger.debug("'{}' artifact coordinates matched with rootArtifact '{}', resolving version from rootArtifact", artifactCoords, rootArtifact);
        final DefaultArtifact artifact = new DefaultArtifact(groupId, artifactId, JAR_EXTENSION, rootArtifact.getVersion());
        logger.debug("'{}' artifact coordinates resolved to: '{}'", artifactCoords, artifact);
        return of(new Dependency(artifact, COMPILE));
    } else {
        logger.debug("Resolving version for '{}' from direct dependencies", artifactCoords);
        return findDirectDependency(groupId, artifactId, classifier, directDependencies);
    }
}
Also used : Dependency(org.eclipse.aether.graph.Dependency) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact)

Example 97 with DefaultArtifact

use of org.eclipse.aether.artifact.DefaultArtifact 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;
}
Also used : PLUGIN(org.mule.test.runner.api.ArtifactClassificationType.PLUGIN) ListIterator(java.util.ListIterator) URL(java.net.URL) Optional.of(java.util.Optional.of) TEST(org.eclipse.aether.util.artifact.JavaScopes.TEST) LoggerFactory(org.slf4j.LoggerFactory) DependencyFilterUtils.orFilter(org.eclipse.aether.util.filter.DependencyFilterUtils.orFilter) FileUtils.toFile(org.apache.commons.io.FileUtils.toFile) APPLICATION(org.mule.test.runner.api.ArtifactClassificationType.APPLICATION) ArtifactDescriptorException(org.eclipse.aether.resolution.ArtifactDescriptorException) Collections.singleton(java.util.Collections.singleton) DependencyFilterUtils.andFilter(org.eclipse.aether.util.filter.DependencyFilterUtils.andFilter) Map(java.util.Map) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) Collectors.toSet(java.util.stream.Collectors.toSet) ArtifactIdUtils.toId(org.eclipse.aether.util.artifact.ArtifactIdUtils.toId) PROVIDED(org.eclipse.aether.util.artifact.JavaScopes.PROVIDED) Maps.newHashMap(com.google.common.collect.Maps.newHashMap) Collections.emptyList(java.util.Collections.emptyList) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Set(java.util.Set) Artifact(org.eclipse.aether.artifact.Artifact) Preconditions.checkNotNull(org.mule.runtime.api.util.Preconditions.checkNotNull) PatternInclusionsDependencyFilter(org.mule.test.runner.classification.PatternInclusionsDependencyFilter) String.format(java.lang.String.format) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) Optional(java.util.Optional) Maps.newLinkedHashMap(com.google.common.collect.Maps.newLinkedHashMap) ArtifactDescriptorResult(org.eclipse.aether.resolution.ArtifactDescriptorResult) PatternExclusionsDependencyFilter(org.mule.test.runner.classification.PatternExclusionsDependencyFilter) Optional.empty(java.util.Optional.empty) DependencyFilter(org.eclipse.aether.graph.DependencyFilter) Extension(org.mule.runtime.extension.api.annotation.Extension) Dependency(org.eclipse.aether.graph.Dependency) AtomicReference(java.util.concurrent.atomic.AtomicReference) COMPILE(org.eclipse.aether.util.artifact.JavaScopes.COMPILE) MODULE(org.mule.test.runner.api.ArtifactClassificationType.MODULE) VersionChecker.areCompatibleVersions(org.mule.maven.client.internal.util.VersionChecker.areCompatibleVersions) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) Properties(java.util.Properties) Logger(org.slf4j.Logger) MalformedURLException(java.net.MalformedURLException) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Sets.newLinkedHashSet(com.google.common.collect.Sets.newLinkedHashSet) IOException(java.io.IOException) File(java.io.File) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) Collectors.toList(java.util.stream.Collectors.toList) FileFilter(java.io.FileFilter) StringUtils.endsWithIgnoreCase(org.apache.commons.lang3.StringUtils.endsWithIgnoreCase) VersionChecker.isHighestVersion(org.mule.maven.client.internal.util.VersionChecker.isHighestVersion) WildcardFileFilter(org.apache.commons.io.filefilter.WildcardFileFilter) Exclusion(org.eclipse.aether.graph.Exclusion) DependencyFilterUtils.classpathFilter(org.eclipse.aether.util.filter.DependencyFilterUtils.classpathFilter) Collections(java.util.Collections) PatternInclusionsDependencyFilter(org.mule.test.runner.classification.PatternInclusionsDependencyFilter) PatternExclusionsDependencyFilter(org.mule.test.runner.classification.PatternExclusionsDependencyFilter) DependencyFilter(org.eclipse.aether.graph.DependencyFilter) Dependency(org.eclipse.aether.graph.Dependency) Artifact(org.eclipse.aether.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) URL(java.net.URL) ArtifactDescriptorException(org.eclipse.aether.resolution.ArtifactDescriptorException) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) PatternExclusionsDependencyFilter(org.mule.test.runner.classification.PatternExclusionsDependencyFilter) FileUtils.toFile(org.apache.commons.io.FileUtils.toFile) File(java.io.File) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact)

Example 98 with DefaultArtifact

use of org.eclipse.aether.artifact.DefaultArtifact in project mule by mulesoft.

the class DefaultWorkspaceReaderTestCase method handleTestAndTargetClassesURLs.

@Test
public void handleTestAndTargetClassesURLs() throws Exception {
    File targetTestClasses = new File(new File(bar, TARGET), TEST_CLASSES);
    assertThat(targetTestClasses.mkdirs(), is(true));
    urls.add(targetTestClasses.toURI().toURL());
    File result = findClassPathURL(artifact, bar, urls);
    assertThat(result, equalTo(targetClasses));
    result = findClassPathURL(new DefaultArtifact(artifact.getGroupId(), artifact.getArtifactId(), TESTS_CLASSIFIER, artifact.getExtension(), artifact.getVersion()), bar, urls);
    assertThat(result, equalTo(targetTestClasses));
}
Also used : File(java.io.File) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Test(org.junit.Test) SmallTest(org.mule.tck.size.SmallTest)

Example 99 with DefaultArtifact

use of org.eclipse.aether.artifact.DefaultArtifact in project spring-cloud-function by spring-cloud.

the class MemoryBasedJavaFileManager method addAndResolveDependencies.

public List<CompilationMessage> addAndResolveDependencies(String[] dependencies) {
    List<CompilationMessage> resolutionMessages = new ArrayList<>();
    for (String dependency : dependencies) {
        if (dependency.startsWith("maven:")) {
            // Resolving an explicit external archive
            String coordinates = dependency.replaceFirst("maven:\\/*", "");
            DependencyResolver engine = DependencyResolver.instance();
            try {
                File resolved = engine.resolve(new Dependency(new DefaultArtifact(coordinates), "runtime"));
                // Example:
                // dependency =
                // maven://org.springframework:spring-expression:4.3.9.RELEASE
                // resolved.toURI() =
                // file:/Users/aclement/.m2/repository/org/springframework/spring-expression/4.3.9.RELEASE/spring-expression-4.3.9.RELEASE.jar
                resolvedAdditionalDependencies.put(dependency, resolved);
            } catch (RuntimeException re) {
                CompilationMessage compilationMessage = new CompilationMessage(CompilationMessage.Kind.ERROR, re.getMessage(), null, 0, 0);
                resolutionMessages.add(compilationMessage);
            }
        } else if (dependency.startsWith("file:")) {
            resolvedAdditionalDependencies.put(dependency, new File(URI.create(dependency)));
        } else {
            resolutionMessages.add(new CompilationMessage(CompilationMessage.Kind.ERROR, "Unrecognized dependency: " + dependency + " (expected something of the form: maven://groupId:artifactId:version)", null, 0, 0));
        }
    }
    return resolutionMessages;
}
Also used : ArrayList(java.util.ArrayList) Dependency(org.eclipse.aether.graph.Dependency) ZipFile(java.util.zip.ZipFile) File(java.io.File) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact)

Example 100 with DefaultArtifact

use of org.eclipse.aether.artifact.DefaultArtifact in project syndesis by syndesisio.

the class RepackageExtensionMojo method downloadAndInstallArtifact.

/*
     * @param artifact The artifact coordinates in the format
     *            {@code <groupId>:<artifactId>[:<extension>[:<classifier>]]:<version>}, must not be {@code null}.
     *
     */
protected ArtifactResult downloadAndInstallArtifact(String artifact) throws MojoExecutionException {
    ArtifactResult result;
    ArtifactRequest request = new ArtifactRequest();
    request.setArtifact(new DefaultArtifact(artifact));
    request.setRepositories(remoteRepos);
    getLog().info("Resolving artifact " + artifact + " from " + remoteRepos);
    try {
        result = repoSystem.resolveArtifact(repoSession, request);
        return result;
    } catch (ArtifactResolutionException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    }
}
Also used : ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult)

Aggregations

DefaultArtifact (org.eclipse.aether.artifact.DefaultArtifact)110 Artifact (org.eclipse.aether.artifact.Artifact)70 ArtifactResult (org.eclipse.aether.resolution.ArtifactResult)42 File (java.io.File)39 ArtifactRequest (org.eclipse.aether.resolution.ArtifactRequest)34 ArtifactResolutionException (org.eclipse.aether.resolution.ArtifactResolutionException)29 IOException (java.io.IOException)24 Dependency (org.eclipse.aether.graph.Dependency)23 ArrayList (java.util.ArrayList)16 RemoteRepository (org.eclipse.aether.repository.RemoteRepository)13 CollectRequest (org.eclipse.aether.collection.CollectRequest)12 VersionRangeRequest (org.eclipse.aether.resolution.VersionRangeRequest)12 VersionRangeResolutionException (org.eclipse.aether.resolution.VersionRangeResolutionException)12 VersionRangeResult (org.eclipse.aether.resolution.VersionRangeResult)12 SubArtifact (org.eclipse.aether.util.artifact.SubArtifact)12 DefaultRepositorySystemSession (org.eclipse.aether.DefaultRepositorySystemSession)11 ArtifactDescriptorException (org.eclipse.aether.resolution.ArtifactDescriptorException)10 Version (org.eclipse.aether.version.Version)10 Model (org.apache.maven.model.Model)9 RepositorySystem (org.eclipse.aether.RepositorySystem)9