Search in sources :

Example 1 with ArtifactResult

use of org.apache.maven.shared.artifact.resolve.ArtifactResult in project maven-plugins by apache.

the class AbstractResolveMojo method resolveArtifactDependencies.

/**
     * This method resolves all transitive dependencies of an artifact.
     *
     * @param artifact the artifact used to retrieve dependencies
     * @return resolved set of dependencies
     * @throws DependencyResolverException
     */
protected Set<Artifact> resolveArtifactDependencies(final DependableCoordinate artifact) throws DependencyResolverException {
    ProjectBuildingRequest buildingRequest = newResolveArtifactProjectBuildingRequest();
    Iterable<ArtifactResult> artifactResults = getDependencyResolver().resolveDependencies(buildingRequest, artifact, null);
    Set<Artifact> artifacts = new LinkedHashSet<Artifact>();
    for (final ArtifactResult artifactResult : artifactResults) {
        artifacts.add(artifactResult.getArtifact());
    }
    return artifacts;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) Artifact(org.apache.maven.artifact.Artifact) ArtifactResult(org.apache.maven.shared.artifact.resolve.ArtifactResult)

Example 2 with ArtifactResult

use of org.apache.maven.shared.artifact.resolve.ArtifactResult in project maven-plugins by apache.

the class ShadeMojoTest method testShadeWithFilter.

/**
     * Tests if a Filter is installed correctly, also if createSourcesJar is set to true.
     *
     * @throws Exception
     */
