问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/27383357/how-do-i-instantiate-mavenresolver-for-modelbuilder-usage