List of usage examples for org.apache.maven.model.building DefaultModelBuildingRequest DefaultModelBuildingRequest
public DefaultModelBuildingRequest()
From source file:biz.itcf.maven.plugins.dependencytransfer.TransferMojo.java
License:Apache License
private ArtifactResult resolveParent(Artifact pomArtifact) throws MojoFailureException { try {//from w ww . ja va 2 s. com ModelBuildingRequest buildingRequest = new DefaultModelBuildingRequest(); buildingRequest.setModelResolver(new AvailableModelResolver(repoSession, null, null, artifactResolver, remoteRepositoryManager, projectRepos)); buildingRequest.setPomFile(pomArtifact.getFile()); buildingRequest.setTwoPhaseBuilding(false); buildingRequest.setSystemProperties(System.getProperties()); buildingRequest.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL); ModelBuildingResult build = modelBuilder.build(buildingRequest); Parent dependencyParent = build.getRawModel().getParent(); if (dependencyParent != null) { ArtifactRequest parentArtifactRequest = new ArtifactRequest(); parentArtifactRequest.setArtifact(new DefaultArtifact(dependencyParent.getGroupId(), dependencyParent.getArtifactId(), "pom", dependencyParent.getVersion())); parentArtifactRequest.setRepositories(projectRepos); return repoSystem.resolveArtifact(repoSession, parentArtifactRequest); } else { return null; } } catch (ArtifactResolutionException e) { throw new MojoFailureException("Could not resolve parent artifact.", e); } catch (ModelBuildingException e) { throw new MojoFailureException("Could not build Maven model for given artifact.", e); } }
From source file:com.bennavetta.util.tycho.maven.Maven.java
License:Apache License
public static Model createModel(File pom) throws ModelBuildingException, ComponentLookupException { ModelBuilder builder = mavenContainer.lookup(ModelBuilder.class); ModelBuildingRequest req = new DefaultModelBuildingRequest(); req.setProcessPlugins(false);//from www. j ava2 s .co m req.setModelResolver(new RepoModelResolver()); req.setPomFile(pom); return builder.build(req).getEffectiveModel(); }
From source file:com.buschmais.jqassistant.plugin.m2repo.impl.scanner.EffectiveModelBuilder.java
@Override public Model getModel(final File pomFile) throws IOException { DefaultModelBuilder builder = new DefaultModelBuilderFactory().newInstance(); ModelBuildingRequest req = new DefaultModelBuildingRequest(); req.setProcessPlugins(false);/*from ww w . j a v a2 s . c o m*/ req.setPomFile(pomFile); req.setModelResolver(modelResolver); req.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL); req.setSystemProperties(System.getProperties()); builder.setModelValidator(new ModelValidatorImpl()); try { return builder.build(req).getEffectiveModel(); } catch (ModelBuildingException e) { LOGGER.warn("Cannot build effective model for " + pomFile.getAbsolutePath(), e); return new EffectiveModel(rawModelBuilder.getModel(pomFile)); } }
From source file:com.facebook.buck.maven.Pom.java
License:Apache License
private Model constructModel() throws IOException { File file = path.toFile();// w ww . ja va2 s . c o m if (file.isFile()) { ModelBuildingRequest modelBuildingRequest = new DefaultModelBuildingRequest().setPomFile(file); ModelBuilder modelBuilder = MODEL_BUILDER_FACTORY.newInstance(); try { ModelBuildingResult modelBuildingResult = modelBuilder.build(modelBuildingRequest); // Would contain extra stuff: <build/>, <repositories/>, <pluginRepositories/>, <reporting/> // model = modelBuildingResult.getEffectiveModel(); return Preconditions.checkNotNull(modelBuildingResult.getRawModel()); } catch (ModelBuildingException e) { throw Throwables.propagate(e); } } else { Model model = new Model(); model.setModelVersion(POM_MODEL_VERSION); return model; } }
From source file:com.github.nethad.clustermeister.provisioning.dependencymanager.MavenRepositorySystem.java
License:Apache License
/** * Resolve the effective Maven model (pom) for a POM file. * /*from ww w .j av a 2 s. co m*/ * This resolves the POM hierarchy (parents and modules) and creates an * overall model. * * @param pom the POM file to resolve. * @return the effective model. */ protected Model getEffectiveModel(File pom) { ModelBuildingRequest req = new DefaultModelBuildingRequest(); req.setProcessPlugins(false); req.setPomFile(pom); req.setModelResolver(new SimpleModelResolver(repositorySystem, createSession(), getRepositories(Collections.EMPTY_LIST))); req.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL); ModelBuilder builder = new DefaultModelBuilderFactory().newInstance(); try { return builder.build(req).getEffectiveModel(); } catch (ModelBuildingException ex) { logger.warn("Could not build maven model.", ex); } return new Model(); }
From source file:com.google.devtools.build.workspace.maven.DefaultModelResolver.java
License:Open Source License
public Model getEffectiveModel(ModelSource modelSource, EventHandler handler) { DefaultModelBuildingRequest request = new DefaultModelBuildingRequest(); request.setModelResolver(this); request.setModelSource(modelSource); Model model;//from w ww .j av a 2s . co m try { ModelBuildingResult result = modelBuilder.build(request); model = result.getEffectiveModel(); } catch (ModelBuildingException | IllegalArgumentException e) { // IllegalArg can be thrown if the parent POM cannot be resolved. handler.handle(Event.error( "Unable to resolve Maven model from " + modelSource.getLocation() + ": " + e.getMessage())); return null; } return model; }
From source file:com.google.devtools.build.workspace.maven.DefaultModelResolver.java
License:Open Source License
public Model getRawModel(FileModelSource fileModelSource, EventHandler handler) { DefaultModelBuildingRequest request = new DefaultModelBuildingRequest(); request.setModelResolver(this); request.setModelSource(fileModelSource); Model model;// ww w.jav a2 s .c o m try { ModelBuildingResult result = modelBuilder.build(request); model = result.getRawModel(); } catch (ModelBuildingException | IllegalArgumentException e) { // IllegalArg can be thrown if the parent POM cannot be resolved. handler.handle(Event.error("Unable to resolve raw Maven model from " + fileModelSource.getLocation() + ": " + e.getMessage())); return null; } return model; }
From source file:com.isomorphic.maven.mojo.AbstractPackagerMojo.java
License:Apache License
/** * Read the given POM so it can be used as the source of coordinates, etc. during artifact construction. Note that * if this object's {@link #snapshots} property is true, and we're working with a development build ({@link #buildNumber} * ends with 'd'), the POM is modified to remove the SNAPSHOT qualifier. * //from ww w . ja va 2 s. c o m * @param pom the POM file containing the artifact metadata * @return A Maven model to be used at {@link com.isomorphic.maven.packaging.Module#Module(Model)} Module construction * @throws ModelBuildingException if the Model cannot be built from the given POM * @throws IOException if the Model cannot be built from the given POM */ private Model getModelFromFile(File pom) throws ModelBuildingException, IOException { if (buildNumber.endsWith("d") && !snapshots) { LOGGER.info("Rewriting file to remove SNAPSHOT qualifier from development POM '{}'", pom.getName()); String content = FileUtils.readFileToString(pom); content = content.replaceAll("-SNAPSHOT", ""); FileUtils.write(pom, content); } ModelBuildingRequest request = new DefaultModelBuildingRequest(); request.setPomFile(pom); ModelBuildingResult result = modelBuilder.build(request); return result.getEffectiveModel(); }
From source file:com.netflix.nebula.lint.rule.dependency.provider.MavenBomRecommendationProvider.java
License:Apache License
private Map<String, String> getMavenRecommendations() throws UnresolvableModelException, FileNotFoundException, ModelBuildingException { Map<String, String> recommendations = new HashMap<>(); Set<File> recommendationFiles = getBomsOnConfiguration(); for (File recommendation : recommendationFiles) { if (!recommendation.getName().endsWith("pom")) { break; }/* w w w . j a va2 s .c o m*/ DefaultModelBuildingRequest request = new DefaultModelBuildingRequest(); request.setModelResolver(new ModelResolver() { @Override public ModelSource2 resolveModel(String groupId, String artifactId, String version) throws UnresolvableModelException { String notation = groupId + ":" + artifactId + ":" + version + "@pom"; org.gradle.api.artifacts.Dependency dependency = project.getDependencies().create(notation); org.gradle.api.artifacts.Configuration configuration = project.getConfigurations() .detachedConfiguration(dependency); try { File file = configuration.getFiles().iterator().next(); return new SimpleModelSource(new java.io.FileInputStream(file)); } catch (Exception e) { throw new UnresolvableModelException(e, groupId, artifactId, version); } } @Override public ModelSource2 resolveModel(Dependency dependency) throws UnresolvableModelException { return resolveModel(dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion()); } @Override public ModelSource2 resolveModel(Parent parent) throws UnresolvableModelException { return resolveModel(parent.getGroupId(), parent.getArtifactId(), parent.getVersion()); } @Override public void addRepository(Repository repository) throws InvalidRepositoryException { // do nothing } @Override public void addRepository(Repository repository, boolean bool) throws InvalidRepositoryException { // do nothing } @Override public ModelResolver newCopy() { return this; // do nothing } }); request.setModelSource(new SimpleModelSource(new java.io.FileInputStream(recommendation))); request.setSystemProperties(System.getProperties()); DefaultModelBuilder modelBuilder = new DefaultModelBuilderFactory().newInstance(); modelBuilder.setModelInterpolator(new ProjectPropertiesModelInterpolator(project)); ModelBuildingResult result = modelBuilder.build(request); log.info("using bom: " + result.getEffectiveModel().getId()); Model model = result.getEffectiveModel(); if (model == null) { break; } org.apache.maven.model.DependencyManagement dependencyManagement = model.getDependencyManagement(); if (dependencyManagement == null) { break; } for (Dependency d : dependencyManagement.getDependencies()) { recommendations.put(d.getGroupId() + ":" + d.getArtifactId(), d.getVersion()); // overwrites previous values } } return recommendations; }
From source file:com.redhat.rcm.version.mgr.VersionManager.java
License:Open Source License
private synchronized ModelBuildingRequest newModelBuildingRequest(final File pom, final VersionManagerSession session) { if (baseMbr == null) { final DefaultModelBuildingRequest mbr = new DefaultModelBuildingRequest(); mbr.setSystemProperties(System.getProperties()); mbr.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL); mbr.setProcessPlugins(false);/*from w w w .j ava 2 s . c o m*/ mbr.setLocationTracking(true); this.baseMbr = mbr; } final DefaultModelBuildingRequest req = new DefaultModelBuildingRequest(baseMbr); req.setModelSource(new FileModelSource(pom)); final VManModelResolver resolver = new VManModelResolver(session, pom, artifactResolver, remoteRepositoryManager); req.setModelResolver(resolver); return req; }