Search in sources :

Example 1 with DefaultModelBuilder

use of org.apache.maven.model.building.DefaultModelBuilder in project meghanada-server by mopemope.

the class POMParser method parsePom.

POMInfo parsePom(File pom) throws ProjectParseException {
    try {
        if (!pom.isAbsolute()) {
            pom = pom.getCanonicalFile();
        }
        ModelBuildingRequest req = new DefaultModelBuildingRequest();
        req.setPomFile(pom);
        req.setSystemProperties(System.getProperties());
        req.setModelResolver(new LocalModelResolver(this.projectRoot));
        req.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL);
        req.setProcessPlugins(true);
        DefaultModelBuilderFactory factory = new DefaultModelBuilderFactory();
        DefaultModelBuilder builder = factory.newInstance();
        ModelBuildingResult result = builder.build(req);
        Model mavenModel = result.getEffectiveModel();
        POMInfo pomInfo = new POMInfo(pom.getParent());
        String groupId = mavenModel.getGroupId();
        if (nonNull(groupId)) {
            pomInfo.groupId = groupId;
        }
        String artifactId = mavenModel.getArtifactId();
        if (nonNull(artifactId)) {
            pomInfo.artifactId = artifactId;
        }
        String version = mavenModel.getVersion();
        if (nonNull(version)) {
            pomInfo.version = version;
        }
        Properties modelProperties = mavenModel.getProperties();
        if (nonNull(modelProperties)) {
            if (nonNull(groupId)) {
                modelProperties.put("project.groupId", groupId);
            }
            if (nonNull(artifactId)) {
                modelProperties.put("project.artifactId", artifactId);
            }
            if (nonNull(version)) {
                modelProperties.put("project.version", version);
            }
            for (String key : modelProperties.stringPropertyNames()) {
                String value = modelProperties.getProperty(key);
                if (nonNull(value)) {
                    pomInfo.properties.setProperty(key, value);
                }
            }
            POMParser.replacePOMProperties(pomInfo);
        }
        Build build = mavenModel.getBuild();
        this.parseBuild(pomInfo, build);
        return pomInfo;
    } catch (IOException | ModelBuildingException e) {
        throw new ProjectParseException(e);
    }
}
Also used : ModelBuildingRequest(org.apache.maven.model.building.ModelBuildingRequest) DefaultModelBuildingRequest(org.apache.maven.model.building.DefaultModelBuildingRequest) ModelBuildingResult(org.apache.maven.model.building.ModelBuildingResult) DefaultModelBuildingRequest(org.apache.maven.model.building.DefaultModelBuildingRequest) IOException(java.io.IOException) Properties(java.util.Properties) DefaultModelBuilder(org.apache.maven.model.building.DefaultModelBuilder) DefaultModelBuilderFactory(org.apache.maven.model.building.DefaultModelBuilderFactory) ProjectParseException(meghanada.project.ProjectParseException) Build(org.apache.maven.model.Build) Model(org.apache.maven.model.Model) ModelBuildingException(org.apache.maven.model.building.ModelBuildingException)

Example 2 with DefaultModelBuilder

use of org.apache.maven.model.building.DefaultModelBuilder in project spring-boot by spring-projects.

the class DependencyManagementBomTransformation method updateDependencyResolutionContext.

private void updateDependencyResolutionContext(List<Map<String, String>> bomDependencies) {
    URI[] uris = Grape.getInstance().resolve(null, bomDependencies.toArray(new Map<?, ?>[0]));
    DefaultModelBuilder modelBuilder = new DefaultModelBuilderFactory().newInstance();
    for (URI uri : uris) {
        try {
            DefaultModelBuildingRequest request = new DefaultModelBuildingRequest();
            request.setModelResolver(new GrapeModelResolver());
            request.setModelSource(new org.apache.maven.model.building.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);
        }
    }
}
Also used : DefaultModelBuildingRequest(org.apache.maven.model.building.DefaultModelBuildingRequest) URI(java.net.URI) DefaultModelBuilder(org.apache.maven.model.building.DefaultModelBuilder) InvalidRepositoryException(org.apache.maven.model.resolution.InvalidRepositoryException) UnresolvableModelException(org.apache.maven.model.resolution.UnresolvableModelException) MalformedURLException(java.net.MalformedURLException) SyntaxException(org.codehaus.groovy.syntax.SyntaxException) DefaultModelBuilderFactory(org.apache.maven.model.building.DefaultModelBuilderFactory) MavenModelDependencyManagement(org.springframework.boot.cli.compiler.dependencies.MavenModelDependencyManagement) Model(org.apache.maven.model.Model) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Model (org.apache.maven.model.Model)2 DefaultModelBuilder (org.apache.maven.model.building.DefaultModelBuilder)2 DefaultModelBuilderFactory (org.apache.maven.model.building.DefaultModelBuilderFactory)2 DefaultModelBuildingRequest (org.apache.maven.model.building.DefaultModelBuildingRequest)2 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 URI (java.net.URI)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Properties (java.util.Properties)1 ProjectParseException (meghanada.project.ProjectParseException)1 Build (org.apache.maven.model.Build)1 ModelBuildingException (org.apache.maven.model.building.ModelBuildingException)1 ModelBuildingRequest (org.apache.maven.model.building.ModelBuildingRequest)1 ModelBuildingResult (org.apache.maven.model.building.ModelBuildingResult)1 InvalidRepositoryException (org.apache.maven.model.resolution.InvalidRepositoryException)1 UnresolvableModelException (org.apache.maven.model.resolution.UnresolvableModelException)1 SyntaxException (org.codehaus.groovy.syntax.SyntaxException)1 MavenModelDependencyManagement (org.springframework.boot.cli.compiler.dependencies.MavenModelDependencyManagement)1