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

The following examples show how to use org.apache.maven.shared.dependency.graph.DependencyGraphBuilderException. 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: BotsingMojo.java    From botsing with Apache License 2.0 6 votes vote down vote up
public List<Artifact> getDependencyTree(DependencyInputType dependencyType) throws MojoExecutionException {
	try {
		ProjectBuildingRequest buildingRequest = getProjectbuildingRequest(dependencyType);

		// TODO check if it is necessary to specify an artifact filter
		DependencyNode rootNode = dependencyGraphBuilder.buildDependencyGraph(buildingRequest, null,
				reactorProjects);

		List<Artifact> artifactList = new ArrayList<Artifact>();
		addChildDependencies(rootNode, artifactList);

		return artifactList;

	} catch (DependencyGraphBuilderException e) {
		throw new MojoExecutionException("Couldn't download artifact: " + e.getMessage(), e);
	}
}
 
Example #2
Source File: BaseCycloneDxMojo.java    From cyclonedx-maven-plugin with Apache License 2.0 6 votes vote down vote up
protected Set<Dependency> buildDependencyGraph(final Set<String> componentRefs) throws MojoExecutionException {
    final Set<Dependency> dependencies = new LinkedHashSet<>();
    final Collection<String> scope = new HashSet<>();
    if (includeCompileScope) scope.add("compile");
    if (includeProvidedScope) scope.add("provided");
    if (includeRuntimeScope) scope.add("runtime");
    if (includeSystemScope) scope.add("system");
    if (includeTestScope) scope.add("test");
    final ArtifactFilter artifactFilter = new CumulativeScopeArtifactFilter(scope);
    final ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
    buildingRequest.setProject(this.project);
    try {
        final DependencyNode rootNode = dependencyGraphBuilder.buildDependencyGraph(buildingRequest, artifactFilter);
        buildDependencyGraphNode(componentRefs, dependencies, rootNode, null);
        final CollectingDependencyNodeVisitor visitor = new CollectingDependencyNodeVisitor();
        rootNode.accept(visitor);
        for (final DependencyNode dependencyNode : visitor.getNodes()) {
            buildDependencyGraphNode(componentRefs, dependencies, dependencyNode, null);
        }
    } catch (DependencyGraphBuilderException e) {
        throw new MojoExecutionException("An error occurred building dependency graph", e);
    }
    return dependencies;
}
 
Example #3
Source File: BanCircularDependencies.java    From extra-enforcer-rules with Apache License 2.0 5 votes vote down vote up
protected Set<Artifact> getDependenciesToCheck( MavenProject project )
{
    Set<Artifact> dependencies = null;
    try
    {
        DependencyNode node = graphBuilder.buildDependencyGraph( project, null );
        dependencies  = getAllDescendants( node );
    }
    catch ( DependencyGraphBuilderException e )
    {
        // otherwise we need to change the signature of this protected method
        throw new RuntimeException( e );
    }
    return dependencies;
}
 
Example #4
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 #5
Source File: NarProvidedDependenciesMojo.java    From nifi-maven with Apache License 2.0 4 votes vote down vote up
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    try {
        // find the nar dependency
        Artifact narArtifact = null;
        for (final Artifact artifact : project.getDependencyArtifacts()) {
            if (NAR.equals(artifact.getType())) {
                // ensure the project doesn't have two nar dependencies
                if (narArtifact != null) {
                    throw new MojoExecutionException("Project can only have one NAR dependency.");
                }

                // record the nar dependency
                narArtifact = artifact;
            }
        }

        // ensure there is a nar dependency
        if (narArtifact == null) {
            throw new MojoExecutionException("Project does not have any NAR dependencies.");
        }

        // build the project for the nar artifact
        final ProjectBuildingRequest narRequest = new DefaultProjectBuildingRequest();
        narRequest.setRepositorySession(repoSession);
        narRequest.setSystemProperties(System.getProperties());

        final ProjectBuildingResult narResult = projectBuilder.build(narArtifact, narRequest);
        narRequest.setProject(narResult.getProject());

        // get the artifact handler for excluding dependencies
        final ArtifactHandler narHandler = excludesDependencies(narArtifact);
        narArtifact.setArtifactHandler(narHandler);

        // nar artifacts by nature includes dependencies, however this prevents the
        // transitive dependencies from printing using tools like dependency:tree.
        // here we are overriding the artifact handler for all nars so the
        // dependencies can be listed. this is important because nar dependencies
        // will be used as the parent classloader for this nar and seeing what
        // dependencies are provided is critical.
        final Map<String, ArtifactHandler> narHandlerMap = new HashMap<>();
        narHandlerMap.put(NAR, narHandler);
        artifactHandlerManager.addHandlers(narHandlerMap);

        // get the dependency tree
        final DependencyNode root = dependencyGraphBuilder.buildDependencyGraph(narRequest, null);

        // write the appropriate output
        DependencyNodeVisitor visitor = null;
        if ("tree".equals(mode)) {
            visitor = new TreeWriter();
        } else if ("pom".equals(mode)) {
            visitor = new PomWriter();
        }

        // ensure the mode was specified correctly
        if (visitor == null) {
            throw new MojoExecutionException("The specified mode is invalid. Supported options are 'tree' and 'pom'.");
        }

        // visit and print the results
        root.accept(visitor);
        getLog().info("--- Provided NAR Dependencies ---\n\n" + visitor.toString());
    } catch (ProjectBuildingException | DependencyGraphBuilderException e) {
        throw new MojoExecutionException("Cannot build project dependency tree", e);
    }
}