Search in sources :

Example 1 with DependencyResolverException

use of org.apache.maven.shared.dependencies.resolve.DependencyResolverException in project maven-dependency-plugin by apache.

the class PurgeLocalRepositoryMojo method getFilteredResolvedArtifacts.

private Set<Artifact> getFilteredResolvedArtifacts(MavenProject theProject, List<Dependency> dependencies, TransformableFilter filter) {
    try {
        Iterable<ArtifactResult> results = dependencyResolver.resolveDependencies(session.getProjectBuildingRequest(), theProject.getModel(), filter);
        Set<Artifact> resolvedArtifacts = new LinkedHashSet<Artifact>();
        for (ArtifactResult artResult : results) {
            resolvedArtifacts.add(artResult.getArtifact());
        }
        return resolvedArtifacts;
    } catch (DependencyResolverException e) {
        getLog().info("Unable to resolve all dependencies for: " + theProject.getGroupId() + ":" + theProject.getArtifactId() + ":" + theProject.getVersion() + ". Falling back to non-transitive mode for initial artifact resolution.");
    }
    Set<Artifact> resolvedArtifacts = new LinkedHashSet<Artifact>();
    ArtifactFilter artifactFilter = filter.transform(new ArtifactIncludeFilterTransformer());
    for (Dependency dependency : dependencies) {
        DefaultArtifactCoordinate coordinate = new DefaultArtifactCoordinate();
        coordinate.setGroupId(dependency.getGroupId());
        coordinate.setArtifactId(dependency.getArtifactId());
        coordinate.setVersion(dependency.getVersion());
        coordinate.setExtension(artifactHandlerManager.getArtifactHandler(dependency.getType()).getExtension());
        try {
            Artifact artifact = artifactResolver.resolveArtifact(session.getProjectBuildingRequest(), coordinate).getArtifact();
            if (artifactFilter.include(artifact)) {
                resolvedArtifacts.add(artifact);
            }
        } catch (ArtifactResolverException e) {
            getLog().debug("Unable to resolve artifact: " + coordinate);
        }
    }
    return resolvedArtifacts;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ArtifactFilter(org.apache.maven.artifact.resolver.filter.ArtifactFilter) ArtifactResolverException(org.apache.maven.shared.artifact.resolve.ArtifactResolverException) DefaultArtifactCoordinate(org.apache.maven.shared.artifact.DefaultArtifactCoordinate) ArtifactIncludeFilterTransformer(org.apache.maven.shared.artifact.filter.resolve.transform.ArtifactIncludeFilterTransformer) DependencyResolverException(org.apache.maven.shared.dependencies.resolve.DependencyResolverException) Dependency(org.apache.maven.model.Dependency) Artifact(org.apache.maven.artifact.Artifact) ArtifactResult(org.apache.maven.shared.artifact.resolve.ArtifactResult)

Example 2 with DependencyResolverException

use of org.apache.maven.shared.dependencies.resolve.DependencyResolverException in project maven-dependency-plugin by apache.

the class ResolvePluginsMojo method doExecute.

/**
 * Main entry into mojo. Gets the list of dependencies and iterates through displaying the resolved version.
 *
 * @throws MojoExecutionException with a message if an error occurs.
 */
@Override
protected void doExecute() throws MojoExecutionException {
    try {
        // ideally this should either be DependencyCoordinates or DependencyNode
        final Set<Artifact> plugins = resolvePluginArtifacts();
        StringBuilder sb = new StringBuilder();
        sb.append("\n");
        sb.append("The following plugins have been resolved:\n");
        if (plugins == null || plugins.isEmpty()) {
            sb.append("   none\n");
        } else {
            for (Artifact plugin : plugins) {
                String artifactFilename = null;
                if (outputAbsoluteArtifactFilename) {
                    try {
                        // we want to print the absolute file name here
                        artifactFilename = plugin.getFile().getAbsoluteFile().getPath();
                    } catch (NullPointerException e) {
                        // ignore the null pointer, we'll output a null string
                        artifactFilename = null;
                    }
                }
                String id = plugin.toString();
                sb.append("   " + id + (outputAbsoluteArtifactFilename ? ":" + artifactFilename : "") + "\n");
                if (!excludeTransitive) {
                    DefaultDependableCoordinate pluginCoordinate = new DefaultDependableCoordinate();
                    pluginCoordinate.setGroupId(plugin.getGroupId());
                    pluginCoordinate.setArtifactId(plugin.getArtifactId());
                    pluginCoordinate.setVersion(plugin.getVersion());
                    for (final Artifact artifact : resolveArtifactDependencies(pluginCoordinate)) {
                        artifactFilename = null;
                        if (outputAbsoluteArtifactFilename) {
                            try {
                                // we want to print the absolute file name here
                                artifactFilename = artifact.getFile().getAbsoluteFile().getPath();
                            } catch (NullPointerException e) {
                                // ignore the null pointer, we'll output a null string
                                artifactFilename = null;
                            }
                        }
                        id = artifact.toString();
                        sb.append("      " + id + (outputAbsoluteArtifactFilename ? ":" + artifactFilename : "") + "\n");
                    }
                }
            }
            sb.append("\n");
            String output = sb.toString();
            if (outputFile == null) {
                DependencyUtil.log(output, getLog());
            } else {
                DependencyUtil.write(output, outputFile, appendOutput, getLog());
            }
        }
    } catch (final IOException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    } catch (final ArtifactFilterException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    } catch (ArtifactResolverException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    } catch (DependencyResolverException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    }
}
Also used : ArtifactFilterException(org.apache.maven.shared.artifact.filter.collection.ArtifactFilterException) ArtifactResolverException(org.apache.maven.shared.artifact.resolve.ArtifactResolverException) DefaultDependableCoordinate(org.apache.maven.shared.dependencies.DefaultDependableCoordinate) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) DependencyResolverException(org.apache.maven.shared.dependencies.resolve.DependencyResolverException) IOException(java.io.IOException) Artifact(org.apache.maven.artifact.Artifact)

