org.apache.maven.shared.dependency.graph.DependencyGraphBuilder Java Examples

The following examples show how to use org.apache.maven.shared.dependency.graph.DependencyGraphBuilder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DependencyCopy.java    From wisdom with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the list of artifact to consider during the analysis.
 *
 * @param mojo       the mojo
 * @param graph      the dependency graph builder
 * @param transitive do we have to include transitive dependencies
 * @return the set of artifacts
 */
public static Set<Artifact> getArtifactsToConsider(AbstractWisdomMojo mojo, DependencyGraphBuilder graph,
                                                   boolean transitive, ArtifactFilter filter) {
    // No transitive.
    Set<Artifact> artifacts;
    if (!transitive) {
        // Direct dependencies that the current project has (no transitives)
        artifacts = mojo.project.getDependencyArtifacts();
    } else {
        // All dependencies that the current project has, including transitive ones. Contents are lazily
        // populated, so depending on what phases have run dependencies in some scopes won't be
        // included.
        artifacts = getTransitiveDependencies(mojo, graph, filter);
    }
    return artifacts;
}
 
Example #2
Source File: DependencyCopy.java    From wisdom with Apache License 2.0 5 votes vote down vote up
/**
 * Collects the transitive dependencies of the current projects.
 *
 * @param mojo  the mojo
 * @param graph the dependency graph builder
 * @return the set of resolved transitive dependencies.
 */
private static Set<Artifact> getTransitiveDependencies(AbstractWisdomMojo mojo, DependencyGraphBuilder graph,
                                                       ArtifactFilter filter) {
    Set<Artifact> artifacts;
    artifacts = new LinkedHashSet<>();
    try {
        Set<Artifact> transitives = new LinkedHashSet<>();
        DependencyNode node = graph.buildDependencyGraph(mojo.project, filter);
        node.accept(new ArtifactVisitor(mojo, transitives));
        mojo.getLog().debug(transitives.size() + " transitive dependencies have been collected : " +
                transitives);

        // Unfortunately, the retrieved artifacts are not resolved, we need to find their 'surrogates' in the
        // resolved list.
        Set<Artifact> resolved = mojo.project.getArtifacts();
        for (Artifact a : transitives) {
            Artifact r = getArtifact(a, resolved);
            if (r == null) {
                mojo.getLog().warn("Cannot find resolved artifact for " + a);
            } else {
                artifacts.add(r);
            }
        }
    } catch (DependencyGraphBuilderException e) {
        mojo.getLog().error("Cannot traverse the project's dependencies to collect transitive dependencies, " +
                "ignoring transitive");
        mojo.getLog().debug("Here is the thrown exception having disabled the transitive dependency collection", e);
        artifacts = mojo.project.getDependencyArtifacts();
    }
    return artifacts;
}
 
Example #3
Source File: DependencyCopy.java    From wisdom with Apache License 2.0 4 votes vote down vote up
/**
 * Copy direct (non-transitive) dependencies that are <strong>not bundles</strong> to the {@literal libs} directory
 * of the Wisdom server. As using such kind of dependencies does not really embrace the modular way promoted by
 * Wisdom, the copy is "explicit" meaning that the dependencies must be declared in the project that is going to
 * be run.
 * <p>
 * Only direct dependencies from the scope {@code compile} are copied.
 *
 * @param mojo  the mojo
 * @param graph the dependency graph builder
 * @return the list of artifact copied to the 'libs' directory. Empty is none.
 * @throws IOException when a file cannot be copied
 */
