How do I instantiate MavenResolver for ModelBuilder usage?

This is a continuation of this answer.

I'm trying to access a pom.xml's full effective pom programmatically by using the maven-model-builder. I'm currently blocked by the fact that I need to create a MavenResolver object. I have found DefaultMavenResolver but it has restricted access.

I'm also in a project that is not a maven plugin. It would be great if I can instantiate this without executing maven as well.

Answers


Actually that was pretty simple (I used Scala, but that doesn't really matter):

import java.io.File

import org.apache.maven.model.Dependency
import org.apache.maven.model.building.{DefaultModelBuilderFactory, DefaultModelBuildingRequest, ModelBuildingRequest}

object POMParser {

  def parse(pomXml: File): List[Dependency] = {
    val modelBuilder = new DefaultModelBuilderFactory().newInstance()
    val req = new DefaultModelBuildingRequest()
    req.setPomFile(pomXml)
    req.setProcessPlugins(false)
    req.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL)
    val model = modelBuilder.build(req).getEffectiveModel
    import collection.JavaConverters._
    model.getDependencies.asScala
  }

}

This will return the effective model with all dependencies populated, and all placeholders from the properties of the parent POM file are resolved too.

Hope that helps anybody who has the same problem as OP.


Need Your Help

layoutSubviews in infinite loop on iOS 8 GM

ios uitableview ios8 infinite-loop layoutsubviews

My app subclasses a UITableViewCell and implements layoutSubviews to modify the cell's contentView's width, like so: