@NotNull protected List<ProjectBuildingResult> getProjectBuildingResults( @NotNull MavenExecutionRequest request, @NotNull Collection<File> files) { final ProjectBuilder builder = getComponent(ProjectBuilder.class); CustomMaven3ModelInterpolator2 modelInterpolator = (CustomMaven3ModelInterpolator2) getComponent(ModelInterpolator.class); String savedLocalRepository = modelInterpolator.getLocalRepository(); modelInterpolator.setLocalRepository(request.getLocalRepositoryPath().getAbsolutePath()); List<ProjectBuildingResult> buildingResults = new SmartList<ProjectBuildingResult>(); final ProjectBuildingRequest projectBuildingRequest = request.getProjectBuildingRequest(); projectBuildingRequest.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL); projectBuildingRequest.setResolveDependencies(false); try { buildingResults = builder.build(new ArrayList<File>(files), false, projectBuildingRequest); } catch (ProjectBuildingException e) { for (ProjectBuildingResult result : e.getResults()) { if (result.getProject() != null) { buildingResults.add(result); } else { try { ProjectBuildingResult build = builder.build(result.getPomFile(), projectBuildingRequest); buildingResults.add(build); } catch (ProjectBuildingException e2) { buildingResults.addAll(e2.getResults()); } } } } finally { modelInterpolator.setLocalRepository(savedLocalRepository); } return buildingResults; }
public List<Artifact> resolvePom(File pomFile) { if (pomFile == null) { throw new RuntimeException("pomFile is null"); } MavenProject pom; try { PlexusContainer container = container(); org.apache.maven.repository.RepositorySystem lrs = container.lookup(org.apache.maven.repository.RepositorySystem.class); ProjectBuilder projectBuilder = container.lookup(ProjectBuilder.class); ProjectBuildingRequest request = new DefaultProjectBuildingRequest(); request.setRepositorySession(repositorySystemSession); request.setProcessPlugins(false); request.setLocalRepository(lrs.createDefaultLocalRepository()); request.setRemoteRepositories( Arrays.asList(new ArtifactRepository[] {lrs.createDefaultRemoteRepository()}.clone())); ProjectBuildingResult result = projectBuilder.build(pomFile, request); pom = result.getProject(); } catch (Exception e) { if (e instanceof RuntimeException) { throw (RuntimeException) e; } throw new RuntimeException("Error loading pom: " + pomFile.getAbsolutePath(), e); } Artifact rootArtifact = new DefaultArtifact( pom.getArtifact().getGroupId(), pom.getArtifact().getArtifactId(), pom.getArtifact().getClassifier(), pom.getArtifact().getType(), pom.getArtifact().getVersion(), null, new File(pom.getModel().getBuild().getOutputDirectory())); CollectRequest collectRequest = new CollectRequest(); for (org.apache.maven.model.Dependency dependency : pom.getDependencies()) { collectRequest.addDependency(toAetherDependency(dependency)); } for (RemoteRepository repository : pom.getRemoteProjectRepositories()) { collectRequest.addRepository(repository); } for (RemoteRepository repository : repositories) { collectRequest.addRepository(repository); } // Make sure we account for managed dependencies if (pom.getDependencyManagement() != null) { for (org.apache.maven.model.Dependency managedDependency : pom.getDependencyManagement().getDependencies()) { collectRequest.addManagedDependency(toAetherDependency(managedDependency)); } } DependencyRequest dependencyRequest = new DependencyRequest( collectRequest, DependencyFilterUtils.classpathFilter(JavaScopes.RUNTIME)); List<Artifact> artifacts = resolveArtifacts(dependencyRequest); return ImmutableList.<Artifact>builder().add(rootArtifact).addAll(artifacts).build(); }
@Override public void resolveProjectDependencies( final IMavenProjectFacade facade, Set<Capability> capabilities, Set<RequiredCapability> requirements, final IProgressMonitor monitor) throws CoreException { long start = System.currentTimeMillis(); log.debug("Resolving dependencies for {}", facade.toString()); // $NON-NLS-1$ markerManager.deleteMarkers(facade.getPom(), IMavenConstants.MARKER_DEPENDENCY_ID); ProjectBuildingRequest configuration = getMaven().getExecutionContext().newProjectBuildingRequest(); configuration.setProject(facade.getMavenProject()); // TODO do we need this? configuration.setResolveDependencies(true); MavenExecutionResult mavenResult = getMaven().readMavenProject(facade.getPomFile(), configuration); markerManager.addMarkers(facade.getPom(), IMavenConstants.MARKER_DEPENDENCY_ID, mavenResult); if (!facade.getResolverConfiguration().shouldResolveWorkspaceProjects()) { return; } MavenProject mavenProject = facade.getMavenProject(); // dependencies // resolved dependencies for (Artifact artifact : mavenProject.getArtifacts()) { requirements.add( MavenRequiredCapability.createMavenArtifact( new ArtifactKey(artifact), artifact.getScope(), artifact.isOptional())); } // extension plugins (affect packaging type calculation) for (Plugin plugin : mavenProject.getBuildPlugins()) { if (plugin.isExtensions()) { ArtifactKey artifactKey = new ArtifactKey(plugin.getGroupId(), plugin.getArtifactId(), plugin.getVersion(), null); requirements.add( MavenRequiredCapability.createMavenArtifact( artifactKey, "plugin", false)); // $NON-NLS-1$ } } // missing dependencies DependencyResolutionResult resolutionResult = mavenResult.getDependencyResolutionResult(); if (resolutionResult != null && resolutionResult.getUnresolvedDependencies() != null) { for (Dependency dependency : resolutionResult.getUnresolvedDependencies()) { org.eclipse.aether.artifact.Artifact artifact = dependency.getArtifact(); ArtifactKey dependencyKey = new ArtifactKey( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), null); requirements.add( MavenRequiredCapability.createMavenArtifact( dependencyKey, dependency.getScope(), dependency.isOptional())); } } log.debug( "Resolved dependencies for {} in {} ms", facade.toString(), System.currentTimeMillis() - start); // $NON-NLS-1$ }