public static Set<Artifact> copyLibs(AbstractWisdomMojo mojo, DependencyGraphBuilder graph, Libraries libraries)
        throws IOException {

    if (libraries == null || !libraries.hasLibraries()) {
        return Collections.emptySet();
    }

    File libsDirectory = new File(mojo.getWisdomRootDirectory(), "libs");
    ArtifactFilter filter = libraries.getFilter();

    Set<Artifact> artifacts = getArtifactsToConsider(mojo, graph, true, null);

    for (final Iterator<Artifact> it = artifacts.iterator(); it.hasNext(); ) {
        final Artifact artifact = it.next();

        if (!filter.include(artifact)) {
            it.remove();
            if (mojo.getLog().isDebugEnabled()) {
                mojo.getLog().debug(artifact.getId() + " was removed by filters.");
            }
            continue;
        }

        if (BundleExclusions.isExcluded(artifact)) {
            it.remove();
            mojo.getLog().info("Dependency " + artifact + " not copied - the artifact is on the exclusion list");
            continue;
        }

        // We still have to do this test, as when using the direct dependencies we may include test and provided
        // dependencies.
        if (SCOPE_COMPILE.equalsIgnoreCase(artifact.getScope())) {
            File file = artifact.getFile();
            if (file != null && file.isFile()) {
                mojo.getLog().warn("Copying " + file.getName() + " to the libs directory");
                FileUtils.copyFileToDirectory(file, libsDirectory);
            } else {
                mojo.getLog().warn("Cannot copy the file associated with " + artifact.getArtifactId() + " - the " +
                        "file is missing");
            }
        } else {
            it.remove();
        }
    }

    return artifacts;
}
 
Example #4
Source File: WebJars.java    From wisdom with Apache License 2.0 4 votes vote down vote up
/**
 * Manage webjars dependencies.
 * <p>
 * This process is executed as follows:
 * <ol>
 * <li>web jars that are also bundles are ignored</li>
 * <li>web jars libraries from a 'provided' dependency (in the 'provided' scope) are copied to the /assets/lib
 * directory.</li>
 * <li>web jars libraries from a 'compile' dependency (in the 'compile' scope) are copied to the application
 * directory.</li>
 * <li>Transitive are also analyzed if enabled (enabled by default).</li>
 * </ol>
 *
 * @param mojo       the mojo
 * @param graph      the dependency graph builder
 * @param transitive whether or not we include the transitive dependencies.
 * @param unpackWebJars whether or not webjars should be extracted to target/webjars
 * @throws java.io.IOException when a web jar cannot be handled correctly
 */
public static void manageWebJars(AbstractWisdomMojo mojo, DependencyGraphBuilder graph,
                                 boolean transitive, boolean unpackWebJars) throws IOException {
    File webjars = new File(mojo.getWisdomRootDirectory(), "assets/libs");
    final File application = new File(mojo.getWisdomRootDirectory(), "application");

    Set<Artifact> artifacts = DependencyCopy.getArtifactsToConsider(mojo, graph, transitive, null);


    for (Artifact artifact : artifacts) {
        if (DependencyCopy.SCOPE_COMPILE.equalsIgnoreCase(artifact.getScope())
                || DependencyCopy.SCOPE_PROVIDED.equalsIgnoreCase(artifact.getScope())) {
            File file = artifact.getFile();

            // Check it's a 'jar file'
            if (!file.getName().endsWith(".jar")) {
                mojo.getLog().debug("Dependency " + file.getName() + " is not a web jar, it's not even a jar file");
                continue;
            }

            // Check that it's a web jar.
            if (!isWebJar(file)) {
                mojo.getLog().debug("Dependency " + file.getName() + " is not a web jar.");
                continue;
            }

            // Check that it's not a bundle.
            if (isBundle(file)) {
                mojo.getLog().debug("Dependency " + file.getName() + " is a web jar, but it's also a bundle, " +
                        "to ignore it.");
                continue;
            }

            // It's a web jar.
            if (DependencyCopy.SCOPE_COMPILE.equalsIgnoreCase(artifact.getScope())) {
                mojo.getLog().info("Copying web jar library " + file.getName() + " to the application directory");
                FileUtils.copyFileToDirectory(file, application);

                // Check whether or not it must be unpacked to target/webjars.
                if (unpackWebJars) {
                    extract(mojo, file, new File(mojo.buildDirectory, "webjars"), true);
                }
                // NOTE: webjars from the 'provided' scope are not unpacked in target/webjars as they are in
                // target/wisdom/assets/libs.
            } else {
                mojo.getLog().info("Extracting web jar libraries from " + file.getName() + " to " + webjars
                        .getAbsolutePath());
                extract(mojo, file, webjars, false);
            }
        }
    }
}
 
