Search in sources :

Example 6 with DependencyNode

use of org.apache.maven.shared.dependency.graph.DependencyNode 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 7 with DependencyNode

use of org.apache.maven.shared.dependency.graph.DependencyNode in project maven-plugins by apache.

the class TestTreeMojo method _testTreeTestEnvironment.

/**
 * Tests the proper discovery and configuration of the mojo.
 *
 * @throws Exception in case of an error.
 */
public void _testTreeTestEnvironment() throws Exception {
    File testPom = new File(getBasedir(), "target/test-classes/unit/tree-test/plugin-config.xml");
    TreeMojo mojo = (TreeMojo) lookupMojo("tree", testPom);
    assertNotNull(mojo);
    assertNotNull(mojo.getProject());
    MavenProject project = mojo.getProject();
    project.setArtifact(this.stubFactory.createArtifact("testGroupId", "project", "1.0"));
    Set<Artifact> artifacts = this.stubFactory.getScopedArtifacts();
    Set<Artifact> directArtifacts = this.stubFactory.getReleaseAndSnapshotArtifacts();
    artifacts.addAll(directArtifacts);
    project.setArtifacts(artifacts);
    project.setDependencyArtifacts(directArtifacts);
    mojo.execute();
    DependencyNode rootNode = mojo.getDependencyGraph();
    assertNodeEquals("testGroupId:project:jar:1.0:compile", rootNode);
    assertEquals(2, rootNode.getChildren().size());
    assertChildNodeEquals("testGroupId:snapshot:jar:2.0-SNAPSHOT:compile", rootNode, 0);
    assertChildNodeEquals("testGroupId:release:jar:1.0:compile", rootNode, 1);
}
Also used : TreeMojo(org.apache.maven.plugins.dependency.tree.TreeMojo) MavenProject(org.apache.maven.project.MavenProject) DependencyNode(org.apache.maven.shared.dependency.graph.DependencyNode) File(java.io.File) Artifact(org.apache.maven.artifact.Artifact)

Example 8 with DependencyNode

use of org.apache.maven.shared.dependency.graph.DependencyNode in project felix by apache.

the class BundlePluginTest method testEmbedDependencyNegativeClauses.

public void testEmbedDependencyNegativeClauses() throws Exception {
    ArtifactStubFactory artifactFactory = new ArtifactStubFactory(plugin.getOutputDirectory(), true);
    Set artifacts = new LinkedHashSet();
    artifacts.addAll(artifactFactory.getClassifiedArtifacts());
    artifacts.addAll(artifactFactory.getScopedArtifacts());
    artifacts.addAll(artifactFactory.getTypedArtifacts());
    MavenProject project = getMavenProjectStub();
    project.setDependencyArtifacts(artifacts);
    Map instructions = new HashMap();
    instructions.put(DependencyEmbedder.EMBED_DEPENDENCY, "!type=jar, !artifactId=c");
    Properties props = new Properties();
    DependencyNode dependencyGraph = plugin.buildDependencyGraph(project);
    Builder builder = plugin.buildOSGiBundle(project, dependencyGraph, instructions, props, plugin.getClasspath(project, dependencyGraph));
    Manifest manifest = builder.getJar().getManifest();
    String bcp = manifest.getMainAttributes().getValue(Constants.BUNDLE_CLASSPATH);
    assertEquals(".," + "a-1.0.war," + "d-1.0.zip," + "e-1.0.rar", bcp);
    String eas = manifest.getMainAttributes().getValue("Embedded-Artifacts");
    assertEquals("a-1.0.war;g=\"g\";a=\"a\";v=\"1.0\"," + "d-1.0.zip;g=\"g\";a=\"d\";v=\"1.0\"," + "e-1.0.rar;g=\"g\";a=\"e\";v=\"1.0\"", eas);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) MavenProject(org.apache.maven.project.MavenProject) HashMap(java.util.HashMap) DependencyNode(org.apache.maven.shared.dependency.graph.DependencyNode) Builder(aQute.bnd.osgi.Builder) DependencyGraphBuilder(org.apache.maven.shared.dependency.graph.DependencyGraphBuilder) Properties(java.util.Properties) Manifest(java.util.jar.Manifest) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 9 with DependencyNode

