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);
}
}
whereconfigurable
はObject
with 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
、ロガーがデバッグ トレースを生成するように実行するかどうかです。