Example #5
Source File: DependencyCopyTest.java    From wisdom with Apache License 2.0 4 votes vote down vote up
@Test
public void testCopyLibs() throws Exception {
    AbstractWisdomMojo mojo = new AbstractWisdomMojo() {
        @Override
        public void execute() throws MojoExecutionException, MojoFailureException {
            // Do nothing;
        }
    };
    mojo.basedir = new File("target/junk/project1");
    mojo.basedir.mkdirs();
    mojo.project = new MavenProject();
    mojo.project.setArtifacts(resolved);
    mojo.buildDirectory = new File(mojo.basedir, "target");

    // Create the artifacts
    final Artifact asmArtifact = artifact("asm");
    final Artifact parboiledCoreArtifact = artifact("parboiledCore");
    final Artifact parboiledArtifact = artifact("parboiled");
    final Artifact pegdownArtifact = artifact("pegdown");
    final Artifact projectArtifact = artifact("project");

    // Define the trails
    setTrail(projectArtifact, projectArtifact);
    setTrail(pegdownArtifact, projectArtifact, pegdownArtifact);
    setTrail(parboiledArtifact, projectArtifact, pegdownArtifact, parboiledArtifact);
    setTrail(parboiledCoreArtifact, projectArtifact, pegdownArtifact, parboiledArtifact, parboiledCoreArtifact);
    setTrail(asmArtifact, projectArtifact, pegdownArtifact, asmArtifact);

    DefaultDependencyNode root = new DefaultDependencyNode(null, projectArtifact, null, null, null);
    DefaultDependencyNode pegdown = new DefaultDependencyNode(root, pegdownArtifact, null, null, null);
    DefaultDependencyNode parboiled = new DefaultDependencyNode(pegdown, parboiledArtifact, null, null, null);
    DefaultDependencyNode parboiledCore = new DefaultDependencyNode(parboiled, parboiledCoreArtifact, null,
            null, null);
    DefaultDependencyNode asm = new DefaultDependencyNode(parboiled, asmArtifact, null, null, null);

    root.setChildren(ImmutableList.<DependencyNode>of(pegdown));
    pegdown.setChildren(ImmutableList.<DependencyNode>of(parboiled));
    parboiled.setChildren(ImmutableList.<DependencyNode>of(parboiledCore, asm));
    parboiledCore.setChildren(Collections.<DependencyNode>emptyList());
    asm.setChildren(Collections.<DependencyNode>emptyList());

    DependencyGraphBuilder builder = mock(DependencyGraphBuilder.class);
    when(builder.buildDependencyGraph(mojo.project, null)).thenReturn(root);

    // First execution, with transitive enabled.
    Libraries libraries = new Libraries();
    libraries.setResolveTransitive(true);
    libraries.setIncludes(ImmutableList.of(":pegdown"));
    Set<Artifact> copied = DependencyCopy.copyLibs(mojo, builder, libraries);

    // In that case, everything is copied, so size == 4
    assertThat(copied).hasSize(4);

    // First execution, without transitives.
    libraries.setResolveTransitive(false);
    libraries.setIncludes(ImmutableList.of(":pegdown"));
    copied = DependencyCopy.copyLibs(mojo, builder, libraries);

    // In that case, only pegdown is copied, so size == 1
    assertThat(copied).hasSize(1);

    // Re-enabled the transitive and exclude asm
    libraries.setResolveTransitive(true);
    libraries.setIncludes(ImmutableList.of(":pegdown"));
    libraries.setExcludes(ImmutableList.of(":asm"));
    copied = DependencyCopy.copyLibs(mojo, builder, libraries);

    // In that case, asm is not copied, size = 3
    assertThat(copied).hasSize(3);

    // Ensure that excluded dependencies are not copied, for this we modify the graph adding a dependency on org
    // .apache.felix.ipojo.annotations.
    Artifact annotation = artifact("org.apache.felix.ipojo.annotations");
    when(annotation.getGroupId()).thenReturn("org.apache.felix");
    setTrail(annotation, projectArtifact, pegdownArtifact);
    DefaultDependencyNode ann = new DefaultDependencyNode(parboiled, annotation, null, null, null);
    ann.setChildren(Collections.<DependencyNode>emptyList());
    pegdown.setChildren(ImmutableList.<DependencyNode>of(parboiled, ann));

    // Re-enabled the transitive and exclude asm
    libraries.setResolveTransitive(true);
    libraries.setIncludes(ImmutableList.of(":pegdown"));
    libraries.setExcludes(ImmutableList.of(":asm"));
    copied = DependencyCopy.copyLibs(mojo, builder, libraries);

    // In that case, asm is not copied, annotation either, size = 3
    assertThat(copied).hasSize(3);

}