Search in sources :

Example 1 with ModelBuildingResult

use of org.apache.maven.model.building.ModelBuildingResult in project buck by facebook.

the class Resolver method loadPomModel.

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);
    }
}
Also used : ModelBuildingResult(org.apache.maven.model.building.ModelBuildingResult) DefaultModelBuildingRequest(org.apache.maven.model.building.DefaultModelBuildingRequest) ModelBuildingException(org.apache.maven.model.building.ModelBuildingException)

Example 2 with ModelBuildingResult

use of org.apache.maven.model.building.ModelBuildingResult in project bazel by bazelbuild.

the class DefaultModelResolver method getEffectiveModel.

public Model getEffectiveModel(ModelSource modelSource, EventHandler handler) {
    DefaultModelBuildingRequest request = new DefaultModelBuildingRequest();
    request.setModelResolver(this);
    request.setModelSource(modelSource);
    Model model;
    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;
}
Also used : ModelBuildingResult(org.apache.maven.model.building.ModelBuildingResult) DefaultModelBuildingRequest(org.apache.maven.model.building.DefaultModelBuildingRequest) Model(org.apache.maven.model.Model) ModelBuildingException(org.apache.maven.model.building.ModelBuildingException)

Example 3 with ModelBuildingResult

use of org.apache.maven.model.building.ModelBuildingResult 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 4 with ModelBuildingResult

use of org.apache.maven.model.building.ModelBuildingResult in project bazel by bazelbuild.

the class DefaultModelResolver method getRawModel.

public Model getRawModel(FileModelSource fileModelSource, EventHandler handler) {
    DefaultModelBuildingRequest request = new DefaultModelBuildingRequest();
    request.setModelResolver(this);
    request.setModelSource(fileModelSource);
    Model model;
    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;
}
Also used : ModelBuildingResult(org.apache.maven.model.building.ModelBuildingResult) DefaultModelBuildingRequest(org.apache.maven.model.building.DefaultModelBuildingRequest) Model(org.apache.maven.model.Model) ModelBuildingException(org.apache.maven.model.building.ModelBuildingException)

Example 5 with ModelBuildingResult

use of org.apache.maven.model.building.ModelBuildingResult in project buck by facebook.

the class Pom method constructModel.

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);
    }
}
Also used : ModelBuilder(org.apache.maven.model.building.ModelBuilder) 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) Model(org.apache.maven.model.Model) ModelBuildingException(org.apache.maven.model.building.ModelBuildingException)

Aggregations

DefaultModelBuildingRequest (org.apache.maven.model.building.DefaultModelBuildingRequest)5 ModelBuildingException (org.apache.maven.model.building.ModelBuildingException)5 ModelBuildingResult (org.apache.maven.model.building.ModelBuildingResult)5 Model (org.apache.maven.model.Model)4 ModelBuildingRequest (org.apache.maven.model.building.ModelBuildingRequest)2 IOException (java.io.IOException)1 Properties (java.util.Properties)1 ProjectParseException (meghanada.project.ProjectParseException)1 Build (org.apache.maven.model.Build)1 DefaultModelBuilder (org.apache.maven.model.building.DefaultModelBuilder)1 DefaultModelBuilderFactory (org.apache.maven.model.building.DefaultModelBuilderFactory)1 ModelBuilder (org.apache.maven.model.building.ModelBuilder)1