Search in sources :

Example 11 with ArtifactResolutionException

use of org.eclipse.aether.resolution.ArtifactResolutionException in project fabric8 by jboss-fuse.

the class AetherBasedResolver method resolve.

private File resolve(List<LocalRepository> defaultRepos, List<RemoteRepository> remoteRepos, Artifact artifact) throws IOException {
    if (artifact.getExtension().isEmpty()) {
        artifact = new DefaultArtifact(artifact.getGroupId(), artifact.getArtifactId(), artifact.getClassifier(), "jar", artifact.getVersion());
    }
    // Try with default repositories
    try {
        VersionConstraint vc = new GenericVersionScheme().parseVersionConstraint(artifact.getVersion());
        if (vc.getVersion() != null) {
            for (LocalRepository repo : defaultRepos) {
                if (vc.getVersion().toString().endsWith("SNAPSHOT") && !handlesSnapshot(repo)) {
                    continue;
                }
                DefaultRepositorySystemSession session = newSession(repo);
                try {
                    return m_repoSystem.resolveArtifact(session, new ArtifactRequest(artifact, null, null)).getArtifact().getFile();
                } catch (ArtifactResolutionException e) {
                // Ignore
                } finally {
                    releaseSession(session);
                }
            }
        }
    } catch (InvalidVersionSpecificationException e) {
    // Should not happen
    }
    DefaultRepositorySystemSession session = newSession(null);
    try {
        artifact = resolveLatestVersionRange(session, remoteRepos, artifact);
        ArtifactResult result = m_repoSystem.resolveArtifact(session, new ArtifactRequest(artifact, remoteRepos, null));
        File resolved = result.getArtifact().getFile();
        LOG.debug("Resolved ({}) as {}", artifact.toString(), resolved.getAbsolutePath());
        return resolved;
    } catch (ArtifactResolutionException e) {
        // we know there's one ArtifactResult, because there was one ArtifactRequest
        ArtifactResolutionException original = new ArtifactResolutionException(e.getResults(), "Error resolving artifact " + artifact.toString(), null);
        original.setStackTrace(e.getStackTrace());
        List<String> messages = new ArrayList<>(e.getResult().getExceptions().size());
        List<Exception> suppressed = new ArrayList<>();
        for (Exception ex : e.getResult().getExceptions()) {
            messages.add(ex.getMessage() == null ? ex.getClass().getName() : ex.getMessage());
            suppressed.add(ex);
        }
        IOException exception = new IOException(original.getMessage() + ": " + messages, original);
        for (Exception ex : suppressed) {
            exception.addSuppressed(ex);
        }
        LOG.warn(exception.getMessage(), exception);
        for (Exception ex : suppressed) {
            LOG.warn(" - " + ex.getMessage());
        }
        throw exception;
    } catch (RepositoryException e) {
        throw new IOException("Error resolving artifact " + artifact.toString(), e);
    } finally {
        releaseSession(session);
    }
}
Also used : VersionConstraint(org.eclipse.aether.version.VersionConstraint) LocalRepository(org.eclipse.aether.repository.LocalRepository) RepositoryException(org.eclipse.aether.RepositoryException) IOException(java.io.IOException) DependencyCollectionException(org.eclipse.aether.collection.DependencyCollectionException) SocketTimeoutException(java.net.SocketTimeoutException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) ArtifactNotFoundException(org.eclipse.aether.transfer.ArtifactNotFoundException) RepositoryException(org.eclipse.aether.RepositoryException) VersionRangeResolutionException(org.eclipse.aether.resolution.VersionRangeResolutionException) MetadataTransferException(org.eclipse.aether.transfer.MetadataTransferException) NoRouteToHostException(java.net.NoRouteToHostException) ArtifactTransferException(org.eclipse.aether.transfer.ArtifactTransferException) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) DependencyResolutionException(org.eclipse.aether.resolution.DependencyResolutionException) MalformedURLException(java.net.MalformedURLException) MetadataNotFoundException(org.eclipse.aether.transfer.MetadataNotFoundException) InvalidVersionSpecificationException(org.eclipse.aether.version.InvalidVersionSpecificationException) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) InvalidVersionSpecificationException(org.eclipse.aether.version.InvalidVersionSpecificationException) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) GenericVersionScheme(org.eclipse.aether.util.version.GenericVersionScheme) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) File(java.io.File) JarFile(java.util.jar.JarFile) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact)

Example 12 with ArtifactResolutionException

use of org.eclipse.aether.resolution.ArtifactResolutionException in project fabric8 by jboss-fuse.

