2

我有一个 Maven 扩展,我已经设置为从 POM 配置对象进行配置,其中

<configuration>
  <foo>...</foo>
  <bar>...</bar>
</configuration>

最终打电话

setFoo(...);
setBar(...);

方法。

我想允许配置导入额外的配置以允许提出和第二种委托方式,所以

<configuration>
  <import>g:a:v</import>
  <bar>...</bar>
</configuration>

其中工件 g:a:v 有一个文件META-INF/my-project-name.xml与 content <configuration><foo>...</foo></configuration>

我希望将<configuration>与导入和该 XML 文件相结合,以生成与上述设置器相同的调用。


使用

import org.codehaus.plexus.component.configurator.ComponentConfigurator;
import org.codehaus.plexus.configuration.PlexusConfiguration;
import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.codehaus.plexus.util.xml.Xpp3DomBuilder;

<dependency>
  <groupId>org.codehaus.plexus</groupId>
  <artifactId>plexus-container-default</artifactId>
  <version>1.6</version>
</dependency>
<dependency>
  <groupId>org.codehaus.plexus</groupId>
  <artifactId>plexus-classworlds</artifactId>
  <version>2.5.2</version>
</dependency>

我编写了以下代码来尝试从 XML 文件解析配置,然后调用配置器来配置我的 Maven 扩展。

{
  PlexusConfiguration configuration;
  try {
    configuration = loadConfiguration(
        log, cr.get(), EXTRA_CONFIGURATION_XML_RELATIVE_PATH);
  } catch (IOException ex) {
    throw new EnforcerRuleException(
        "Failed to load " + EXTRA_CONFIGURATION_XML_RELATIVE_PATH
        + " from " + artifactId, ex);
  }

  // TODO: is this right.
  // Newer versions have a MavenProject.getClassRealm() says
  // """
  // Warning: This is an internal utility method that is only public for
  // technical reasons, it is not part of the public API. In particular,
  // this method can be changed or deleted without prior notice and must
  // not be used by plugins.
  // """
  ClassRealm realm = null;

  try {
    configurator.configureComponent(configurable, configuration, realm);
  } catch (ComponentConfigurationException ex) {
    throw new EnforcerRuleException(
        "Failed to process configuration "
        + EXTRA_CONFIGURATION_XML_RELATIVE_PATH
        + " from " + artifactId,
        ex);
  }
}

whereconfigurable只是Objectwith setter 并且configuration是这样XmlPlexusConfiguration加载的:

static XmlPlexusConfiguration loadConfiguration(
    Log log,
    ClassRoot cr,
    String path)
throws EnforcerRuleException, IOException {
  log.debug("Loading " + path + " from " + cr.art.getId());
  File classRootFile = cr.classRoot;
  if (classRootFile == null) {
    throw new EnforcerRuleException(
        "Cannot import configuration from unresolved artifact "
        + art.getId());
  }
  Xpp3Dom dom = cr.readRelativePath(
      path,
      new ClassRoot.IOConsumer<InputStream, Xpp3Dom>() {
        public Xpp3Dom read(InputStream is) throws IOException {
          try {
            return Xpp3DomBuilder.build(is, "UTF-8", true);
          } catch (XmlPullParserException ex) {
            throw new IOException("Malformed XML", ex);
          } finally {
            is.close();
          }
        }
      });
  return new XmlPlexusConfiguration(dom);
}

我获得了ComponentConfigurator通过

  configurator = (ComponentConfigurator) helper.getComponent(
      ComponentConfigurator.class);

当我运行这个时,我得到,

org.codehaus.plexus.component.configurator.ComponentConfigurationException:
Component does not implement interface org.codehaus.plexus.component.MapOrientedComponent
    at org.codehaus.plexus.component.configurator.MapOrientedComponentConfigurator.configureComponent(MapOrientedComponentConfigurator.java:41)
    at org.codehaus.plexus.component.configurator.AbstractComponentConfigurator.configureComponent(AbstractComponentConfigurator.java:44)
    at org.codehaus.plexus.component.configurator.AbstractComponentConfigurator.configureComponent(AbstractComponentConfigurator.java:37)
    at com.google.security.fences.ConfigurationImport.configure(ConfigurationImport.java:70)
    at com.google.security.fences.FencesMavenEnforcerRule.execute(FencesMavenEnforcerRule.java:146)
    at org.apache.maven.plugins.enforcer.EnforceMojo.execute(EnforceMojo.java:193)

有什么方法可以连接 MapOrientedComponents 和用于配置我的扩展的 bean 样式反射设置器调用?

或者有没有更好的方法将 XML 文件的文本转化为额外的配置操作?


编辑:

经过多一点挖掘后,它看起来像

configurator = (ComponentConfigurator) helper.getComponent(
    ComponentConfigurator.class);

MapOrientedComponentConfigurator当我通过导致失败的集成测试运行它时返回 a Verifier,但是当我不这样做时,它会产生一种不同且兼容的配置器。

100% 可重复的区别在于我是否运行-X以使记录器产生调试跟踪。

4

1 回答 1

1

我不知道根本原因,但我知道

  1. 运行 with-X打开调试时,helper.getComponent(ComponentConfigurator.class)返回 aBasicComponentConfigurator并且一切正常。
  2. 在没有 的情况下运行时-X,相同的调用会返回一个MapOrientedComponentConfiguratorwhich barfs,因为它只能配置MapOrientedComponents。
  3. new BasicComponentConfigurator()用作品代替那个电话。
  4. 解决依赖注入让我感觉很脏。

我最好的猜测是,根据是否请求调试跟踪,某些东西会在某个范围内创建组件配置器,并且不会自行清理。


https://github.com/mikesamuel/fences-maven-enforcer-rule/commit/7cab8d8bd873f2341acab088e5bdad9c3e35640b是“修复”此行为的提交。

于 2016-03-10T20:40:44.087 回答