Example 3 with DependencyResolverException

use of org.apache.maven.shared.dependencies.resolve.DependencyResolverException in project maven-plugins by apache.

the class InstallMojo method installExtraArtifacts.

private void installExtraArtifacts(String[] extraArtifacts) throws MojoExecutionException {
    if (extraArtifacts == null) {
        return;
    }
    for (String extraArtifact : extraArtifacts) {
        String[] gav = extraArtifact.split(":");
        if (gav.length < 3 || gav.length > 5) {
            throw new MojoExecutionException("Invalid artifact " + extraArtifact);
        }
        String groupId = gav[0];
        String artifactId = gav[1];
        String version = gav[2];
        String type = "jar";
        if (gav.length > 3) {
            type = gav[3];
        }
        String classifier = null;
        if (gav.length == 5) {
            classifier = gav[4];
        }
        DefaultDependableCoordinate coordinate = new DefaultDependableCoordinate();
        try {
            coordinate.setGroupId(groupId);
            coordinate.setArtifactId(artifactId);
            coordinate.setVersion(version);
            coordinate.setType(type);
            coordinate.setClassifier(classifier);
            resolver.resolveDependencies(projectBuildingRequest, coordinate, null);
        } catch (DependencyResolverException e) {
            throw new MojoExecutionException("Unable to resolve dependencies for: " + coordinate, e);
        }
    }
}
Also used : DefaultDependableCoordinate(org.apache.maven.shared.dependencies.DefaultDependableCoordinate) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) DependencyResolverException(org.apache.maven.shared.dependencies.resolve.DependencyResolverException)

Example 4 with DependencyResolverException

use of org.apache.maven.shared.dependencies.resolve.DependencyResolverException in project maven-plugins by apache.

the class PurgeLocalRepositoryMojo method getFilteredResolvedArtifacts.