the class AetherResolutionWithHintsTest method hintedResolution.

@Test
public void hintedResolution() throws Exception {
    final MavenConfigurationImpl mavenConfiguration = mavenConfiguration();
    mavenConfiguration.setSettings(settingsWithProxy());
    MavenResolver resolver = new AetherBasedResolver(mavenConfiguration);
    try {
        resolver.download("mvn:org.ops4j.pax.web/pax-web-api/1");
        fail("Resolution should fail");
    } catch (IOException e) {
        RepositoryException exception = ((AetherBasedResolver) resolver).findAetherException(e);
        assertNotNull(exception);
        assertTrue(exception instanceof ArtifactResolutionException);
        ArtifactResolutionException are = (ArtifactResolutionException) exception;
        assertThat(are.getResult().getExceptions().size(), equalTo(3));
        assertTrue("Non-retryable exception", are.getResult().getExceptions().get(0) instanceof ArtifactNotFoundException);
        assertTrue("Non-retryable exception", are.getResult().getExceptions().get(1) instanceof ArtifactNotFoundException);
        assertTrue("Retryable exception", are.getResult().getExceptions().get(2) instanceof ArtifactTransferException);
        assertFalse("Retryable exception", are.getResult().getExceptions().get(2) instanceof ArtifactNotFoundException);
        try {
            // try again with exception hint
            resolver.download("mvn:org.ops4j.pax.web/pax-web-api/1", e);
            fail("Resolution should fail");
        } catch (IOException e2) {
            exception = ((AetherBasedResolver) resolver).findAetherException(e2);
            assertNotNull(exception);
            assertTrue(exception instanceof ArtifactResolutionException);
            are = (ArtifactResolutionException) exception;
            assertThat(are.getResult().getExceptions().size(), equalTo(1));
            assertTrue("Retryable exception", are.getResult().getExceptions().get(0) instanceof ArtifactTransferException);
            assertFalse("Retryable exception", are.getResult().getExceptions().get(0) instanceof ArtifactNotFoundException);
        }
    } finally {
        resolver.close();
    }
}
Also used : ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) MavenConfigurationImpl(io.fabric8.maven.util.MavenConfigurationImpl) ArtifactTransferException(org.eclipse.aether.transfer.ArtifactTransferException) MavenResolver(io.fabric8.maven.MavenResolver) RepositoryException(org.eclipse.aether.RepositoryException) IOException(java.io.IOException) ArtifactNotFoundException(org.eclipse.aether.transfer.ArtifactNotFoundException) Test(org.junit.Test)

Example 13 with ArtifactResolutionException

use of org.eclipse.aether.resolution.ArtifactResolutionException in project elasticsearch-maven-plugin by alexcojocaru.

the class MyArtifactResolver method resolveArtifact.

/**
 * Resolves an Artifact from the repositories.
 *
 * @param coordinates The artifact coordinates
 * @return The local file resolved/downloaded for the given coordinates
 * @throws ResolutionException If the artifact cannot be resolved
 */
public File resolveArtifact(String coordinates) throws ResolutionException {
    ArtifactRequest request = new ArtifactRequest();
    Artifact artifact = new DefaultArtifact(coordinates);
    request.setArtifact(artifact);
    request.setRepositories(remoteRepositories);
    log.debug(String.format("Resolving artifact %s from %s", artifact, remoteRepositories));
    ArtifactResult result;
    try {
        result = repositorySystem.resolveArtifact(repositorySession, request);
    } catch (ArtifactResolutionException e) {
        throw new ResolutionException(e.getMessage(), e);
    }
    log.debug(String.format("Resolved artifact %s to %s from %s", artifact, result.getArtifact().getFile(), result.getRepository()));
    return result.getArtifact().getFile();
}
Also used : ResolutionException(com.github.alexcojocaru.mojo.elasticsearch.v2.configuration.ResolutionException) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult)

Example 14 with ArtifactResolutionException

use of org.eclipse.aether.resolution.ArtifactResolutionException in project BIMserver by opensourceBIM.

the class MavenPluginLocation method createMavenVersion.

