替换 Maven 3 中的 plexus 组件
我需要用我自己的实现替换一些 Maven 默认功能,并且我正在寻找一种干净的方法来做到这一点。
我已经扩展了 org.apache.maven.repository.internal.DefaultVersionRangeResolver 并使用 component.xml 注册了我的扩展组件,如下所示:
<component-set>
<components>
<component>
<role>org.sonatype.aether.impl.VersionRangeResolver</role>
<role-hint>default</role-hint>
<implementation>com.my.custom.VersionRangeResolver
</implementation>
<isolated-realm>false</isolated-realm>
<requirements>
<requirement>
<role>org.sonatype.aether.spi.log.Logger</role>
<role-hint />
<field-name>logger</field-name>
</requirement>
<requirement>
<role>org.sonatype.aether.spi.log.Logger</role>
<role-hint />
<field-name>logger2</field-name>
</requirement>
<requirement>
<role>org.sonatype.aether.impl.MetadataResolver</role>
<role-hint />
<field-name>metadataResolver</field-name>
</requirement>
</requirements>
</component>
</components>
</component-set>
我已经在本地存储库中安装了包含此组件的项目,并且我引用它在另一个项目的 pom.xml 中是这样的:
<build>
<extensions>
<extension>
<groupId>my.groupId</groupId>
<artifactId>maven-version-resolver</artifactId>
<version>SNAPSHOT</version>
</extension>
</extensions>
</build>
但是,我的工件没有被使用。当我在构建中运行这个小 GMaven groovy 脚本时:
session.container.getComponentDescriptorList(
'org.sonatype.aether.impl.VersionRangeResolver'
).each{
println "Role Hint: ${it.roleHint}, implementation: ${it.implementation}" ;
}
它向我显示了默认实现和我自己的实现,两者都带有提示“默认”。那么我该如何解决这个问题呢?
- 我是否需要在 Components.xml 中设置额外的参数(可能具有更高的优先级)?
- 我是否需要将组件编写为 Maven 插件并以编程方式主动注册该组件?
- 是否有任何 Plexus 文档涵盖了这一点?
I need to replace some Maven default functionality with my own implementation, and I am looking for a clean way to do that.
I have extended org.apache.maven.repository.internal.DefaultVersionRangeResolver
and registered my extended component using a component.xml as follows:
<component-set>
<components>
<component>
<role>org.sonatype.aether.impl.VersionRangeResolver</role>
<role-hint>default</role-hint>
<implementation>com.my.custom.VersionRangeResolver
</implementation>
<isolated-realm>false</isolated-realm>
<requirements>
<requirement>
<role>org.sonatype.aether.spi.log.Logger</role>
<role-hint />
<field-name>logger</field-name>
</requirement>
<requirement>
<role>org.sonatype.aether.spi.log.Logger</role>
<role-hint />
<field-name>logger2</field-name>
</requirement>
<requirement>
<role>org.sonatype.aether.impl.MetadataResolver</role>
<role-hint />
<field-name>metadataResolver</field-name>
</requirement>
</requirements>
</component>
</components>
</component-set>
I have installed the project that contains this in my local repo, and I reference it like this in another project's pom.xml:
<build>
<extensions>
<extension>
<groupId>my.groupId</groupId>
<artifactId>maven-version-resolver</artifactId>
<version>SNAPSHOT</version>
</extension>
</extensions>
</build>
However, my artifact is not used. When I run this little GMaven groovy script inside the build:
session.container.getComponentDescriptorList(
'org.sonatype.aether.impl.VersionRangeResolver'
).each{
println "Role Hint: ${it.roleHint}, implementation: ${it.implementation}" ;
}
it shows me both the default implementation and my own implementation, both with the hint 'default'. So how can I solve this?
- Do I need to set an additional parameter in the components.xml (perhaps a higher priority)?
- Do I need to write my component as a Maven Plugin and actively register the component programmatically?
- Is there any Plexus documentation that covers this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个很晚的答案,但我希望它能帮助那些在 maven-core 中遇到类似问题的人。
使用 Maven 3.3.1,可以使用在项目根目录中定义的自定义核心扩展,并且不需要对命令参数或 Maven 安装进行任何修改: https://maven.apache.org/docs/3.3.1/release-notes.html#Core_Extensions。
我遇到了和您一样的问题,需要自定义版本范围分辨率并设法使其正常工作。我的第一个工作实现标记在这里:https://github。 com/splatch/maven-osgi-resolver/tree/3.3.9-a。我将代码向前推了一点,但此标签包含自定义版本范围处理所需的所有内容。
一般来说,魔鬼在于细节 - 你必须使用 plexus 注释并在
META-INF/maven/extension.xml
导出包中使用plexus-components.xml
声明,否则它会不工作。This is very late answer, but I hope it will help these who got stuck with similar problem in maven-core.
With Maven 3.3.1 it is possible to use custom core extensions which are defined in project root and does not require any modifications to command parameters or maven installation: https://maven.apache.org/docs/3.3.1/release-notes.html#Core_Extensions.
I had very same issue like you to customize version range resolution and managed to make it working. My first working implementation is tagged here: https://github.com/splatch/maven-osgi-resolver/tree/3.3.9-a. I pushed code a little bit forward, but this tag contains everything you need to customize version range handling.
In general devil sits in details - you must use plexus annotations and declare in
META-INF/maven/extension.xml
export package withplexus-components.xml
, otherwise it will not work.显然问题是我的组件定义注册得太晚了。在解析 pom
时,常规的DefaultVersionRangeResolver
已经被实例化。解决方案更像是一种破解:
$MAVEN_HOME
并把你的那里的组件
将它们注册到
$MAVEN_HOME`/bin/m2.conf
(如您所见,
ext/.*
在lib/.*
之前加载)因为我的组件旨在添加站点范围功能,我现在必须创建一个插件来安装和强制执行这些扩展。
Apparently the problem is that my component definition is registered too late. At the time when the pom
<extension>
is parsed, the regularDefaultVersionRangeResolver
has already been instantiated.The solution is more of a hack:
$MAVEN_HOME
and put yourcomponents there
register them in
$MAVEN_HOME`/bin/m2.conf
(as you can see
ext/.*
is loaded beforelib/.*
)Since my component is meant to add site-wide functionality, I will now have to create a plugin that installs and enforces these extensions.