use of org.apache.maven.shared.dependency.graph.DependencyNode in project felix by apache.

the class BundlePluginTest method testEmbedDependencyMissingNegativeKey.

public void testEmbedDependencyMissingNegativeKey() throws Exception {
    ArtifactStubFactory artifactFactory = new ArtifactStubFactory(plugin.getOutputDirectory(), true);
    Set artifacts = new LinkedHashSet();
    artifacts.addAll(artifactFactory.getClassifiedArtifacts());
    artifacts.addAll(artifactFactory.getScopedArtifacts());
    artifacts.addAll(artifactFactory.getTypedArtifacts());
    MavenProject project = getMavenProjectStub();
    project.setDependencyArtifacts(artifacts);
    Properties props = new Properties();
    DependencyNode dependencyGraph = plugin.buildDependencyGraph(project);
    Jar[] classpath = plugin.getClasspath(project, dependencyGraph);
    Map instructions1 = new HashMap();
    instructions1.put(DependencyEmbedder.EMBED_DEPENDENCY, "!scope=compile");
    Builder builder1 = plugin.buildOSGiBundle(project, dependencyGraph, instructions1, props, classpath);
    Manifest manifest1 = builder1.getJar().getManifest();
    Map instructions2 = new HashMap();
    instructions2.put(DependencyEmbedder.EMBED_DEPENDENCY, "scope=!compile");
    Builder builder2 = plugin.buildOSGiBundle(project, dependencyGraph, instructions2, props, classpath);
    Manifest manifest2 = builder2.getJar().getManifest();
    String bcp1 = manifest1.getMainAttributes().getValue(Constants.BUNDLE_CLASSPATH);
    assertEquals(".," + "provided-1.0.jar," + "test-1.0.jar," + "runtime-1.0.jar," + "system-1.0.jar", bcp1);
    String eas1 = manifest1.getMainAttributes().getValue("Embedded-Artifacts");
    assertEquals("provided-1.0.jar;g=\"g\";a=\"provided\";v=\"1.0\"," + "test-1.0.jar;g=\"g\";a=\"test\";v=\"1.0\"," + "runtime-1.0.jar;g=\"g\";a=\"runtime\";v=\"1.0\"," + "system-1.0.jar;g=\"g\";a=\"system\";v=\"1.0\"", eas1);
    String bcp2 = manifest2.getMainAttributes().getValue(Constants.BUNDLE_CLASSPATH);
    assertEquals(bcp1, bcp2);
    String eas2 = manifest2.getMainAttributes().getValue("Embedded-Artifacts");
    assertEquals(eas1, eas2);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) HashMap(java.util.HashMap) Builder(aQute.bnd.osgi.Builder) DependencyGraphBuilder(org.apache.maven.shared.dependency.graph.DependencyGraphBuilder) Properties(java.util.Properties) Manifest(java.util.jar.Manifest) MavenProject(org.apache.maven.project.MavenProject) DependencyNode(org.apache.maven.shared.dependency.graph.DependencyNode) Jar(aQute.bnd.osgi.Jar) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 10 with DependencyNode

use of org.apache.maven.shared.dependency.graph.DependencyNode in project felix by apache.

the class AbstractDependencyFilter method filteredDependencies.