private MavenPluginVersion createMavenVersion(Version version) throws ArtifactDescriptorException, FileNotFoundException, IOException, ArtifactResolutionException, XmlPullParserException {
    ArtifactDescriptorRequest descriptorRequest = new ArtifactDescriptorRequest();
    Artifact versionArtifact = new DefaultArtifact(groupId, artifactId, "pom", version.toString());
    descriptorRequest.setArtifact(versionArtifact);
    descriptorRequest.setRepositories(mavenPluginRepository.getRepositoriesAsList());
    MavenPluginVersion mavenPluginVersion = new MavenPluginVersion(versionArtifact, version);
    ArtifactDescriptorResult descriptorResult;
    descriptorResult = mavenPluginRepository.getSystem().readArtifactDescriptor(mavenPluginRepository.getSession(), descriptorRequest);
    try {
        ArtifactRequest request = new ArtifactRequest();
        request.setArtifact(descriptorResult.getArtifact());
        request.setRepositories(mavenPluginRepository.getRepositoriesAsList());
        ArtifactResult resolveArtifact = mavenPluginRepository.getSystem().resolveArtifact(mavenPluginRepository.getSession(), request);
        File pomFile = resolveArtifact.getArtifact().getFile();
        MavenXpp3Reader mavenreader = new MavenXpp3Reader();
        try (FileReader fileReader = new FileReader(pomFile)) {
            Model model = mavenreader.read(fileReader);
            mavenPluginVersion.setModel(model);
        }
    } catch (Exception e) {
        LOGGER.error(e.getMessage());
    }
    for (org.eclipse.aether.graph.Dependency dependency : descriptorResult.getDependencies()) {
        DefaultArtifactVersion artifactVersion = new DefaultArtifactVersion(dependency.getArtifact().getVersion());
        mavenPluginVersion.addDependency(new MavenDependency(dependency.getArtifact(), artifactVersion));
    }
    return mavenPluginVersion;
}
Also used : DefaultArtifactVersion(org.apache.maven.artifact.versioning.DefaultArtifactVersion) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact) ArtifactDescriptorException(org.eclipse.aether.resolution.ArtifactDescriptorException) VersionRangeResolutionException(org.eclipse.aether.resolution.VersionRangeResolutionException) ParseException(java.text.ParseException) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult) ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) Model(org.apache.maven.model.Model) FileReader(java.io.FileReader) ArtifactDescriptorResult(org.eclipse.aether.resolution.ArtifactDescriptorResult) File(java.io.File) ArtifactDescriptorRequest(org.eclipse.aether.resolution.ArtifactDescriptorRequest) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact)

Example 15 with ArtifactResolutionException

use of org.eclipse.aether.resolution.ArtifactResolutionException in project BIMserver by opensourceBIM.

the class PluginBundleDatabaseAction method processMavenPluginLocation.

