使用DefaultModelBuilder构建有效模型。建立()

时间:2012-01-16 23:40:42

标签: maven maven-3

我正在尝试为项目获得一个有效的模型,目前在Maven Core API 3.0.3(或3.0.4)下有一个名为build()的方法,它很整洁但是

它需要太多像ModelNormalizer,ProfileInjector等,因为它不会在构建时抛出空指针异常。在构建有效模型之前需要初始化的东西太多了,我拥有的所有信息都是pom.xml的文件路径

任何人都有这个工作吗?

3 个答案:

答案 0 :(得分:1)

Here is sample code:

    DefaultModelBuilder modelBuilder = new DefaultModelBuilderFactory().newInstance();

    ModelBuildingRequest req = new DefaultModelBuildingRequest();
    req.setProcessPlugins(false);
    req.setPomFile(file);
    req.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL);
    req.setModelResolver(new MyLocalModelResolver());

    Model model = modelBuilder.build(req).getEffectiveModel();

答案 1 :(得分:0)

也许这个图书馆可以提供帮助:https://github.com/jenkinsci/lib-jenkins-maven-embedder

或至少作为样本。

HTH

奥利弗

答案 2 :(得分:0)

是的,它对我有用。我必须使用默认值设置所有数据。非常烦人......

我为

创建了一个MavenModelBuilder构建器类
/**
 * Default MavenModelBuilder.
 */
public class MavenModelBuilder  extends DefaultModelBuilder {

  /**
   * Constructor
   */
  public MavenModelBuilder() {
    super();
    ModelProcessor modelProcessor = new DefaultModelProcessor()
        .setModelLocator(new DefaultModelLocator())
        .setModelReader(new DefaultModelReader());

    ModelInterpolator modelInterpolator = new StringSearchModelInterpolator()
        .setPathTranslator(new DefaultPathTranslator())
        .setUrlNormalizer(new DefaultUrlNormalizer());

    setProfileSelector(new DefaultProfileSelector());
    setModelProcessor(modelProcessor);
    setModelValidator(new DefaultModelValidator());
    setSuperPomProvider(new DefaultSuperPomProvider().setModelProcessor(modelProcessor));
    setModelNormalizer(new DefaultModelNormalizer());
    setInheritanceAssembler(new DefaultInheritanceAssembler());
    setModelInterpolator(modelInterpolator);
    setModelUrlNormalizer(new DefaultModelUrlNormalizer().setUrlNormalizer(new DefaultUrlNormalizer()));
    setModelPathTranslator(new DefaultModelPathTranslator().setPathTranslator(new DefaultPathTranslator()));
    setPluginManagementInjector(new DefaultPluginManagementInjector());
    setLifecycleBindingsInjector(new DefaultLifecycleBindingsInjector());
    setDependencyManagementInjector(new DefaultDependencyManagementInjector());
    setReportConfigurationExpander(new DefaultReportConfigurationExpander());
    setReportingConverter(new DefaultReportingConverter());
    setPluginConfigurationExpander(new DefaultPluginConfigurationExpander());
    setDependencyManagementImporter(new DefaultDependencyManagementImporter());
    setProfileInjector(new DefaultProfileInjector());
  }

}

我称之为

  /**
   * Parse the xml file to get the model of the xml file.
   *
   * @param path of the project
   * @throws IllegalArgumentException : exception
   */
  public static Model parseFile(String path) throws IllegalArgumentException {
    DefaultModelBuilder builder = new MavenModelBuilder();

    ModelBuildingRequest req = new DefaultModelBuildingRequest();
    req.setProcessPlugins(false);
    req.setPomFile(new File(path));
    req.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL);

    Model model = null;
    try {
      model = builder.build(req).getEffectiveModel();
    } catch (ModelBuildingException e) {
      e.printStackTrace();
    }

    return model;
  }