public void testShadeWithFilter() throws Exception {
    ShadeMojo mojo = new ShadeMojo();
    // set createSourcesJar = true
    Field createSourcesJar = ShadeMojo.class.getDeclaredField("createSourcesJar");
    createSourcesJar.setAccessible(true);
    createSourcesJar.set(mojo, Boolean.TRUE);
    // configure artifactResolver (mocked) for mojo
    ArtifactResolver mockArtifactResolver = new ArtifactResolver() {

        @Override
        public ArtifactResult resolveArtifact(ProjectBuildingRequest req, final Artifact art) throws ArtifactResolverException {
            return new ArtifactResult() {

                @Override
                public Artifact getArtifact() {
                    art.setResolved(true);
                    String fileName = art.getArtifactId() + "-" + art.getVersion() + (art.getClassifier() != null ? "-" + art.getClassifier() : "") + ".jar";
                    art.setFile(new File(fileName));
                    return art;
                }
            };
        }

        @Override
        public ArtifactResult resolveArtifact(ProjectBuildingRequest req, final ArtifactCoordinate coordinate) throws ArtifactResolverException {
            return new ArtifactResult() {

                @Override
                public Artifact getArtifact() {
                    Artifact art = mock(Artifact.class);
                    when(art.getGroupId()).thenReturn(coordinate.getGroupId());
                    when(art.getArtifactId()).thenReturn(coordinate.getArtifactId());
                    when(art.getType()).thenReturn(coordinate.getExtension());
                    when(art.getClassifier()).thenReturn(coordinate.getClassifier());
                    when(art.isResolved()).thenReturn(true);
                    String fileName = coordinate.getArtifactId() + "-" + coordinate.getVersion() + (coordinate.getClassifier() != null ? "-" + coordinate.getClassifier() : "") + ".jar";
                    when(art.getFile()).thenReturn(new File(fileName));
                    return art;
                }
            };
        }
    };
    Field artifactResolverField = ShadeMojo.class.getDeclaredField("artifactResolver");
    artifactResolverField.setAccessible(true);
    artifactResolverField.set(mojo, mockArtifactResolver);
    // create and configure MavenProject
    MavenProject project = new MavenProject();
    ArtifactHandler artifactHandler = (ArtifactHandler) lookup(ArtifactHandler.ROLE);
    Artifact artifact = new DefaultArtifact("org.apache.myfaces.core", "myfaces-impl", VersionRange.createFromVersion("2.0.1-SNAPSHOT"), "compile", "jar", null, artifactHandler);
    // setFile and setResolved
    artifact = mockArtifactResolver.resolveArtifact(null, artifact).getArtifact();
    project.setArtifact(artifact);
    Field projectField = ShadeMojo.class.getDeclaredField("project");
    projectField.setAccessible(true);
    projectField.set(mojo, project);
    // create and configure the ArchiveFilter
    ArchiveFilter archiveFilter = new ArchiveFilter();
    Field archiveFilterArtifact = ArchiveFilter.class.getDeclaredField("artifact");
    archiveFilterArtifact.setAccessible(true);
    archiveFilterArtifact.set(archiveFilter, "org.apache.myfaces.core:myfaces-impl");
    // add ArchiveFilter to mojo
    Field filtersField = ShadeMojo.class.getDeclaredField("filters");
    filtersField.setAccessible(true);
    filtersField.set(mojo, new ArchiveFilter[] { archiveFilter });
    Field sessionField = ShadeMojo.class.getDeclaredField("session");
    sessionField.setAccessible(true);
    sessionField.set(mojo, mock(MavenSession.class));
    // invoke getFilters()
    Method getFilters = ShadeMojo.class.getDeclaredMethod("getFilters", new Class[0]);
    getFilters.setAccessible(true);
    List<Filter> filters = (List<Filter>) getFilters.invoke(mojo);
    // assertions - there must be one filter
    assertEquals(1, filters.size());
    // the filter must be able to filter the binary and the sources jar
    Filter filter = filters.get(0);
    // binary jar
    assertTrue(filter.canFilter(new File("myfaces-impl-2.0.1-SNAPSHOT.jar")));
    // sources jar
    assertTrue(filter.canFilter(new File("myfaces-impl-2.0.1-SNAPSHOT-sources.jar")));
}
Also used : ArtifactCoordinate(org.apache.maven.shared.artifact.ArtifactCoordinate) Method(java.lang.reflect.Method) ArtifactResolver(org.apache.maven.shared.artifact.resolve.ArtifactResolver) Artifact(org.apache.maven.artifact.Artifact) DefaultArtifact(org.apache.maven.artifact.DefaultArtifact) ArtifactResult(org.apache.maven.shared.artifact.resolve.ArtifactResult) Field(java.lang.reflect.Field) ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) MavenSession(org.apache.maven.execution.MavenSession) ArtifactHandler(org.apache.maven.artifact.handler.ArtifactHandler) MavenProject(org.apache.maven.project.MavenProject) Filter(org.apache.maven.plugins.shade.filter.Filter) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) DefaultArtifact(org.apache.maven.artifact.DefaultArtifact)

Example 3 with ArtifactResult

use of org.apache.maven.shared.artifact.resolve.ArtifactResult 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<String>();
    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) JavadocPathArtifact(org.apache.maven.plugin.javadoc.options.JavadocPathArtifact) Artifact(org.apache.maven.artifact.Artifact) DocletArtifact(org.apache.maven.plugin.javadoc.options.DocletArtifact) BootclasspathArtifact(org.apache.maven.plugin.javadoc.options.BootclasspathArtifact) ResourcesArtifact(org.apache.maven.plugin.javadoc.options.ResourcesArtifact) TagletArtifact(org.apache.maven.plugin.javadoc.options.TagletArtifact) ArtifactResult(org.apache.maven.shared.artifact.resolve.ArtifactResult) MavenReportException(org.apache.maven.reporting.MavenReportException)

Example 4 with ArtifactResult

use of org.apache.maven.shared.artifact.resolve.ArtifactResult in project maven-plugins by apache.

the class PurgeLocalRepositoryMojo method getFilteredResolvedArtifacts.

