2

POM 構成オブジェクトから構成するようにセットアップした Maven 拡張機能があります。

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

結局電話する

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

メソッド。

構成が追加の構成をインポートして、提案と第 2 のスタイルの委任を許可することを許可したいので、

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

アーティファクト g:a:v には、 content を含むファイルMETA-INF/my-project-name.xmlがあります<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);
  }
}

whereconfigurableObjectwith setters で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. で実行し-Xてデバッグをオンにすると、helper.getComponent(ComponentConfigurator.class)a が返されBasicComponentConfigurator、すべて正常に動作します。
  2. なしで実行する-Xと、同じ呼び出しでMapOrientedComponentConfiguratorwhich barfs しか構成できないため、 which barfsが返されますMapOrientedComponent
  3. new BasicComponentConfigurator()その呼び出しを作品に置き換えます。
  4. 依存性注入を回避すると、気分が悪くなります。

私の推測では、デバッグ トレースが要求されているかどうかに応じて、何らかのスコープでコンポーネント コンフィギュレーターが作成されており、それ自体はクリーンアップされていません。


https://github.com/mikesamuel/fences-maven-enforcer-rule/commit/7cab8d8bd873f2341acab088e5bdad9c3e35640bは、この動作が「修正」されたコミットです。

于 2016-03-10T20:40:44.087 に答える