/** * Creates a list of POM models in an inheritance lineage. * Each resulting model is "raw", so contains no interpolation or inheritance. * In particular beware that groupId and/or version may be null if inherited from a parent; use {@link Model#getParent} to resolve. * Internally calls <code>executeModelBuilder</code> so if you need to call both just use the execute method. * @param pom a POM to inspect * @param embedder an embedder to use * @return a list of models, starting with the specified POM, going through any parents, finishing with the Maven superpom (with a null artifactId) * @throws ModelBuildingException if the POM or parents could not even be parsed; warnings are not reported */ public List<Model> createModelLineage(File pom) throws ModelBuildingException { ModelBuildingResult res = executeModelBuilder(pom); List<Model> toRet = new ArrayList<Model>(); for (String id : res.getModelIds()) { Model m = res.getRawModel(id); normalizePath(m); toRet.add(m); } // for (ModelProblem p : res.getProblems()) { // System.out.println("problem=" + p); // if (p.getException() != null) { // p.getException().printStackTrace(); // } // } return toRet; }
@Override public ModelBuildingResult build(ModelBuildingRequest request) throws ModelBuildingException { ModelBuildingResult toRet = super.build(request); Model eff = toRet.getEffectiveModel(); InputSource source = new InputSource(); source.setLocation(""); InputLocation location = new InputLocation(-1, -1, source); eff.setLocation(NETBEANS_PROFILES, location); for (String id : toRet.getModelIds()) { Model mdl = toRet.getRawModel(id); for (Profile p : mdl.getProfiles()) { source.setLocation(source.getLocation() + "|" + p.getId()); } } return toRet; }
private String findProfilesXml( ModelBuildingResult result, Map<File, Boolean> profilesXmls ) { for ( String modelId : result.getModelIds() ) { Model model = result.getRawModel( modelId ); File basedir = model.getProjectDirectory(); if ( basedir == null ) { break; } Boolean profilesXml = profilesXmls.get( basedir ); if ( profilesXml == null ) { profilesXml = Boolean.valueOf( new File( basedir, "profiles.xml" ).exists() ); profilesXmls.put( basedir, profilesXml ); } if ( profilesXml.booleanValue() ) { return modelId; } } return null; }
/**f * 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. * * @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(); }
@Override public void validate(ValidatorContext ctx) { Collection<File> pomFiles = listPomFiles(ctx.getValidatedRepository(), fileFilter); for (File pomFile : pomFiles) { if (!ctx.getErrors(pomFile).isEmpty()) { logger.debug("skipping `{}`, because already contains exceptions", pomFile); continue; } ModelBuildingResult result = validatorSupport.buildModel(pomFile); Model rawModel = result.getRawModel(); Model effectiveModel = result.getEffectiveModel(); if (bomFilter.isBom(effectiveModel)) { validateBomDependenciesVersionProperty(ctx, pomFile, rawModel, effectiveModel); } } }
public Model getRawModel() throws ModelBuildingException { synchronized(MODEL_LOCK) { if(model == null) { MavenEmbedder projectEmbedder = EmbedderFactory.getProjectEmbedder(); ModelBuildingResult br = projectEmbedder.executeModelBuilder(getPOMFile()); model = br.getRawModel(); } return model; } }
/** * * @param pom * @return result object with access to effective pom model and raw models for each parent. * @throws ModelBuildingException if the POM or parents could not even be parsed; warnings are not reported */ public ModelBuildingResult executeModelBuilder(File pom) throws ModelBuildingException { ModelBuilder mb = lookupComponent(ModelBuilder.class); assert mb!=null : "ModelBuilder component not found in maven"; ModelBuildingRequest req = new DefaultModelBuildingRequest(); req.setPomFile(pom); req.setProcessPlugins(false); req.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL); req.setLocationTracking(true); req.setModelResolver(new NBRepositoryModelResolver(this)); req.setSystemProperties(getSystemProperties()); req.setUserProperties(embedderConfiguration.getUserProperties()); return mb.build(req); }
public Model loadPomFromFile(File pomFile, String... profiles) { RepositorySystem system = mavenContainer.getRepositorySystem(); Settings settings = mavenContainer.getSettings(); DefaultRepositorySystemSession session = mavenContainer.setupRepoSession(system, settings); final DefaultModelBuildingRequest request = new DefaultModelBuildingRequest() .setSystemProperties(System.getProperties()) .setPomFile(pomFile) .setActiveProfileIds(settings.getActiveProfiles()); ModelBuilder builder = new DefaultModelBuilderFactory().newInstance(); ModelBuildingResult result; try { request.setModelResolver(new MavenModelResolver(system, session, MavenRepositories.getRemoteRepositories(mavenContainer, settings))); result = builder.build(request); } // wrap exception message catch (ModelBuildingException e) { String pomPath = request.getPomFile().getAbsolutePath(); StringBuilder sb = new StringBuilder("Found ").append(e.getProblems().size()) .append(" problems while building POM model from ").append(pomPath).append("\n"); int counter = 1; for (ModelProblem problem : e.getProblems()) { sb.append(counter++).append("/ ").append(problem).append("\n"); } throw new RuntimeException(sb.toString()); } return result.getEffectiveModel(); }
InterimResult( File pomFile, ModelBuildingRequest request, ModelBuildingResult result, DefaultModelBuildingListener listener, boolean root ) { this.pomFile = pomFile; this.request = request; this.result = result; this.listener = listener; this.root = root; }
private boolean build( List<ProjectBuildingResult> results, List<MavenProject> projects, Map<String, MavenProject> projectIndex, List<InterimResult> interimResults, ProjectBuildingRequest request, Map<File, Boolean> profilesXmls ) { boolean noErrors = true; for ( InterimResult interimResult : interimResults ) { try { ModelBuildingResult result = modelBuilder.build( interimResult.request, interimResult.result ); MavenProject project = interimResult.listener.getProject(); initProject( project, projectIndex, result, profilesXmls ); List<MavenProject> modules = new ArrayList<MavenProject>(); noErrors = build( results, modules, projectIndex, interimResult.modules, request, profilesXmls ) && noErrors; projects.addAll( modules ); projects.add( project ); project.setExecutionRoot( interimResult.root ); project.setCollectedProjects( modules ); results.add( new DefaultProjectBuildingResult( project, result.getProblems(), null ) ); } catch ( ModelBuildingException e ) { results.add( new DefaultProjectBuildingResult( e.getModelId(), interimResult.pomFile, e.getProblems() ) ); noErrors = false; } } return noErrors; }
private Model loadPomModel(Path pomFile) { DefaultModelBuildingRequest request = new DefaultModelBuildingRequest(); request.setPomFile(pomFile.toFile()); try { ModelBuildingResult result = modelBuilder.build(request); return result.getRawModel(); } catch (ModelBuildingException | IllegalArgumentException e) { // IllegalArg can be thrown if the parent POM cannot be resolved. throw new RuntimeException(e); } }
private Model constructModel(File file, Model model) { ModelBuilder modelBuilder = MODEL_BUILDER_FACTORY.newInstance(); try { ModelBuildingRequest req = new DefaultModelBuildingRequest().setPomFile(file); ModelBuildingResult modelBuildingResult = modelBuilder.build(req); Model constructed = Preconditions.checkNotNull(modelBuildingResult.getRawModel()); return merge(model, constructed); } catch (ModelBuildingException e) { throw new RuntimeException(e); } }
public ModelBuildingResult buildModel(File pomFile) { ModelBuildingResult result = null; DefaultModelBuildingRequest request = new DefaultModelBuildingRequest(modelBuildingRequestTemplate); request.setPomFile(pomFile); request.setModelSource(new FileModelSource(pomFile)); try { result = modelBuilder.build(request); } catch (ModelBuildingException e) { result = null; } return result; }
@Override public void validate(ValidatorContext ctx) { Iterator<ModelBuildingResult> modelIterator = validatorSupport.modelIterator(ctx, fileFilter); while (modelIterator.hasNext()) { ModelBuildingResult modelResult = modelIterator.next(); if (modelResult != null) { Model rawModel = modelResult.getRawModel(); Model effectiveModel = modelResult.getEffectiveModel(); if (rawModel != null && effectiveModel != null) { logger.trace("validating {}", relativize(ctx, rawModel.getPomFile())); validateBestPractices(ctx, rawModel, effectiveModel); } } } }
public Model loadPomFromFile(File pomFile, String... profiles) { RepositorySystem system = mavenContainer.getRepositorySystem(); Settings settings = mavenContainer.getSettings(); DefaultRepositorySystemSession session = mavenContainer.setupRepoSession(system, settings); final DefaultModelBuildingRequest request = new DefaultModelBuildingRequest() .setSystemProperties(System.getProperties()) .setPomFile(pomFile) .setActiveProfileIds(settings.getActiveProfiles()); ModelBuilder builder = new DefaultModelBuilderFactory().newInstance(); ModelBuildingResult result; try { request.setModelResolver(new MavenModelResolver(system, session, mavenContainer .getEnabledRepositoriesFromProfile(settings))); result = builder.build(request); } // wrap exception message catch (ModelBuildingException e) { String pomPath = request.getPomFile().getAbsolutePath(); StringBuilder sb = new StringBuilder("Found ").append(e.getProblems().size()) .append(" problems while building POM model from ").append(pomPath).append("\n"); int counter = 1; for (ModelProblem problem : e.getProblems()) { sb.append(counter++).append("/ ").append(problem).append("\n"); } throw new RuntimeException(sb.toString()); } return result.getEffectiveModel(); }