private void updateDependencyResolutionContext( List<Map<String, String>> bomDependencies) { URI[] uris = Grape.getInstance().resolve(null, bomDependencies.toArray(new Map[bomDependencies.size()])); DefaultModelBuilder modelBuilder = new DefaultModelBuilderFactory().newInstance(); for (URI uri : uris) { try { DefaultModelBuildingRequest request = new DefaultModelBuildingRequest(); request.setModelResolver(new GrapeModelResolver()); request.setModelSource(new UrlModelSource(uri.toURL())); request.setSystemProperties(System.getProperties()); Model model = modelBuilder.build(request).getEffectiveModel(); this.resolutionContext.addDependencyManagement( new MavenModelDependencyManagement(model)); } catch (Exception ex) { throw new IllegalStateException("Failed to build model for '" + uri + "'. Is it a valid Maven bom?", ex); } } }
public void testLocationTrackerShouldBeExcludedFromInterpolation() { Properties props = new Properties(); props.setProperty( "expression", "value" ); DefaultModelBuildingRequest request = new DefaultModelBuildingRequest(); request.setUserProperties( props ); InputSource source = new InputSource(); source.setLocation( "${expression}" ); source.setModelId( "${expression}" ); Model model = new Model(); model.setLocation( "", new InputLocation( 1, 1, source ) ); SimpleProblemCollector problems = new SimpleProblemCollector(); StringSearchModelInterpolator interpolator = new StringSearchModelInterpolator(); interpolator.interpolateObject( model, model, null, request, problems ); assertProblemFree( problems ); assertEquals( "${expression}", source.getLocation() ); assertEquals( "${expression}", source.getModelId() ); }
/**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(); }
@Bean public ModelBuildingRequest modelBuildingRequestTemplate(RepositorySystemSession repositorySystemSession, LocalRepositoryModelResolver localRepositoryModelResolver) { Properties userProperties = new Properties(); userProperties.putAll(repositorySystemSession.getUserProperties()); Properties systemProperties = new Properties(); systemProperties.putAll(repositorySystemSession.getSystemProperties()); DefaultModelBuildingRequest request = new DefaultModelBuildingRequest(); request.setProcessPlugins(true); request.setLocationTracking(true); request.setModelResolver(localRepositoryModelResolver); request.setUserProperties(userProperties); request.setSystemProperties(systemProperties); return request; }
/** * * TODO: This does not currently work. * */ @SuppressWarnings("unused") private static Model complexModelBuilding(File file) throws Exception { ModelBuilder builder = new DefaultModelBuilder(); ModelBuildingRequest request = new DefaultModelBuildingRequest(); request.setProcessPlugins(false); request.setPomFile(file); // request.setModelResolver(new ModelResolver() {}); request.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL); //noinspection UnnecessaryLocalVariable Model model = builder.build(request).getEffectiveModel(); return model; }
private ModelBuildingRequest createModelBuildingRequest( File pomFile ) { FlattenModelResolver resolver = new FlattenModelResolver( this.localRepository, this.artifactFactory, this.dependencyResolver, this.session.getProjectBuildingRequest(), this.session.getAllProjects() ); Properties userProperties = this.session.getUserProperties(); List<String> activeProfiles = this.session.getRequest().getActiveProfiles(); // @formatter:off ModelBuildingRequest buildingRequest = new DefaultModelBuildingRequest().setUserProperties( userProperties ).setSystemProperties( System.getProperties() ).setPomFile( pomFile ).setModelResolver( resolver ).setActiveProfileIds( activeProfiles ); // @formatter:on return buildingRequest; }
/** * Tests method to create effective POM. * * @throws Exception if something goes wrong. */ @Test public void testCreateEffectivePom() throws Exception { String magicValue = "magic-value"; Properties userProperties = new Properties(); userProperties.setProperty( "cmd.test.property", magicValue ); File pomFile = new File( "src/test/resources/cmdpropertysubstituion/pom.xml" ); ArtifactRepository localRepository = new MavenArtifactRepository(); localRepository.setLayout( new DefaultRepositoryLayout() ); ArtifactFactory artifactFactory = new DefaultArtifactFactory(); ArtifactHandlerManager artifactHandlerManager = new DefaultArtifactHandlerManager(); setDeclaredField( artifactFactory, "artifactHandlerManager", artifactHandlerManager ); Map<String, ArtifactHandler> artifactHandlers = new HashMap<String, ArtifactHandler>(); setDeclaredField( artifactHandlerManager, "artifactHandlers", artifactHandlers ); DefaultDependencyResolver depencencyResolver = new DefaultDependencyResolver(); DefaultProjectBuildingRequest projectBuildingRequest = new DefaultProjectBuildingRequest(); FlattenModelResolver resolver = new FlattenModelResolver( localRepository, artifactFactory, depencencyResolver, projectBuildingRequest, Collections.<MavenProject>emptyList() ); ModelBuildingRequest buildingRequest = new DefaultModelBuildingRequest().setPomFile( pomFile ).setModelResolver( resolver ).setUserProperties( userProperties ); Model effectivePom = FlattenMojo.createEffectivePom( buildingRequest, false ); assertThat( effectivePom.getName() ).isEqualTo( magicValue ); }
public static Model createModel(File pom) throws ModelBuildingException, ComponentLookupException { ModelBuilder builder = mavenContainer.lookup(ModelBuilder.class); ModelBuildingRequest req = new DefaultModelBuildingRequest(); req.setProcessPlugins(false); req.setModelResolver(new RepoModelResolver()); req.setPomFile(pom); return builder.build(req).getEffectiveModel(); }
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(); }
private SimpleProblemCollector validateEffective( String pom, int level ) throws Exception { ModelBuildingRequest request = new DefaultModelBuildingRequest().setValidationLevel( level ); SimpleProblemCollector problems = new SimpleProblemCollector(); validator.validateEffectiveModel( read( pom ), request, problems ); return problems; }
private SimpleProblemCollector validateRaw( String pom, int level ) throws Exception { ModelBuildingRequest request = new DefaultModelBuildingRequest().setValidationLevel( level ); SimpleProblemCollector problems = new SimpleProblemCollector(); validator.validateRawModel( read( pom ), request, problems ); return problems; }
private ModelBuildingRequest createModelBuildingRequest( Properties p ) { ModelBuildingRequest config = new DefaultModelBuildingRequest(); if ( p != null ) { config.setSystemProperties( p ); } return config; }
public void testFinalFieldsExcludedFromInterpolation() { Properties props = new Properties(); props.setProperty( "expression", "value" ); DefaultModelBuildingRequest request = new DefaultModelBuildingRequest(); request.setUserProperties( props ); SimpleProblemCollector problems = new SimpleProblemCollector(); StringSearchModelInterpolator interpolator = new StringSearchModelInterpolator(); interpolator.interpolateObject( new ClassWithFinalField(), new Model(), null, request, problems ); assertProblemFree( problems ); }
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; }
private void validate(ValidatorContext ctx, File pomFile) { DefaultModelBuildingRequest request = new DefaultModelBuildingRequest(modelBuildingRequestTemplate); request.setPomFile(pomFile); request.setModelSource(new FileModelSource(pomFile)); request.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MAVEN_3_0); try { modelBuilder.build(request); } catch (ModelBuildingException e) { ctx.addError(this, pomFile, e); } }
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(); }
private ModelBuildingRequest createModelBuildingRequest( Properties p ) { ModelBuildingRequest config = new DefaultModelBuildingRequest(); config.setSystemProperties( p ); return config; }
public ModelValidationResult validate( Model model ) { ModelValidationResult result = new ModelValidationResult(); ModelBuildingRequest request = new DefaultModelBuildingRequest().setValidationLevel( ModelBuildingRequest.VALIDATION_LEVEL_MAVEN_2_0 ); SimpleModelProblemCollector problems = new SimpleModelProblemCollector( result ); modelValidator.validateEffectiveModel( model, request, problems ); return result; }