我有一个 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
只是Object
with 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
以使记录器产生调试跟踪。