use of org.apache.maven.project.ProjectBuilder in project drools by kiegroup.
the class MavenEmbedder method readProject.
// ----------------------------------------------------------------------
// Project
// ----------------------------------------------------------------------
public MavenProject readProject(final InputStream mavenProjectStream) throws ProjectBuildingException, MavenEmbedderException {
ModelSource modelSource = new ModelSource() {
@Override
public InputStream getInputStream() {
return mavenProjectStream;
}
@Override
public String getLocation() {
return "";
}
};
ClassLoader originalCl = Thread.currentThread().getContextClassLoader();
try {
org.eclipse.aether.artifact.Artifact lastArtifact = null;
do {
Thread.currentThread().setContextClassLoader(componentProvider.getSystemClassLoader());
ProjectBuilder projectBuilder = componentProvider.lookup(ProjectBuilder.class);
// BZ-1007894: Check if added dependencies are resolvable.
ProjectBuildingResult result = projectBuilder.build(modelSource, getProjectBuildingRequest());
if (result != null && result.getDependencyResolutionResult() != null && !result.getDependencyResolutionResult().getCollectionErrors().isEmpty()) {
// A dependency resolution error has been produced. It can contains some error. Throw the first one to the client, so the user will fix every one sequentially.
final Exception depedencyResolutionException = result.getDependencyResolutionResult().getCollectionErrors().get(0);
if (depedencyResolutionException instanceof ArtifactDescriptorException) {
final org.eclipse.aether.artifact.Artifact artifact = ((ArtifactDescriptorException) depedencyResolutionException).getResult().getArtifact();
if (!artifact.equals(lastArtifact)) {
tryRemoveLocalArtifact(artifact);
lastArtifact = artifact;
continue;
}
}
if (depedencyResolutionException != null) {
throw new MavenEmbedderException(depedencyResolutionException.getMessage(), depedencyResolutionException);
}
}
return (result == null || result.getProject() == null) ? null : result.getProject();
} while (true);
} catch (ComponentLookupException e) {
throw new MavenEmbedderException(e.getMessage(), e);
} finally {
Thread.currentThread().setContextClassLoader(originalCl);
try {
mavenProjectStream.close();
} catch (IOException e) {
}
}
}
use of org.apache.maven.project.ProjectBuilder in project che by eclipse.
the class MavenServerImpl method internalResolveProject.
private MavenResult internalResolveProject(File pom, List<String> activeProfiles, List<String> inactiveProfiles, List<ResolutionListener> dependencyTreeResolutionListeners) {
MavenExecutionRequest request = newMavenRequest(pom, activeProfiles, inactiveProfiles, Collections.emptyList());
request.setUpdateSnapshots(updateSnapshots);
AtomicReference<MavenResult> reference = new AtomicReference<>();
runMavenRequest(request, () -> {
try {
ProjectBuilder builder = getMavenComponent(ProjectBuilder.class);
List<ProjectBuildingResult> resultList = builder.build(Collections.singletonList(pom), false, request.getProjectBuildingRequest());
ProjectBuildingResult result = resultList.get(0);
MavenProject mavenProject = result.getProject();
RepositorySystemSession repositorySession = getMavenComponent(LegacySupport.class).getRepositorySession();
if (repositorySession instanceof DefaultRepositorySystemSession) {
((DefaultRepositorySystemSession) repositorySession).setTransferListener(new ArtifactTransferListener(mavenProgressNotifier));
if (workspaceCache != null) {
((DefaultRepositorySystemSession) repositorySession).setWorkspaceReader(new MavenWorkspaceReader(workspaceCache));
}
}
List<Exception> exceptions = new ArrayList<>();
loadExtensions(mavenProject, exceptions);
mavenProject.setDependencyArtifacts(mavenProject.createArtifacts(getMavenComponent(ArtifactFactory.class), null, null));
ArtifactResolutionRequest resolutionRequest = new ArtifactResolutionRequest();
resolutionRequest.setArtifact(mavenProject.getArtifact());
resolutionRequest.setRemoteRepositories(mavenProject.getRemoteArtifactRepositories());
resolutionRequest.setArtifactDependencies(mavenProject.getDependencyArtifacts());
resolutionRequest.setListeners(dependencyTreeResolutionListeners);
resolutionRequest.setLocalRepository(localRepo);
resolutionRequest.setManagedVersionMap(mavenProject.getManagedVersionMap());
resolutionRequest.setResolveTransitively(true);
resolutionRequest.setResolveRoot(false);
ArtifactResolver resolver = getMavenComponent(ArtifactResolver.class);
ArtifactResolutionResult resolve = resolver.resolve(resolutionRequest);
mavenProject.setArtifacts(resolve.getArtifacts());
reference.set(new MavenResult(mavenProject, exceptions));
} catch (Exception e) {
reference.set(new MavenResult(null, null, Collections.singletonList(e)));
}
});
return reference.get();
}
use of org.apache.maven.project.ProjectBuilder in project che by eclipse.
the class EffectivePomWriter method getEffectivePom.
public static String getEffectivePom(MavenServerImpl server, final File pom, List<String> activeProfiles, List<String> inactiveProfiles) {
StringWriter stringWriter = new StringWriter();
try {
MavenExecutionRequest request = server.newMavenRequest(pom, activeProfiles, inactiveProfiles, Collections.emptyList());
server.runMavenRequest(request, () -> {
try {
ProjectBuilder builder = server.getMavenComponent(ProjectBuilder.class);
ProjectBuildingResult projectBuildingResult = builder.build(new File(pom.getPath()), request.getProjectBuildingRequest());
MavenProject project = projectBuildingResult.getProject();
XMLWriter writer = new PrettyPrintXMLWriter(stringWriter, " ");
writeHeader(writer);
writeEffectivePom(project, writer);
} catch (ProjectBuildingException | MojoExecutionException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
});
} catch (Exception e) {
return null;
}
return stringWriter.toString();
}
use of org.apache.maven.project.ProjectBuilder in project intellij-community by JetBrains.
the class MavenEffectivePomDumper method evaluateEffectivePom.
// See org.apache.maven.plugins.help.EffectivePomMojo#execute from maven-help-plugin
@Nullable
public static String evaluateEffectivePom(final Maven3ServerEmbedder embedder, @NotNull final File file, @NotNull List<String> activeProfiles, @NotNull List<String> inactiveProfiles) throws RemoteException, MavenServerProcessCanceledException {
final StringWriter w = new StringWriter();
try {
final MavenExecutionRequest request = embedder.createRequest(file, activeProfiles, inactiveProfiles, Collections.<String>emptyList());
embedder.executeWithMavenSession(request, new Runnable() {
@Override
public void run() {
try {
// copied from DefaultMavenProjectBuilder.buildWithDependencies
ProjectBuilder builder = embedder.getComponent(ProjectBuilder.class);
ProjectBuildingResult buildingResult = builder.build(new File(file.getPath()), request.getProjectBuildingRequest());
MavenProject project = buildingResult.getProject();
XMLWriter writer = new PrettyPrintXMLWriter(new PrintWriter(w), StringUtils.repeat(" ", XmlWriterUtil.DEFAULT_INDENTATION_SIZE), "\n", null, null);
writeHeader(writer);
writeEffectivePom(project, writer);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
});
} catch (Exception e) {
return null;
}
return w.toString();
}
use of org.apache.maven.project.ProjectBuilder in project drools by kiegroup.
the class MavenEmbedder method buildProjects.
public List<ProjectBuildingResult> buildProjects(File mavenProject, boolean recursive) throws ProjectBuildingException, MavenEmbedderException {
ClassLoader originalCl = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(componentProvider.getSystemClassLoader());
ProjectBuilder projectBuilder = componentProvider.lookup(ProjectBuilder.class);
return projectBuilder.build(Collections.singletonList(mavenProject), recursive, getProjectBuildingRequest());
} catch (ComponentLookupException e) {
throw new MavenEmbedderException(e.getMessage(), e);
} finally {
Thread.currentThread().setContextClassLoader(originalCl);
}
}
Aggregations