简体   繁体   English

如何实例化MavenResolver以供ModelBuilder使用?

[英]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 . 我正在尝试通过使用maven-model-builder来以编程方式访问pom.xml的完整有效pom。 I'm currently blocked by the fact that I need to create a MavenResolver object. 我目前被需要创建MavenResolver对象的事实所MavenResolver I have found DefaultMavenResolver but it has restricted access. 我已经找到DefaultMavenResolver但是访问受到限制。

I'm also in a project that is not a maven plugin. 我也在一个不是Maven插件的项目中。 It would be great if I can instantiate this without executing maven as well. 如果我也可以在不执行Maven的情况下实例化它,那就太好了。

Actually that was pretty simple (I used Scala, but that doesn't really matter): 实际上,这非常简单(我使用了Scala,但这并不重要):

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. 这将返回填充了所有依赖项的有效模型,并且父POM文件的属性中的所有占位符也会被解析。

Hope that helps anybody who has the same problem as OP. 希望对任何与OP有相同问题的人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM