Replace plexus component in Maven 3

1.9k views Asked by At

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?
2

There are 2 answers

0
splatch On BEST ANSWER

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.

0
Sean Patrick Floyd On

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.