use of org.apache.maven.project.DefaultProjectBuildingRequest in project maven-plugins by apache.
the class InstallMojoTest method createMavenSession.
private MavenSession createMavenSession() {
MavenSession session = mock(MavenSession.class);
DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession();
repositorySession.setLocalRepositoryManager(new EnhancedLocalRepositoryManager(new File(LOCAL_REPO)));
ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
buildingRequest.setRepositorySession(repositorySession);
when(session.getProjectBuildingRequest()).thenReturn(buildingRequest);
return session;
}
use of org.apache.maven.project.DefaultProjectBuildingRequest in project maven-plugins by apache.
the class ShadeMojo method rewriteDependencyReducedPomIfWeHaveReduction.
private void rewriteDependencyReducedPomIfWeHaveReduction(List<Dependency> dependencies, boolean modified, List<Dependency> transitiveDeps, Model model) throws IOException, ProjectBuildingException, DependencyGraphBuilderException {
if (modified) {
for (int loopCounter = 0; modified; loopCounter++) {
model.setDependencies(dependencies);
if (generateUniqueDependencyReducedPom) {
dependencyReducedPomLocation = File.createTempFile("dependency-reduced-pom-", ".xml", project.getBasedir());
project.getProperties().setProperty("maven.shade.dependency-reduced-pom", dependencyReducedPomLocation.getAbsolutePath());
} else {
if (dependencyReducedPomLocation == null) {
// MSHADE-123: We can't default to 'target' because it messes up uses of ${project.basedir}
dependencyReducedPomLocation = new File(project.getBasedir(), "dependency-reduced-pom.xml");
}
}
File f = dependencyReducedPomLocation;
// Works for now, maybe there's a better algorithm where no for-loop is required
if (loopCounter == 0) {
getLog().info("Dependency-reduced POM written at: " + f.getAbsolutePath());
}
if (f.exists()) {
// noinspection ResultOfMethodCallIgnored
f.delete();
}
Writer w = WriterFactory.newXmlWriter(f);
String replaceRelativePath = null;
if (model.getParent() != null) {
replaceRelativePath = model.getParent().getRelativePath();
}
if (model.getParent() != null) {
File parentFile = new File(project.getBasedir(), model.getParent().getRelativePath()).getCanonicalFile();
if (!parentFile.isFile()) {
parentFile = new File(parentFile, "pom.xml");
}
parentFile = parentFile.getCanonicalFile();
String relPath = RelativizePath.convertToRelativePath(parentFile, f);
model.getParent().setRelativePath(relPath);
}
try {
PomWriter.write(w, model, true);
} finally {
if (model.getParent() != null) {
model.getParent().setRelativePath(replaceRelativePath);
}
w.close();
}
ProjectBuildingRequest projectBuildingRequest = new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
projectBuildingRequest.setLocalRepository(localRepository);
projectBuildingRequest.setRemoteRepositories(remoteArtifactRepositories);
ProjectBuildingResult result = projectBuilder.build(f, projectBuildingRequest);
getLog().debug("updateExcludesInDeps()");
modified = updateExcludesInDeps(result.getProject(), dependencies, transitiveDeps);
}
project.setFile(dependencyReducedPomLocation);
}
}
use of org.apache.maven.project.DefaultProjectBuildingRequest in project maven-plugins by apache.
the class DefaultDependencyResolver method updateDependencySetResolutionRequirements.
void updateDependencySetResolutionRequirements(final DependencySet set, final ResolutionManagementInfo requirements, AssemblyId assemblyId, ProjectBuildingRequest buildingRequest, final MavenProject... projects) throws DependencyResolutionException {
requirements.setResolutionRequired(true);
requirements.setResolvedTransitively(set.isUseTransitiveDependencies());
ScopeFilter scopeFilter = FilterUtils.newScopeFilter(set.getScope());
requirements.setScopeFilter(scopeFilter);
for (final MavenProject project : projects) {
if (project == null) {
continue;
}
Set<Artifact> dependencyArtifacts = project.getDependencyArtifacts();
if (dependencyArtifacts == null) {
try {
ProjectBuildingRequest pbr = new DefaultProjectBuildingRequest(buildingRequest);
pbr.setRemoteRepositories(project.getRemoteArtifactRepositories());
Iterable<ArtifactResult> artifactResults = dependencyResolver.resolveDependencies(pbr, project.getModel(), scopeFilter);
dependencyArtifacts = new HashSet<Artifact>();
for (ArtifactResult artifactResult : artifactResults) {
dependencyArtifacts.add(artifactResult.getArtifact());
}
project.setDependencyArtifacts(dependencyArtifacts);
} catch (final DependencyResolverException e) {
throw new DependencyResolutionException("Failed to create dependency artifacts for resolution. Assembly: " + assemblyId, e);
}
}
requirements.addArtifacts(dependencyArtifacts);
getLogger().debug("Dependencies for project: " + project.getId() + " are:\n" + StringUtils.join(dependencyArtifacts.iterator(), "\n"));
}
}
use of org.apache.maven.project.DefaultProjectBuildingRequest in project maven-plugins by apache.
the class AbstractDependencyMojo method newResolveArtifactProjectBuildingRequest.
/**
* @return Returns a new ProjectBuildingRequest populated from the current session and the current project remote
* repositories, used to resolve artifacts.
*/
public ProjectBuildingRequest newResolveArtifactProjectBuildingRequest() {
ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
buildingRequest.setRemoteRepositories(remoteRepositories);
return buildingRequest;
}
use of org.apache.maven.project.DefaultProjectBuildingRequest in project maven-plugins by apache.
the class GetMojo method execute.
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
if (isSkip()) {
getLog().info("Skipping plugin execution");
return;
}
if (coordinate.getArtifactId() == null && artifact == null) {
throw new MojoFailureException("You must specify an artifact, " + "e.g. -Dartifact=org.apache.maven.plugins:maven-downloader-plugin:1.0");
}
if (artifact != null) {
String[] tokens = StringUtils.split(artifact, ":");
if (tokens.length < 3 || tokens.length > 5) {
throw new MojoFailureException("Invalid artifact, you must specify " + "groupId:artifactId:version[:packaging[:classifier]] " + artifact);
}
coordinate.setGroupId(tokens[0]);
coordinate.setArtifactId(tokens[1]);
coordinate.setVersion(tokens[2]);
if (tokens.length >= 4) {
coordinate.setType(tokens[3]);
}
if (tokens.length == 5) {
coordinate.setClassifier(tokens[4]);
}
}
ArtifactRepositoryPolicy always = new ArtifactRepositoryPolicy(true, ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS, ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN);
List<ArtifactRepository> repoList = new ArrayList<ArtifactRepository>();
if (pomRemoteRepositories != null) {
repoList.addAll(pomRemoteRepositories);
}
if (remoteRepositories != null) {
// Use the same format as in the deploy plugin id::layout::url
List<String> repos = Arrays.asList(StringUtils.split(remoteRepositories, ","));
for (String repo : repos) {
repoList.add(parseRepository(repo, always));
}
}
try {
ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
buildingRequest.setRemoteRepositories(repoList);
if (transitive) {
getLog().info("Resolving " + coordinate + " with transitive dependencies");
dependencyResolver.resolveDependencies(buildingRequest, coordinate, null);
} else {
getLog().info("Resolving " + coordinate);
artifactResolver.resolveArtifact(buildingRequest, toArtifactCoordinate(coordinate));
}
} catch (ArtifactResolverException e) {
throw new MojoExecutionException("Couldn't download artifact: " + e.getMessage(), e);
} catch (DependencyResolverException e) {
throw new MojoExecutionException("Couldn't download artifact: " + e.getMessage(), e);
}
}
Aggregations