public SPluginBundle processMavenPluginLocation(MavenPluginLocation mavenPluginLocation, boolean strictVersionChecking, ArtifactVersion bimserverVersion) {
    SPluginBundle pluginBundle = new SPluginBundle();
    boolean usefulBundle = false;
    Iterator<MavenPluginVersion> iterateAllVersions = mavenPluginLocation.iterateAllVersions();
    int matchingPlugins = 0;
    while (iterateAllVersions.hasNext() && matchingPlugins < DEFAULT_MATCHING_PLUGINS) {
        MavenPluginVersion mavenPluginVersion = iterateAllVersions.next();
        SPluginBundleVersion sPluginBundleVersion = new SPluginBundleVersion();
        boolean useful = true;
        boolean hasPluginBaseDependency = false;
        for (MavenDependency mavenDependency : mavenPluginVersion.getDependencies()) {
            if (mavenDependency.getArtifact().getGroupId().equals("org.opensourcebim")) {
                String artifactId = mavenDependency.getArtifact().getArtifactId();
                // for the plugin, it's version has to be ok
                if (artifactId.equals("shared") || artifactId.equals("pluginbase")) {
                    hasPluginBaseDependency = true;
                    try {
                        String version = mavenDependency.getArtifact().getVersion();
                        if (!version.contains("[") && !version.contains("(")) {
                            version = "[" + version + "]";
                        }
                        VersionRange versionRange = VersionRange.createFromVersionSpec(version);
                        if (bimserverVersion == null || versionRange.containsVersion(bimserverVersion) || (bimserverVersion.getQualifier() != null && bimserverVersion.getQualifier().equals("SNAPSHOT"))) {
                        // OK
                        } else {
                            sPluginBundleVersion.setMismatch(true);
                            if (strictVersionChecking) {
                                useful = false;
                            }
                        }
                    } catch (InvalidVersionSpecificationException e) {
                        LOGGER.error("", e);
                    }
                }
            }
        }
        if (useful && hasPluginBaseDependency) {
            usefulBundle = true;
            matchingPlugins++;
            sPluginBundleVersion.setName(mavenPluginVersion.getModel().getName());
            sPluginBundleVersion.setOrganization(mavenPluginVersion.getModel().getOrganization().getName());
            sPluginBundleVersion.setArtifactId(mavenPluginLocation.getArtifactId());
            sPluginBundleVersion.setGroupId(mavenPluginLocation.getGroupId());
            try {
                sPluginBundleVersion.setRepository(mavenPluginLocation.getRepository(mavenPluginVersion.getVersion().toString()));
            } catch (ArtifactResolutionException e) {
                LOGGER.error("", e);
            }
            sPluginBundleVersion.setType(SPluginBundleType.MAVEN);
            sPluginBundleVersion.setVersion(mavenPluginVersion.getVersion().toString());
            sPluginBundleVersion.setDescription(mavenPluginVersion.getModel().getDescription());
            pluginBundle.setName(mavenPluginVersion.getModel().getName());
            pluginBundle.setOrganization(mavenPluginVersion.getModel().getOrganization().getName());
            if (pluginBundle.getLatestVersion() == null) {
                pluginBundle.setLatestVersion(sPluginBundleVersion);
            }
            pluginBundle.getAvailableVersions().add(sPluginBundleVersion);
            try {
                sPluginBundleVersion.setIcon(mavenPluginLocation.getVersionIcon(mavenPluginVersion.getVersion().toString()));
            } catch (ArtifactResolutionException e) {
            // This is not important
            } catch (IOException e) {
                LOGGER.error("", e);
            }
            try {
                GregorianCalendar date = mavenPluginLocation.getVersionDate(mavenPluginVersion.getVersion().toString());
                if (date != null) {
                    sPluginBundleVersion.setDate(date.getTime());
                }
            // byte[] bytes = Files.readAllBytes(date);
            // Properties properties = new Properties();
            // properties.load(new ByteArrayInputStream(bytes));
            // String buildDateString = properties.getProperty("build.date");
            // 
            // DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
            // try {
            // } catch (ParseException e) {
            // LOGGER.error("Invalid date format for plugin " + mavenPluginVersion.getModel().getName() + ": '" + buildDateString + "'");
            // }
            } catch (ArtifactResolutionException e) {
            // Not a problem
            } catch (Exception e) {
                LOGGER.error("", e);
            }
        }
    }
    if (usefulBundle) {
        // }});
        return pluginBundle;
    }
    return null;
}
Also used : SPluginBundleVersion(org.bimserver.interfaces.objects.SPluginBundleVersion) SPluginBundle(org.bimserver.interfaces.objects.SPluginBundle) GregorianCalendar(java.util.GregorianCalendar) VersionRange(org.apache.maven.artifact.versioning.VersionRange) IOException(java.io.IOException) InvalidVersionSpecificationException(org.apache.maven.artifact.versioning.InvalidVersionSpecificationException) IOException(java.io.IOException) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) InvalidVersionSpecificationException(org.apache.maven.artifact.versioning.InvalidVersionSpecificationException) MavenPluginVersion(org.bimserver.plugins.MavenPluginVersion) MavenDependency(org.bimserver.plugins.MavenDependency)

Aggregations

ArtifactResolutionException (org.eclipse.aether.resolution.ArtifactResolutionException)52 ArtifactResult (org.eclipse.aether.resolution.ArtifactResult)33 ArtifactRequest (org.eclipse.aether.resolution.ArtifactRequest)32 DefaultArtifact (org.eclipse.aether.artifact.DefaultArtifact)27 File (java.io.File)21 Artifact (org.eclipse.aether.artifact.Artifact)21 IOException (java.io.IOException)19 VersionRangeResolutionException (org.eclipse.aether.resolution.VersionRangeResolutionException)10 FileNotFoundException (java.io.FileNotFoundException)9 MalformedURLException (java.net.MalformedURLException)9 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)9 DefaultRepositorySystemSession (org.eclipse.aether.DefaultRepositorySystemSession)9 XmlPullParserException (org.codehaus.plexus.util.xml.pull.XmlPullParserException)8 RepositorySystemSession (org.eclipse.aether.RepositorySystemSession)8 RemoteRepository (org.eclipse.aether.repository.RemoteRepository)7 ArtifactDescriptorException (org.eclipse.aether.resolution.ArtifactDescriptorException)7 DependencyResolutionException (org.eclipse.aether.resolution.DependencyResolutionException)7 ArrayList (java.util.ArrayList)6 Model (org.apache.maven.model.Model)6 DependencyCollectionException (org.eclipse.aether.collection.DependencyCollectionException)6