Search in sources :

Example 26 with ProjectBuildingRequest

use of org.apache.maven.project.ProjectBuildingRequest in project maven-plugins by apache.

the class InstallFileMojo method createMavenProject.

/**
 * Creates a Maven project in-memory from the user-supplied groupId, artifactId and version. When a classifier is
 * supplied, the packaging must be POM because the project with only have attachments. This project serves as basis
 * to attach the artifacts to install to.
 *
 * @return The created Maven project, never <code>null</code>.
 * @throws MojoExecutionException When the model of the project could not be built.
 * @throws MojoFailureException When building the project failed.
 */
private MavenProject createMavenProject() throws MojoExecutionException, MojoFailureException {
    if (groupId == null || artifactId == null || version == null || packaging == null) {
        throw new MojoExecutionException("The artifact information is incomplete: 'groupId', 'artifactId', " + "'version' and 'packaging' are required.");
    }
    ModelSource modelSource = new StringModelSource("<project><modelVersion>4.0.0</modelVersion><groupId>" + groupId + "</groupId><artifactId>" + artifactId + "</artifactId><version>" + version + "</version><packaging>" + (classifier == null ? packaging : "pom") + "</packaging></project>");
    ProjectBuildingRequest pbr = new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
    pbr.setProcessPlugins(false);
    try {
        return projectBuilder.build(modelSource, pbr).getProject();
    } catch (ProjectBuildingException e) {
        if (e.getCause() instanceof ModelBuildingException) {
            throw new MojoExecutionException("The artifact information is not valid:" + Os.LINE_SEP + e.getCause().getMessage());
        }
        throw new MojoFailureException("Unable to create the project.", e);
    }
}
Also used : ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest) ProjectBuildingException(org.apache.maven.project.ProjectBuildingException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) ModelBuildingException(org.apache.maven.model.building.ModelBuildingException) StringModelSource(org.apache.maven.model.building.StringModelSource) StringModelSource(org.apache.maven.model.building.StringModelSource) ModelSource(org.apache.maven.model.building.ModelSource) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest)

Example 27 with ProjectBuildingRequest

use of org.apache.maven.project.ProjectBuildingRequest in project maven-plugins by apache.

the class AbstractDependencyFilterMojo method resolve.

/**
 * @param coordinates The set of artifact coordinates{@link ArtifactCoordinate}.
 * @param stopOnFailure <code>true</code> if we should fail with exception if an artifact couldn't be resolved
 *            <code>false</code> otherwise.
 * @return the resolved artifacts. {@link Artifact}.
 * @throws MojoExecutionException in case of error.
 */
protected Set<Artifact> resolve(Set<ArtifactCoordinate> coordinates, boolean stopOnFailure) throws MojoExecutionException {
    ProjectBuildingRequest buildingRequest = newResolveArtifactProjectBuildingRequest();
    Set<Artifact> resolvedArtifacts = new LinkedHashSet<Artifact>();
    for (ArtifactCoordinate coordinate : coordinates) {
        try {
            Artifact artifact = artifactResolver.resolveArtifact(buildingRequest, coordinate).getArtifact();
            resolvedArtifacts.add(artifact);
        } catch (ArtifactResolverException ex) {
            // an error occurred during resolution, log it an continue
            getLog().debug("error resolving: " + coordinate);
            getLog().debug(ex);
            if (stopOnFailure) {
                throw new MojoExecutionException("error resolving: " + coordinate, ex);
            }
        }
    }
    return resolvedArtifacts;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) ArtifactCoordinate(org.apache.maven.shared.artifact.ArtifactCoordinate) ArtifactResolverException(org.apache.maven.shared.artifact.resolve.ArtifactResolverException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Artifact(org.apache.maven.artifact.Artifact)

Example 28 with ProjectBuildingRequest

use of org.apache.maven.project.ProjectBuildingRequest in project maven-plugins by apache.

the class AbstractDependencyFilterMojo method addParentArtifacts.

private void addParentArtifacts(MavenProject project, Set<Artifact> artifacts) throws MojoExecutionException {
    while (project.hasParent()) {
        project = project.getParent();
        if (artifacts.contains(project.getArtifact())) {
            // artifact already in the set
            break;
        }
        try {
            ProjectBuildingRequest buildingRequest = newResolveArtifactProjectBuildingRequest();
            Artifact resolvedArtifact = artifactResolver.resolveArtifact(buildingRequest, project.getArtifact()).getArtifact();
            artifacts.add(resolvedArtifact);
        } catch (ArtifactResolverException e) {
            throw new MojoExecutionException(e.getMessage(), e);
        }
    }
}
Also used : ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) ArtifactResolverException(org.apache.maven.shared.artifact.resolve.ArtifactResolverException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Artifact(org.apache.maven.artifact.Artifact)

Example 29 with ProjectBuildingRequest

use of org.apache.maven.project.ProjectBuildingRequest in project maven-plugins by apache.

the class CopyDependenciesMojo method doExecute.

/**
 * Main entry into mojo. Gets the list of dependencies and iterates through calling copyArtifact.
 *
 * @throws MojoExecutionException with a message if an error occurs.
 * @see #getDependencySets(boolean, boolean)
 * @see #copyArtifact(Artifact, boolean, boolean, boolean, boolean)
 */
@Override
protected void doExecute() throws MojoExecutionException {
    DependencyStatusSets dss = getDependencySets(this.failOnMissingClassifierArtifact, addParentPoms);
    Set<Artifact> artifacts = dss.getResolvedDependencies();
    if (!useRepositoryLayout) {
        for (Artifact artifact : artifacts) {
            copyArtifact(artifact, isStripVersion(), this.prependGroupId, this.useBaseVersion, this.stripClassifier);
        }
    } else {
        ProjectBuildingRequest buildingRequest = getRepositoryManager().setLocalRepositoryBasedir(session.getProjectBuildingRequest(), outputDirectory);
        for (Artifact artifact : artifacts) {
            installArtifact(artifact, buildingRequest);
        }
    }
    Set<Artifact> skippedArtifacts = dss.getSkippedDependencies();
    for (Artifact artifact : skippedArtifacts) {
        getLog().info(artifact.getId() + " already exists in destination.");
    }
    if (isCopyPom() && !useRepositoryLayout) {
        copyPoms(getOutputDirectory(), artifacts, this.stripVersion);
        // Artifacts
        copyPoms(getOutputDirectory(), skippedArtifacts, this.stripVersion, this.stripClassifier);
    // that already
    // exist may
    // not yet have
    // poms
    }
}
Also used : ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) DependencyStatusSets(org.apache.maven.plugins.dependency.utils.DependencyStatusSets) Artifact(org.apache.maven.artifact.Artifact)