private Set<Artifact> getFilteredResolvedArtifacts(MavenProject project, Set<Artifact> artifacts, 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());
    // purge anyway
    for (Artifact artifact : artifacts) {
        if (artifactFilter.include(artifact)) {
            try {
                ArtifactResult artifactResult = artifactResolver.resolveArtifact(session.getProjectBuildingRequest(), artifact);
                resolvedArtifacts.add(artifactResult.getArtifact());
            } catch (ArtifactResolverException e) {
                getLog().debug("Unable to resolve artifact: " + artifact);
            }
        }
    }
    return resolvedArtifacts;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ArtifactFilter(org.apache.maven.artifact.resolver.filter.ArtifactFilter) ArtifactResolverException(org.apache.maven.shared.artifact.resolve.ArtifactResolverException) ArtifactIncludeFilterTransformer(org.apache.maven.shared.artifact.filter.resolve.transform.ArtifactIncludeFilterTransformer) DependencyResolverException(org.apache.maven.shared.dependencies.resolve.DependencyResolverException) Artifact(org.apache.maven.artifact.Artifact) ArtifactResult(org.apache.maven.shared.artifact.resolve.ArtifactResult)

Example 5 with ArtifactResult

use of org.apache.maven.shared.artifact.resolve.ArtifactResult in project maven-plugins by apache.

the class DefaultRepositoryAssembler method assembleRepositoryArtifacts.

private void assembleRepositoryArtifacts(ProjectBuildingRequest buildingRequest, Iterable<ArtifactResult> result, ArtifactFilter filter, Map<String, GroupVersionAlignment> groupVersionAlignments) throws RepositoryAssemblyException {
    try {
        for (ArtifactResult ar : result) {
            Artifact a = ar.getArtifact();
            if (filter.include(a)) {
                getLogger().debug("Re-resolving: " + a + " for repository assembly.");
                setAlignment(a, groupVersionAlignments);
                artifactResolver.resolveArtifact(buildingRequest, TransferUtils.toArtifactCoordinate(a));
                a.setVersion(a.getBaseVersion());
                File targetFile = new File(repositoryManager.getLocalRepositoryBasedir(buildingRequest), repositoryManager.getPathForLocalArtifact(buildingRequest, a));
                FileUtils.copyFile(a.getFile(), targetFile);
            //                    writeChecksums( targetFile );
            }
        }
    } catch (ArtifactResolverException e) {
        throw new RepositoryAssemblyException("Error resolving artifacts: " + e.getMessage(), e);
    } catch (IOException e) {
        throw new RepositoryAssemblyException("Error writing artifact metdata.", e);
    }
}
Also used : ArtifactResolverException(org.apache.maven.shared.artifact.resolve.ArtifactResolverException) IOException(java.io.IOException) File(java.io.File) Artifact(org.apache.maven.artifact.Artifact) ArtifactResult(org.apache.maven.shared.artifact.resolve.ArtifactResult)

Aggregations

ArtifactResult (org.apache.maven.shared.artifact.resolve.ArtifactResult)7 Artifact (org.apache.maven.artifact.Artifact)6 DependencyResolverException (org.apache.maven.shared.dependencies.resolve.DependencyResolverException)4 File (java.io.File)3 MavenProject (org.apache.maven.project.MavenProject)3 ProjectBuildingRequest (org.apache.maven.project.ProjectBuildingRequest)3 ArtifactResolverException (org.apache.maven.shared.artifact.resolve.ArtifactResolverException)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 LinkedHashSet (java.util.LinkedHashSet)2 ArtifactFilter (org.apache.maven.artifact.resolver.filter.ArtifactFilter)2 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 List (java.util.List)1 DefaultArtifact (org.apache.maven.artifact.DefaultArtifact)1 ArtifactHandler (org.apache.maven.artifact.handler.ArtifactHandler)1 AndArtifactFilter (org.apache.maven.artifact.resolver.filter.AndArtifactFilter)1 MavenSession (org.apache.maven.execution.MavenSession)1 Dependency (org.apache.maven.model.Dependency)1 BootclasspathArtifact (org.apache.maven.plugin.javadoc.options.BootclasspathArtifact)1