Java Code Examples for org.apache.maven.project.ProjectBuildingRequest#setProject()

The following examples show how to use org.apache.maven.project.ProjectBuildingRequest#setProject() . 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: 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 2
Source File: ForArtifactMojo.java    From depgraph-maven-plugin with Apache License 2.0 6 votes vote down vote up
@Override
public MavenProject getProject() {
  ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(this.session.getProjectBuildingRequest());
  buildingRequest.setRepositorySession(this.session.getRepositorySession());
  buildingRequest.setProject(null);
  buildingRequest.setResolveDependencies(true);
  buildingRequest.setActiveProfileIds(this.profiles);

  DefaultArtifact artifact = new DefaultArtifact(this.groupId, this.artifactId, this.version, SCOPE_COMPILE, this.type, this.classifier, new DefaultArtifactHandler());
  try {
    return this.projectBuilder.build(artifact, buildingRequest).getProject();
  } catch (ProjectBuildingException e) {
    throw new IllegalStateException("Error while creating Maven project from Artifact '" + artifact + "'.", e);
  }

}
 
Example 3
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);
    }
}