Example 30 with ProjectBuildingRequest

use of org.apache.maven.project.ProjectBuildingRequest in project maven-plugins by apache.

the class AbstractResolveMojo method resolveArtifactDependencies.

/**
 * This method resolves all transitive dependencies of an artifact.
 *
 * @param artifact the artifact used to retrieve dependencies
 * @return resolved set of dependencies
 * @throws DependencyResolverException in case of error while resolving artifacts.
 */
protected Set<Artifact> resolveArtifactDependencies(final DependableCoordinate artifact) throws DependencyResolverException {
    ProjectBuildingRequest buildingRequest = newResolveArtifactProjectBuildingRequest();
    Iterable<ArtifactResult> artifactResults = getDependencyResolver().resolveDependencies(buildingRequest, artifact, null);
    Set<Artifact> artifacts = new LinkedHashSet<Artifact>();
    for (final ArtifactResult artifactResult : artifactResults) {
        artifacts.add(artifactResult.getArtifact());
    }
    return artifacts;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) Artifact(org.apache.maven.artifact.Artifact) ArtifactResult(org.apache.maven.shared.artifact.resolve.ArtifactResult)

Aggregations

ProjectBuildingRequest (org.apache.maven.project.ProjectBuildingRequest)81 File (java.io.File)40 DefaultProjectBuildingRequest (org.apache.maven.project.DefaultProjectBuildingRequest)37 MavenRepositorySystemSession (org.apache.maven.repository.internal.MavenRepositorySystemSession)28 SimpleLocalRepositoryManager (org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager)28 Artifact (org.apache.maven.artifact.Artifact)25 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)22 MavenProject (org.apache.maven.project.MavenProject)22 MavenSession (org.apache.maven.execution.MavenSession)17 IOException (java.io.IOException)14 LegacySupport (org.apache.maven.plugin.LegacySupport)13 ArtifactResolverException (org.apache.maven.shared.artifact.resolve.ArtifactResolverException)12 ArrayList (java.util.ArrayList)9 ArtifactRepository (org.apache.maven.artifact.repository.ArtifactRepository)8 MavenArtifactRepository (org.apache.maven.artifact.repository.MavenArtifactRepository)8 ArchetypeGenerationRequest (org.apache.maven.archetype.ArchetypeGenerationRequest)7 ArchetypeManager (org.apache.maven.archetype.ArchetypeManager)7 ArchetypeCatalog (org.apache.maven.archetype.catalog.ArchetypeCatalog)7 ProjectBuildingException (org.apache.maven.project.ProjectBuildingException)7 ArtifactResult (org.apache.maven.shared.artifact.resolve.ArtifactResult)7