How do I instantiate MavenResolver for ModelBuilder usage?

≯℡__Kan透↙ 提交于 2019-12-11 07:53:09

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!