Java Code Examples for org.apache.maven.project.ProjectBuildingRequest#setRepositorySession()
The following examples show how to use
org.apache.maven.project.ProjectBuildingRequest#setRepositorySession() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MavenEmbedder.java From netbeans with Apache License 2.0 | 6 votes |
public MavenExecutionResult readProjectWithDependencies(MavenExecutionRequest req, boolean useWorkspaceResolution) { if (useWorkspaceResolution) { req.setWorkspaceReader(new NbWorkspaceReader()); } File pomFile = req.getPom(); MavenExecutionResult result = new DefaultMavenExecutionResult(); try { ProjectBuildingRequest configuration = req.getProjectBuildingRequest(); configuration.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL); configuration.setResolveDependencies(true); configuration.setRepositorySession(maven.newRepositorySession(req)); ProjectBuildingResult projectBuildingResult = projectBuilder.build(pomFile, configuration); result.setProject(projectBuildingResult.getProject()); result.setDependencyResolutionResult(projectBuildingResult.getDependencyResolutionResult()); } catch (ProjectBuildingException ex) { //don't add the exception here. this should come out as a build marker, not fill //the error logs with msgs return result.addException(ex); } normalizePaths(result.getProject()); return result; }
Example 2
Source File: ExtensionClassLoaderFactory.java From nifi-maven with Apache License 2.0 | 6 votes |
private Set<Artifact> getNarDependencies(final Artifact narArtifact) throws MojoExecutionException, ProjectBuildingException { final ProjectBuildingRequest narRequest = new DefaultProjectBuildingRequest(); narRequest.setRepositorySession(repoSession); narRequest.setSystemProperties(System.getProperties()); narRequest.setLocalRepository(localRepo); final ProjectBuildingResult narResult = projectBuilder.build(narArtifact, narRequest); final Set<Artifact> narDependencies = new TreeSet<>(); gatherArtifacts(narResult.getProject(), narDependencies); narDependencies.remove(narArtifact); narDependencies.remove(project.getArtifact()); getLog().debug("Found NAR dependency of " + narArtifact + ", which resolved to the following artifacts: " + narDependencies); return narDependencies; }
Example 3
Source File: ForArtifactMojo.java From depgraph-maven-plugin with Apache License 2.0 | 6 votes |
@Override public MavenProject getProject() { ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(this.session.getProjectBuildingRequest()); buildingRequest.setRepositorySession(this.session.getRepositorySession()); buildingRequest.setProject(null); buildingRequest.setResolveDependencies(true); buildingRequest.setActiveProfileIds(this.profiles); DefaultArtifact artifact = new DefaultArtifact(this.groupId, this.artifactId, this.version, SCOPE_COMPILE, this.type, this.classifier, new DefaultArtifactHandler()); try { return this.projectBuilder.build(artifact, buildingRequest).getProject(); } catch (ProjectBuildingException e) { throw new IllegalStateException("Error while creating Maven project from Artifact '" + artifact + "'.", e); } }
Example 4
Source File: RepositoryUtility.java From cloud-opensource-java with Apache License 2.0 | 5 votes |
static MavenProject createMavenProject(Path pomFile, RepositorySystemSession session) throws MavenRepositoryException { // MavenCli's way to instantiate PlexusContainer ClassWorld classWorld = new ClassWorld("plexus.core", Thread.currentThread().getContextClassLoader()); ContainerConfiguration containerConfiguration = new DefaultContainerConfiguration() .setClassWorld(classWorld) .setRealm(classWorld.getClassRealm("plexus.core")) .setClassPathScanning(PlexusConstants.SCANNING_INDEX) .setAutoWiring(true) .setJSR250Lifecycle(true) .setName("linkage-checker"); try { PlexusContainer container = new DefaultPlexusContainer(containerConfiguration); MavenExecutionRequest mavenExecutionRequest = new DefaultMavenExecutionRequest(); ProjectBuildingRequest projectBuildingRequest = mavenExecutionRequest.getProjectBuildingRequest(); projectBuildingRequest.setRepositorySession(session); // Profile activation needs properties such as JDK version Properties properties = new Properties(); // allowing duplicate entries properties.putAll(projectBuildingRequest.getSystemProperties()); properties.putAll(OsProperties.detectOsProperties()); properties.putAll(System.getProperties()); projectBuildingRequest.setSystemProperties(properties); ProjectBuilder projectBuilder = container.lookup(ProjectBuilder.class); ProjectBuildingResult projectBuildingResult = projectBuilder.build(pomFile.toFile(), projectBuildingRequest); return projectBuildingResult.getProject(); } catch (PlexusContainerException | ComponentLookupException | ProjectBuildingException ex) { throw new MavenRepositoryException(ex); } }
Example 5
Source File: NbMavenProjectImpl.java From netbeans with Apache License 2.0 | 5 votes |
/** * replacement for MavenProject.getParent() which has bad long term memory behaviour. We offset it by recalculating/reparsing everything * therefore should not be used lightly! * pass a MavenProject instance and current configuration and other settings will be applied when loading the parent. * @param project * @return null or the parent mavenproject */ public MavenProject loadParentOf(MavenEmbedder embedder, MavenProject project) throws ProjectBuildingException { MavenProject parent = null; ProjectBuilder builder = embedder.lookupComponent(ProjectBuilder.class); MavenExecutionRequest req = embedder.createMavenExecutionRequest(); M2Configuration active = configProvider.getActiveConfiguration(); req.addActiveProfiles(active.getActivatedProfiles()); req.setNoSnapshotUpdates(true); req.setUpdateSnapshots(false); req.setInteractiveMode(false); req.setRecursive(false); req.setOffline(true); //#238800 important to merge, not replace Properties uprops = req.getUserProperties(); uprops.putAll(MavenProjectCache.createUserPropsForProjectLoading(active.getProperties())); req.setUserProperties(uprops); ProjectBuildingRequest request = req.getProjectBuildingRequest(); request.setRemoteRepositories(project.getRemoteArtifactRepositories()); DefaultMaven maven = (DefaultMaven) embedder.lookupComponent(Maven.class); request.setRepositorySession(maven.newRepositorySession(req)); if (project.getParentFile() != null) { parent = builder.build(project.getParentFile(), request).getProject(); } else if (project.getModel().getParent() != null) { parent = builder.build(project.getParentArtifact(), request).getProject(); } //clear the project building request, it references multiple Maven Models via the RepositorySession cache //is not used in maven itself, most likely used by m2e only.. if (parent != null) { parent.setProjectBuildingRequest(null); } MavenEmbedder.normalizePaths(parent); return parent; }
Example 6
Source File: EffectivePomMD.java From netbeans with Apache License 2.0 | 5 votes |
static List<ModelProblem> runMavenValidationImpl(final File pom) { //TODO profiles based on current configuration?? MavenEmbedder embedder = EmbedderFactory.getProjectEmbedder(); MavenExecutionRequest meReq = embedder.createMavenExecutionRequest(); ProjectBuildingRequest req = meReq.getProjectBuildingRequest(); req.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MAVEN_3_1); // currently enables just <reporting> warning req.setLocalRepository(embedder.getLocalRepository()); List<ArtifactRepository> remoteRepos = RepositoryPreferences.getInstance().remoteRepositories(embedder); req.setRemoteRepositories(remoteRepos); req.setRepositorySession(((DefaultMaven) embedder.lookupComponent(Maven.class)).newRepositorySession(meReq)); List<ModelProblem> problems; try { problems = embedder.lookupComponent(ProjectBuilder.class).build(pom, req).getProblems(); } catch (ProjectBuildingException x) { problems = new ArrayList<ModelProblem>(); List<ProjectBuildingResult> results = x.getResults(); if (results != null) { //one code point throwing ProjectBuildingException contains results, for (ProjectBuildingResult result : results) { problems.addAll(result.getProblems()); } } else { // another code point throwing ProjectBuildingException doesn't contain results.. Throwable cause = x.getCause(); if (cause instanceof ModelBuildingException) { problems.addAll(((ModelBuildingException) cause).getProblems()); } } } return problems; }
Example 7
Source File: MavenEmbedder.java From netbeans with Apache License 2.0 | 5 votes |
public ProjectBuildingResult buildProject(Artifact art, ProjectBuildingRequest req) throws ProjectBuildingException { if (req.getLocalRepository() == null) { req.setLocalRepository(getLocalRepository()); } MavenExecutionRequest request = createMavenExecutionRequest(); req.setProcessPlugins(false); req.setRepositorySession(maven.newRepositorySession(request)); ProjectBuildingResult res = projectBuilder.build(art, req); normalizePaths(res.getProject()); return res; }
Example 8
Source File: StatusProvider.java From netbeans with Apache License 2.0 | 5 votes |
static List<ModelProblem> runMavenValidationImpl(final File pom) { MavenEmbedder embedder = EmbedderFactory.getProjectEmbedder(); MavenExecutionRequest meReq = embedder.createMavenExecutionRequest(); ProjectBuildingRequest req = meReq.getProjectBuildingRequest(); req.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MAVEN_3_0); // 3.1 currently enables just <reporting> warning, see issue 223562 for details on why it's bad to show. req.setLocalRepository(embedder.getLocalRepository()); List<ArtifactRepository> remoteRepos = RepositoryPreferences.getInstance().remoteRepositories(embedder); req.setRemoteRepositories(remoteRepos); req.setRepositorySession(((DefaultMaven) embedder.lookupComponent(Maven.class)).newRepositorySession(meReq)); List<ModelProblem> problems; try { problems = embedder.lookupComponent(ProjectBuilder.class).build(pom, req).getProblems(); } catch (ProjectBuildingException x) { problems = new ArrayList<ModelProblem>(); List<ProjectBuildingResult> results = x.getResults(); if (results != null) { //one code point throwing ProjectBuildingException contains results, for (ProjectBuildingResult result : results) { problems.addAll(result.getProblems()); } } else { // another code point throwing ProjectBuildingException doesn't contain results.. Throwable cause = x.getCause(); if (cause instanceof ModelBuildingException) { problems.addAll(((ModelBuildingException) cause).getProblems()); } } } List<ModelProblem> toRet = new LinkedList<ModelProblem>(); for (ModelProblem problem : problems) { if(ModelUtils.checkByCLIMavenValidationLevel(problem)) { toRet.add(problem); } } return toRet; }
Example 9
Source File: CodeGenMojoTest.java From copybook4java with MIT License | 5 votes |
private Mojo lookupConfiguredMojo(String goal, File pom) throws Exception { MavenExecutionRequest request = new DefaultMavenExecutionRequest(); request.setBaseDirectory(pom.getParentFile()); ProjectBuildingRequest configuration = request.getProjectBuildingRequest(); // Fix for bug: https://git-wip-us.apache.org/repos/asf?p=maven-plugin-testing.git;a=commit;h=3cd5f47c586499e438a3f9393304ac9d1f9a7f53 configuration.setRepositorySession(new DefaultRepositorySystemSession()); MavenProject project = lookup(ProjectBuilder.class).build(pom, configuration).getProject(); return super.lookupConfiguredMojo(project, goal); }
Example 10
Source File: ExtensionClassLoaderFactory.java From nifi-maven with Apache License 2.0 | 5 votes |
private String findProvidedDependencyVersion(final Set<Artifact> artifacts, final String groupId, final String artifactId) { final ProjectBuildingRequest projectRequest = new DefaultProjectBuildingRequest(); projectRequest.setRepositorySession(repoSession); projectRequest.setSystemProperties(System.getProperties()); projectRequest.setLocalRepository(localRepo); for (final Artifact artifact : artifacts) { final Set<Artifact> artifactDependencies = new HashSet<>(); try { final ProjectBuildingResult projectResult = projectBuilder.build(artifact, projectRequest); gatherArtifacts(projectResult.getProject(), artifactDependencies); getLog().debug("For Artifact " + artifact + ", found the following dependencies:"); artifactDependencies.forEach(dep -> getLog().debug(dep.toString())); for (final Artifact dependency : artifactDependencies) { if (dependency.getGroupId().equals(groupId) && dependency.getArtifactId().equals(artifactId)) { getLog().debug("Found version of " + groupId + ":" + artifactId + " to be " + artifact.getVersion()); return artifact.getVersion(); } } } catch (final Exception e) { getLog().warn("Unable to construct Maven Project for " + artifact + " when attempting to determine the expected version of NiFi API"); getLog().debug("Unable to construct Maven Project for " + artifact + " when attempting to determine the expected version of NiFi API", e); } } return null; }
Example 11
Source File: InstallDeployTest.java From takari-lifecycle with Eclipse Public License 1.0 | 5 votes |
private MavenProject readMavenProject(File basedir, Properties properties) throws Exception { File pom = new File(basedir, "pom.xml"); MavenExecutionRequest request = new DefaultMavenExecutionRequest(); request.setUserProperties(properties); request.setBaseDirectory(basedir); ProjectBuildingRequest configuration = request.getProjectBuildingRequest(); configuration.setRepositorySession(new DefaultRepositorySystemSession()); MavenProject project = mojos.lookup(ProjectBuilder.class).build(pom, configuration).getProject(); Assert.assertNotNull(project); return project; }
Example 12
Source File: AbstractWildFlyMojoTest.java From wildfly-maven-plugin with GNU Lesser General Public License v2.1 | 5 votes |
public MavenProject readMavenProject(Path pom) throws Exception { MavenExecutionRequest request = new DefaultMavenExecutionRequest(); request.setBaseDirectory(pom.getParent().toFile()); ProjectBuildingRequest configuration = request.getProjectBuildingRequest(); configuration.setRepositorySession(new DefaultRepositorySystemSession()); MavenProject project = rule.lookup(ProjectBuilder.class).build(pom.toFile(), configuration).getProject(); Assert.assertNotNull(project); return project; }
Example 13
Source File: NarProvidedDependenciesMojo.java From nifi-maven with Apache License 2.0 | 4 votes |
@Override public void execute() throws MojoExecutionException, MojoFailureException { try { // find the nar dependency Artifact narArtifact = null; for (final Artifact artifact : project.getDependencyArtifacts()) { if (NAR.equals(artifact.getType())) { // ensure the project doesn't have two nar dependencies if (narArtifact != null) { throw new MojoExecutionException("Project can only have one NAR dependency."); } // record the nar dependency narArtifact = artifact; } } // ensure there is a nar dependency if (narArtifact == null) { throw new MojoExecutionException("Project does not have any NAR dependencies."); } // build the project for the nar artifact final ProjectBuildingRequest narRequest = new DefaultProjectBuildingRequest(); narRequest.setRepositorySession(repoSession); narRequest.setSystemProperties(System.getProperties()); final ProjectBuildingResult narResult = projectBuilder.build(narArtifact, narRequest); narRequest.setProject(narResult.getProject()); // get the artifact handler for excluding dependencies final ArtifactHandler narHandler = excludesDependencies(narArtifact); narArtifact.setArtifactHandler(narHandler); // nar artifacts by nature includes dependencies, however this prevents the // transitive dependencies from printing using tools like dependency:tree. // here we are overriding the artifact handler for all nars so the // dependencies can be listed. this is important because nar dependencies // will be used as the parent classloader for this nar and seeing what // dependencies are provided is critical. final Map<String, ArtifactHandler> narHandlerMap = new HashMap<>(); narHandlerMap.put(NAR, narHandler); artifactHandlerManager.addHandlers(narHandlerMap); // get the dependency tree final DependencyNode root = dependencyGraphBuilder.buildDependencyGraph(narRequest, null); // write the appropriate output DependencyNodeVisitor visitor = null; if ("tree".equals(mode)) { visitor = new TreeWriter(); } else if ("pom".equals(mode)) { visitor = new PomWriter(); } // ensure the mode was specified correctly if (visitor == null) { throw new MojoExecutionException("The specified mode is invalid. Supported options are 'tree' and 'pom'."); } // visit and print the results root.accept(visitor); getLog().info("--- Provided NAR Dependencies ---\n\n" + visitor.toString()); } catch (ProjectBuildingException | DependencyGraphBuilderException e) { throw new MojoExecutionException("Cannot build project dependency tree", e); } }