替换 Maven 3 中的 plexus 组件

发布于 2024-10-17 14:53:04 字数 2353 浏览 1 评论 0原文

我需要用我自己的实现替换一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

べ繥欢鉨o。 2024-10-24 14:53:04

这是一个很晚的答案,但我希望它能帮助那些在 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 with plexus-components.xml, otherwise it will not work.

太阳公公是暖光 2024-10-24 14:53:04

显然问题是我的组件定义注册得太晚了。在解析 pom 时,常规的 DefaultVersionRangeResolver 已经被实例化。

解决方案更像是一种破解:

  1. 在其中创建一个目录
    $MAVEN_HOME 并把你的
    那里的组件
  2. 将它们注册到
    $MAVEN_HOME`/bin/m2.conf

    main 是来自 plexus.core 的 org.apache.maven.cli.MavenCli
    
    设置maven.home默认${user.home}/m2
    
    [丛核]
    加载 ${maven.home}/ext/*.jar
    加载 ${maven.home}/lib/*.jar
    

    (如您所见,ext/.*lib/.* 之前加载)

因为我的组件旨在添加站点范围功能,我现在必须创建一个插件来安装和强制执行这些扩展。

Apparently the problem is that my component definition is registered too late. At the time when the pom <extension> is parsed, the regular DefaultVersionRangeResolver has already been instantiated.

The solution is more of a hack:

  1. Create a directory inside
    $MAVEN_HOME and put your
    components there
  2. register them in
    $MAVEN_HOME`/bin/m2.conf

    main is org.apache.maven.cli.MavenCli from plexus.core
    
    set maven.home default ${user.home}/m2
    
    [plexus.core]
    load ${maven.home}/ext/*.jar
    load ${maven.home}/lib/*.jar
    

    (as you can see ext/.* is loaded before lib/.*)

Since my component is meant to add site-wide functionality, I will now have to create a plugin that installs and enforces these extensions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文