private void filteredDependencies(final ArtifactFilter artifactFilter, Collection<Artifact> filteredDependencies) {
    CollectingDependencyNodeVisitor collectingDependencyNodeVisitor = new CollectingDependencyNodeVisitor();
    final Artifact rootArtifact = m_dependencyGraph.getArtifact();
    ArtifactFilter filter = new ArtifactFilter() {

        public boolean include(Artifact artifact) {
            return artifact.equals(rootArtifact) || artifactFilter.include(artifact);
        }
    };
    DependencyNodeFilter dependencyNodeFilter = new ArtifactDependencyNodeFilter(filter);
    dependencyNodeFilter = new TrimmingDependencyNodeFilter(dependencyNodeFilter);
    DependencyNodeVisitor dependencyNodeVisitor = new FilteringDependencyNodeVisitor(collectingDependencyNodeVisitor, dependencyNodeFilter);
    dependencyNodeVisitor = new BuildingDependencyNodeVisitor(dependencyNodeVisitor);
    m_dependencyGraph.accept(dependencyNodeVisitor);
    List<DependencyNode> dependencyNodes = collectingDependencyNodeVisitor.getNodes();
    Set<String> ids = new LinkedHashSet<String>(dependencyNodes.size());
    for (DependencyNode dependencyNode : dependencyNodes) {
        Artifact artifact = dependencyNode.getArtifact();
        String id = artifact.getId();
        ids.add(id);
    }
    for (Iterator<Artifact> iterator = filteredDependencies.iterator(); iterator.hasNext(); ) {
        Artifact artifact = iterator.next();
        String id = artifact.getId();
        if (!ids.contains(id)) {
            iterator.remove();
        }
    }
}
Also used : FilteringDependencyNodeVisitor(org.apache.maven.shared.dependency.graph.traversal.FilteringDependencyNodeVisitor) LinkedHashSet(java.util.LinkedHashSet) CollectingDependencyNodeVisitor(org.apache.maven.shared.dependency.graph.traversal.CollectingDependencyNodeVisitor) ArtifactDependencyNodeFilter(org.apache.maven.shared.dependency.graph.filter.ArtifactDependencyNodeFilter) ArtifactDependencyNodeFilter(org.apache.maven.shared.dependency.graph.filter.ArtifactDependencyNodeFilter) DependencyNodeFilter(org.apache.maven.shared.dependency.graph.filter.DependencyNodeFilter) Artifact(org.apache.maven.artifact.Artifact) FilteringDependencyNodeVisitor(org.apache.maven.shared.dependency.graph.traversal.FilteringDependencyNodeVisitor) DependencyNodeVisitor(org.apache.maven.shared.dependency.graph.traversal.DependencyNodeVisitor) BuildingDependencyNodeVisitor(org.apache.maven.shared.dependency.graph.traversal.BuildingDependencyNodeVisitor) CollectingDependencyNodeVisitor(org.apache.maven.shared.dependency.graph.traversal.CollectingDependencyNodeVisitor) AndArtifactFilter(org.apache.maven.artifact.resolver.filter.AndArtifactFilter) ArtifactFilter(org.apache.maven.artifact.resolver.filter.ArtifactFilter) BuildingDependencyNodeVisitor(org.apache.maven.shared.dependency.graph.traversal.BuildingDependencyNodeVisitor) DependencyNode(org.apache.maven.shared.dependency.graph.DependencyNode)

Aggregations

DependencyNode (org.apache.maven.shared.dependency.graph.DependencyNode)23 MavenProject (org.apache.maven.project.MavenProject)9 Artifact (org.apache.maven.artifact.Artifact)8 DependencyGraphBuilder (org.apache.maven.shared.dependency.graph.DependencyGraphBuilder)8 Builder (aQute.bnd.osgi.Builder)7 HashMap (java.util.HashMap)7 Properties (java.util.Properties)7 LinkedHashSet (java.util.LinkedHashSet)6 Map (java.util.Map)6 Manifest (java.util.jar.Manifest)6 File (java.io.File)5 Set (java.util.Set)5 TreeMap (java.util.TreeMap)5 ArrayList (java.util.ArrayList)4 ArtifactFilter (org.apache.maven.artifact.resolver.filter.ArtifactFilter)3 IOException (java.io.IOException)2 List (java.util.List)2 DefaultArtifact (org.apache.maven.artifact.DefaultArtifact)2 DefaultArtifactHandler (org.apache.maven.artifact.handler.DefaultArtifactHandler)2 AndArtifactFilter (org.apache.maven.artifact.resolver.filter.AndArtifactFilter)2