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);
}
}
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;
}
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);
}
}
}
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
}
}
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;
}
Aggregations