use of org.apache.maven.execution.MavenExecutionRequest 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.execution.MavenExecutionRequest 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.execution.MavenExecutionRequest in project che by eclipse.
the class MavenServerImpl method resolveArtifact.
@Override
public MavenArtifact resolveArtifact(MavenArtifactKey artifactKey, List<MavenRemoteRepository> repositories) throws RemoteException {
Artifact artifact = getMavenComponent(ArtifactFactory.class).createArtifactWithClassifier(artifactKey.getGroupId(), artifactKey.getArtifactId(), artifactKey.getVersion(), artifactKey.getPackaging(), artifactKey.getClassifier());
List<ArtifactRepository> repos = new ArrayList<>();
ArtifactRepositoryFactory factory = getMavenComponent(ArtifactRepositoryFactory.class);
for (MavenRemoteRepository repository : repositories) {
try {
ArtifactRepository artifactRepository = ProjectUtils.buildArtifactRepository(MavenModelUtil.convertToMavenRepository(repository), factory, container);
repos.add(artifactRepository);
} catch (InvalidRepositoryException e) {
MavenServerContext.getLogger().error(e);
}
}
MavenExecutionRequest request = newMavenRequest(null, Collections.emptyList(), Collections.emptyList(), Collections.emptyList());
repos.forEach(request::addRemoteRepository);
DefaultMaven maven = (DefaultMaven) getMavenComponent(Maven.class);
RepositorySystemSession repositorySystemSession = maven.newRepositorySession(request);
org.eclipse.aether.impl.ArtifactResolver artifactResolver = getMavenComponent(org.eclipse.aether.impl.ArtifactResolver.class);
InternalLoggerFactory loggerFactory = new InternalLoggerFactory();
if (artifactResolver instanceof DefaultArtifactResolver) {
((DefaultArtifactResolver) artifactResolver).setLoggerFactory(loggerFactory);
}
org.eclipse.aether.RepositorySystem repositorySystem = getMavenComponent(org.eclipse.aether.RepositorySystem.class);
if (repositorySystem instanceof DefaultRepositorySystem) {
((DefaultRepositorySystem) repositorySystem).setLoggerFactory(loggerFactory);
}
List<RemoteRepository> remoteRepositories = RepositoryUtils.toRepos(request.getRemoteRepositories());
remoteRepositories = repositorySystem.newResolutionRepositories(repositorySystemSession, remoteRepositories);
try {
ArtifactResult artifactResult = repositorySystem.resolveArtifact(repositorySystemSession, new ArtifactRequest(RepositoryUtils.toArtifact(artifact), remoteRepositories, null));
return MavenModelUtil.convertArtifact(RepositoryUtils.toArtifact(artifactResult.getArtifact()), localRepository);
} catch (ArtifactResolutionException e) {
MavenServerContext.getLogger().info(e);
}
return MavenModelUtil.convertArtifact(artifact, localRepository);
}
use of org.apache.maven.execution.MavenExecutionRequest in project intellij-community by JetBrains.
the class Maven3ServerIndexerImpl method updateIndex.
@Override
public void updateIndex(int id, MavenServerSettings settings, final MavenServerProgressIndicator indicator) throws RemoteException, MavenServerIndexerException, MavenServerProcessCanceledException {
final IndexingContext index = getIndex(id);
try {
File repository = index.getRepository();
if (repository != null) {
// is local repository
if (repository.exists()) {
indicator.setIndeterminate(true);
try {
myIndexer.scan(index, new MyScanningListener(indicator), false);
} finally {
indicator.setIndeterminate(false);
}
}
} else {
final Maven3ServerEmbedder embedder = createEmbedder(settings);
MavenExecutionRequest r = embedder.createRequest(null, Collections.<String>emptyList(), Collections.<String>emptyList(), Collections.<String>emptyList());
final IndexUpdateRequest request = new IndexUpdateRequest(index);
try {
embedder.executeWithMavenSession(r, new Runnable() {
@Override
public void run() {
request.setResourceFetcher(new Maven3ServerIndexFetcher(index.getRepositoryId(), index.getRepositoryUrl(), embedder.getComponent(WagonManager.class), embedder.getComponent(RepositorySystem.class), new WagonTransferListenerAdapter(indicator) {
@Override
protected void downloadProgress(long downloaded, long total) {
super.downloadProgress(downloaded, total);
try {
myIndicator.setFraction(((double) downloaded) / total);
} catch (RemoteException e) {
throw new RuntimeRemoteException(e);
}
}
@Override
public void transferCompleted(TransferEvent event) {
super.transferCompleted(event);
try {
myIndicator.setText2("Processing indices...");
} catch (RemoteException e) {
throw new RuntimeRemoteException(e);
}
}
}));
try {
myUpdater.fetchAndUpdateIndex(request);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
} finally {
embedder.release();
}
}
} catch (RuntimeRemoteException e) {
throw e.getCause();
} catch (ProcessCanceledException e) {
throw new MavenServerProcessCanceledException();
} catch (Exception e) {
throw new MavenServerIndexerException(wrapException(e));
}
}
use of org.apache.maven.execution.MavenExecutionRequest 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();
}
Aggregations