private Set<Artifact> getFilteredResolvedArtifacts(MavenProject project, List<Dependency> dependencies, TransformableFilter filter) {
    try {
        Iterable<ArtifactResult> results = dependencyResolver.resolveDependencies(session.getProjectBuildingRequest(), project.getModel(), filter);
        Set<Artifact> resolvedArtifacts = new LinkedHashSet<Artifact>();
        for (ArtifactResult artResult : results) {
            resolvedArtifacts.add(artResult.getArtifact());
        }
        return resolvedArtifacts;
    } catch (DependencyResolverException e) {
        getLog().info("Unable to resolve all dependencies for: " + project.getGroupId() + ":" + project.getArtifactId() + ":" + project.getVersion() + ". Falling back to non-transitive mode for initial artifact resolution.");
    }
    Set<Artifact> resolvedArtifacts = new LinkedHashSet<Artifact>();
    ArtifactFilter artifactFilter = filter.transform(new ArtifactIncludeFilterTransformer());
    for (Dependency dependency : dependencies) {
        DefaultArtifactCoordinate coordinate = new DefaultArtifactCoordinate();
        coordinate.setGroupId(dependency.getGroupId());
        coordinate.setArtifactId(dependency.getArtifactId());
        coordinate.setVersion(dependency.getVersion());
        coordinate.setExtension(artifactHandlerManager.getArtifactHandler(dependency.getType()).getExtension());
        try {
            Artifact artifact = artifactResolver.resolveArtifact(session.getProjectBuildingRequest(), coordinate).getArtifact();
            if (artifactFilter.include(artifact)) {
                resolvedArtifacts.add(artifact);
            }
        } catch (ArtifactResolverException e) {
            getLog().debug("Unable to resolve artifact: " + coordinate);
        }
    }
    return resolvedArtifacts;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ArtifactFilter(org.apache.maven.artifact.resolver.filter.ArtifactFilter) ArtifactResolverException(org.apache.maven.shared.artifact.resolve.ArtifactResolverException) DefaultArtifactCoordinate(org.apache.maven.shared.artifact.DefaultArtifactCoordinate) ArtifactIncludeFilterTransformer(org.apache.maven.shared.artifact.filter.resolve.transform.ArtifactIncludeFilterTransformer) DependencyResolverException(org.apache.maven.shared.dependencies.resolve.DependencyResolverException) Dependency(org.apache.maven.model.Dependency) Artifact(org.apache.maven.artifact.Artifact) ArtifactResult(org.apache.maven.shared.artifact.resolve.ArtifactResult)

Example 5 with DependencyResolverException

use of org.apache.maven.shared.dependencies.resolve.DependencyResolverException in project maven-plugins by apache.

the class AbstractJavadocMojo method getArtifactsAbsolutePath.

/**
 * Return the Javadoc artifact path and its transitive dependencies path from the local repository
 *
 * @param javadocArtifact not null
 * @return a list of locale artifacts absolute path
 * @throws MavenReportException if any
 */
private List<String> getArtifactsAbsolutePath(JavadocPathArtifact javadocArtifact) throws MavenReportException {
    if ((StringUtils.isEmpty(javadocArtifact.getGroupId())) && (StringUtils.isEmpty(javadocArtifact.getArtifactId())) && (StringUtils.isEmpty(javadocArtifact.getVersion()))) {
        return Collections.emptyList();
    }
    List<String> path = new ArrayList<>();
    try {
        Artifact artifact = createAndResolveArtifact(javadocArtifact);
        path.add(artifact.getFile().getAbsolutePath());
        DefaultDependableCoordinate coordinate = new DefaultDependableCoordinate();
        coordinate.setGroupId(javadocArtifact.getGroupId());
        coordinate.setArtifactId(javadocArtifact.getArtifactId());
        coordinate.setVersion(javadocArtifact.getVersion());
        Iterable<ArtifactResult> deps = dependencyResolver.resolveDependencies(session.getProjectBuildingRequest(), coordinate, ScopeFilter.including("compile", "provided"));
        for (ArtifactResult a : deps) {
            path.add(a.getArtifact().getFile().getAbsolutePath());
        }
        return path;
    } catch (ArtifactResolverException e) {
        throw new MavenReportException("Unable to resolve artifact:" + javadocArtifact, e);
    } catch (DependencyResolverException e) {
        throw new MavenReportException("Unable to resolve dependencies for:" + javadocArtifact, e);
    }
}
Also used : ArtifactResolverException(org.apache.maven.shared.artifact.resolve.ArtifactResolverException) DefaultDependableCoordinate(org.apache.maven.shared.dependencies.DefaultDependableCoordinate) ArrayList(java.util.ArrayList) DependencyResolverException(org.apache.maven.shared.dependencies.resolve.DependencyResolverException) BootclasspathArtifact(org.apache.maven.plugins.javadoc.options.BootclasspathArtifact) JavadocPathArtifact(org.apache.maven.plugins.javadoc.options.JavadocPathArtifact) ResourcesArtifact(org.apache.maven.plugins.javadoc.options.ResourcesArtifact) Artifact(org.apache.maven.artifact.Artifact) TagletArtifact(org.apache.maven.plugins.javadoc.options.TagletArtifact) DocletArtifact(org.apache.maven.plugins.javadoc.options.DocletArtifact) ArtifactResult(org.apache.maven.shared.artifact.resolve.ArtifactResult) MavenReportException(org.apache.maven.reporting.MavenReportException)

Aggregations

DependencyResolverException (org.apache.maven.shared.dependencies.resolve.DependencyResolverException)13 Artifact (org.apache.maven.artifact.Artifact)9 ArtifactResolverException (org.apache.maven.shared.artifact.resolve.ArtifactResolverException)9 ArtifactResult (org.apache.maven.shared.artifact.resolve.ArtifactResult)8 ArrayList (java.util.ArrayList)5 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)5 ProjectBuildingRequest (org.apache.maven.project.ProjectBuildingRequest)5 DefaultDependableCoordinate (org.apache.maven.shared.dependencies.DefaultDependableCoordinate)5 ArtifactFilter (org.apache.maven.artifact.resolver.filter.ArtifactFilter)4 Dependency (org.apache.maven.model.Dependency)4 IOException (java.io.IOException)3 LinkedHashSet (java.util.LinkedHashSet)3 DefaultProjectBuildingRequest (org.apache.maven.project.DefaultProjectBuildingRequest)3 MavenProject (org.apache.maven.project.MavenProject)3 MavenReportException (org.apache.maven.reporting.MavenReportException)3 File (java.io.File)2 ArtifactRepository (org.apache.maven.artifact.repository.ArtifactRepository)2 ArtifactRepositoryPolicy (org.apache.maven.artifact.repository.ArtifactRepositoryPolicy)2 MavenArtifactRepository (org.apache.maven.artifact.repository.MavenArtifactRepository)2 MojoFailureException (org.apache.maven.plugin.MojoFailureException)2