Search in sources :

Example 1 with ArtifactResolver

use of org.apache.maven.shared.artifact.resolve.ArtifactResolver in project component-runtime by Talend.

the class ArtifactTransformer method hasTransformedResource.

@Override
public boolean hasTransformedResource() {
    artifacts = new ArrayList<>();
    final ArtifactFilter filter = getFilter();
    if (userArtifacts != null && !userArtifacts.isEmpty()) {
        final ArtifactResolver resolver;
        final DependencyGraphBuilder graphBuilder;
        final ProjectBuilder projectBuilder;
        final PlexusContainer container = session.getContainer();
        try {
            resolver = ArtifactResolver.class.cast(container.lookup(ArtifactResolver.class, "default"));
            projectBuilder = ProjectBuilder.class.cast(container.lookup(ProjectBuilder.class, "default"));
            graphBuilder = includeTransitiveDependencies ? DependencyGraphBuilder.class.cast(container.lookup(DependencyGraphBuilder.class, "default")) : null;
        } catch (final ComponentLookupException e) {
            throw new IllegalArgumentException(e);
        }
        artifacts.addAll(userArtifacts.stream().flatMap(coords -> {
            try {
                final String type = ofNullable(coords.type).filter(s -> !s.isEmpty()).orElse("jar");
                final Artifact art = new DefaultArtifact(coords.groupId, coords.artifactId, coords.version, coords.scope, type, ofNullable(coords.classifier).orElse(""), new DefaultArtifactHandler() {

                    {
                        setExtension(type);
                    }
                });
                final Artifact artifact = resolver.resolveArtifact(session.getProjectBuildingRequest(), art).getArtifact();
                if (includeTransitiveDependencies) {
                    final DefaultProjectBuildingRequest request = new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
                    try {
                        final ProjectBuildingResult projectBuildingResult = projectBuilder.build(artifact, true, request);
                        final DependencyNode transitives = graphBuilder.buildDependencyGraph(projectBuildingResult.getProject(), filter);
                        final CollectingDependencyNodeVisitor visitor = new CollectingDependencyNodeVisitor();
                        transitives.accept(new FilteringDependencyNodeVisitor(visitor, new ArtifactDependencyNodeFilter(filter)));
                        return Stream.concat(Stream.of(artifact), visitor.getNodes().stream().map(DependencyNode::getArtifact).map(a -> {
                            try {
                                return resolver.resolveArtifact(request, a).getArtifact();
                            } catch (final ArtifactResolverException e) {
                                throw new IllegalStateException(e);
                            }
                        }));
                    } catch (final ProjectBuildingException | DependencyGraphBuilderException e) {
                        throw new IllegalStateException(e);
                    }
                }
                return Stream.of(artifact);
            } catch (final ArtifactResolverException e) {
                throw new IllegalArgumentException(e);
            }
        }).collect(toSet()));
    }
    if (includeProjectComponentDependencies) {
        // this is a weird category of projects but can exist
        final MavenProject project = session.getCurrentProject();
        if (project.getArtifacts() != null && !project.getArtifacts().isEmpty()) {
            project.setArtifactFilter(filter);
            try {
                artifacts.addAll(project.getArtifacts());
            } finally {
                // shade plugin uses it OOTB so reset it for the end of the execution (in case
                // another transformer needs it)
                project.setArtifactFilter(new CumulativeScopeArtifactFilter(singletonList("runtime")));
            }
        }
    }
    return !artifacts.isEmpty();
}
Also used : CumulativeScopeArtifactFilter(org.apache.maven.artifact.resolver.filter.CumulativeScopeArtifactFilter) Setter(lombok.Setter) FilteringDependencyNodeVisitor(org.apache.maven.shared.dependency.graph.traversal.FilteringDependencyNodeVisitor) ResourceTransformer(org.apache.maven.plugins.shade.resource.ResourceTransformer) ComponentLookupException(org.codehaus.plexus.component.repository.exception.ComponentLookupException) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) AndArtifactFilter(org.apache.maven.artifact.resolver.filter.AndArtifactFilter) ProjectBuildingException(org.apache.maven.project.ProjectBuildingException) IncludesArtifactFilter(org.apache.maven.artifact.resolver.filter.IncludesArtifactFilter) ArtifactFilter(org.apache.maven.artifact.resolver.filter.ArtifactFilter) MavenProject(org.apache.maven.project.MavenProject) DependencyGraphBuilderException(org.apache.maven.shared.dependency.graph.DependencyGraphBuilderException) Artifact(org.apache.maven.artifact.Artifact) DependencyGraphBuilder(org.apache.maven.shared.dependency.graph.DependencyGraphBuilder) CollectingDependencyNodeVisitor(org.apache.maven.shared.dependency.graph.traversal.CollectingDependencyNodeVisitor) Collectors.toSet(java.util.stream.Collectors.toSet) DefaultArtifact(org.apache.maven.artifact.DefaultArtifact) DefaultArtifactHandler(org.apache.maven.artifact.handler.DefaultArtifactHandler) ExcludesArtifactFilter(org.apache.maven.artifact.resolver.filter.ExcludesArtifactFilter) MavenSession(org.apache.maven.execution.MavenSession) ArtifactResolver(org.apache.maven.shared.artifact.resolve.ArtifactResolver) ArtifactResolverException(org.apache.maven.shared.artifact.resolve.ArtifactResolverException) Optional.ofNullable(java.util.Optional.ofNullable) NONE(lombok.AccessLevel.NONE) Relocator(org.apache.maven.plugins.shade.relocation.Relocator) IOException(java.io.IOException) ArtifactDependencyNodeFilter(org.apache.maven.shared.dependency.graph.filter.ArtifactDependencyNodeFilter) PlexusContainer(org.codehaus.plexus.PlexusContainer) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) DependencyNode(org.apache.maven.shared.dependency.graph.DependencyNode) Stream(java.util.stream.Stream) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest) Data(lombok.Data) ProjectBuilder(org.apache.maven.project.ProjectBuilder) ScopeArtifactFilter(org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter) ProjectBuildingResult(org.apache.maven.project.ProjectBuildingResult) InputStream(java.io.InputStream) FilteringDependencyNodeVisitor(org.apache.maven.shared.dependency.graph.traversal.FilteringDependencyNodeVisitor) CollectingDependencyNodeVisitor(org.apache.maven.shared.dependency.graph.traversal.CollectingDependencyNodeVisitor) ArtifactResolverException(org.apache.maven.shared.artifact.resolve.ArtifactResolverException) PlexusContainer(org.codehaus.plexus.PlexusContainer) ComponentLookupException(org.codehaus.plexus.component.repository.exception.ComponentLookupException) ArtifactResolver(org.apache.maven.shared.artifact.resolve.ArtifactResolver) CumulativeScopeArtifactFilter(org.apache.maven.artifact.resolver.filter.CumulativeScopeArtifactFilter) MavenProject(org.apache.maven.project.MavenProject) DependencyNode(org.apache.maven.shared.dependency.graph.DependencyNode) ProjectBuildingException(org.apache.maven.project.ProjectBuildingException) ArtifactDependencyNodeFilter(org.apache.maven.shared.dependency.graph.filter.ArtifactDependencyNodeFilter) DependencyGraphBuilderException(org.apache.maven.shared.dependency.graph.DependencyGraphBuilderException) Artifact(org.apache.maven.artifact.Artifact) DefaultArtifact(org.apache.maven.artifact.DefaultArtifact) CumulativeScopeArtifactFilter(org.apache.maven.artifact.resolver.filter.CumulativeScopeArtifactFilter) AndArtifactFilter(org.apache.maven.artifact.resolver.filter.AndArtifactFilter) IncludesArtifactFilter(org.apache.maven.artifact.resolver.filter.IncludesArtifactFilter) ArtifactFilter(org.apache.maven.artifact.resolver.filter.ArtifactFilter) ExcludesArtifactFilter(org.apache.maven.artifact.resolver.filter.ExcludesArtifactFilter) ScopeArtifactFilter(org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter) ProjectBuilder(org.apache.maven.project.ProjectBuilder) DefaultArtifactHandler(org.apache.maven.artifact.handler.DefaultArtifactHandler) DependencyGraphBuilder(org.apache.maven.shared.dependency.graph.DependencyGraphBuilder) ProjectBuildingResult(org.apache.maven.project.ProjectBuildingResult) DefaultArtifact(org.apache.maven.artifact.DefaultArtifact) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest)

Example 2 with ArtifactResolver

use of org.apache.maven.shared.artifact.resolve.ArtifactResolver 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)

Aggregations

ArrayList (java.util.ArrayList)2 List (java.util.List)2 Artifact (org.apache.maven.artifact.Artifact)2 DefaultArtifact (org.apache.maven.artifact.DefaultArtifact)2 MavenSession (org.apache.maven.execution.MavenSession)2 MavenProject (org.apache.maven.project.MavenProject)2 ArtifactResolver (org.apache.maven.shared.artifact.resolve.ArtifactResolver)2 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 Collections.singletonList (java.util.Collections.singletonList)1 Optional.ofNullable (java.util.Optional.ofNullable)1 Collectors.toList (java.util.stream.Collectors.toList)1 Collectors.toSet (java.util.stream.Collectors.toSet)1 Stream (java.util.stream.Stream)1 NONE (lombok.AccessLevel.NONE)1 Data (lombok.Data)1 Setter (lombok.Setter)1