You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@maven.apache.org by Francois-Xavier Bonnet <fx...@gmail.com> on 2024/04/30 22:41:48 UTC

How to replace a Maven core component with a custom one?

Hey there,

I am writing an extension that needs to replace the
default ProjectDependenciesResolver with a custom one.
According to the documentation I think I should be able to do it: "The
mechanism allows extensions to either replace default Sisu components with
custom ones or add new components which are used at run time."
But I could not figure out how to do it or find any example.

I have tried to create a new class like this:

@Component(role = ProjectDependenciesResolver.class)
public class CustomProjectDependencyResolver implements
ProjectDependenciesResolver {
...
}

This did not work.

Re: How to replace a Maven core component with a custom one?

Posted by Tamás Cservenák <ta...@cservenak.net>.
Howdy,

sorry for late reply, I did some experimentation, and came up with this:
https://github.com/cstamas/override-maven-component

It seems overriding the core component from (core) extension wildly differs
HOW that component to be overridden is defined in core in the first place...

Take a look, and until then I will try to dust off my memories as well...
T

On Wed, May 1, 2024 at 3:11 AM Francois-Xavier Bonnet <
francois-xavier.bonnet@centraliens.net> wrote:

> Thanks Tamas,
>
> I am using Maven 3.9.6 and building my extension with java 11
> The project I am using to test the extension is built with Maven 3.9.6 and
> java 11 and I can tell the extension is loaded because it also contains an
> EventSpy that logs some stuff.
> This is the code for my custom ProjectDepedencyResolver but it is not
> loaded. What am I doing wrong?
>
> import org.apache.maven.project.*;
> import org.codehaus.plexus.component.annotations.Component;
> import org.eclipse.sisu.Priority;
>
> import javax.inject.Inject;
> import javax.inject.Named;
>
> @Named
> @Component(role = ProjectDependenciesResolver.class)
> @Priority(999)
> public class CustomProjectDependencyResolver extends
> DefaultProjectDependenciesResolver implements ProjectDependenciesResolver {
>
>     @Inject
>     public CustomProjectDependencyResolver() {
>     }
>
>     @Override
>     public DependencyResolutionResult resolve(DependencyResolutionRequest
> request) throws DependencyResolutionException {
>         throw new RuntimeException(); // just crashing for now to check if
> this class is properly injected
>     }
> }
>
> On Wed, 1 May 2024 at 08:54, Tamás Cservenák <ta...@cservenak.net> wrote:
>
> > Howdy,
> >
> > IF you target latest Maven 3.9.x (uses Sisu 0.9.0.M2), then:
> > - use max Java 17 bytecode
> > - use JSR330 instead of plexus annotations
> > - use org.eclipse.sisu.Priority annotation to override a component
> >
> > IF you target Maven 3.8.x or so, similar, but use Java 11 bytecode max
> >
> > Thanks
> > T
> >
> >
> > On Wed, May 1, 2024 at 12:42 AM Francois-Xavier Bonnet <
> > fx.bonnet@gmail.com>
> > wrote:
> >
> > > Hey there,
> > >
> > > I am writing an extension that needs to replace the
> > > default ProjectDependenciesResolver with a custom one.
> > > According to the documentation I think I should be able to do it: "The
> > > mechanism allows extensions to either replace default Sisu components
> > with
> > > custom ones or add new components which are used at run time."
> > > But I could not figure out how to do it or find any example.
> > >
> > > I have tried to create a new class like this:
> > >
> > > @Component(role = ProjectDependenciesResolver.class)
> > > public class CustomProjectDependencyResolver implements
> > > ProjectDependenciesResolver {
> > > ...
> > > }
> > >
> > > This did not work.
> > >
> >
>

Re: How to replace a Maven core component with a custom one?

Posted by Francois-Xavier Bonnet <fr...@centraliens.net>.
Hi Tamas,

Thanks so much for looking into this and creating a bug report.

I did some more testing, starting with the 3 lifecycle extension points
mentioned in the documentation
<https://maven.apache.org/examples/maven-3-lifecycle-extensions.html>. I
successfully created subclasses of AbstractEventSpy and
AbstractMavenLifecycleParticipant and made them work but I could not get my
subclass of AbstractExecutionListener to work at all. Debug shows that the
singleton is not even created despite the JSR330 annotations.
The ProjectDependenciesResolver singleton is created but not executed
so it looks like a different problem.

For my initial problem, I found a workaround using a
RepositorySessionDecorator which lets me modify the dependency tree after
the dependency resolution which is just what I needed. I am not sure this
will be supported long term though.


On Thu, 2 May 2024 at 00:00, Tamás Cservenák <ta...@cservenak.net> wrote:

> Guillaume,
>
> it does not help, see my reproducer.
>
> Or in other words, it works if the Maven Core component is defined as a
> JSR330 component, but does not work if the Core component is defined as a
> Plexus component.
>
> The ProjectDependenciesResolver implementation
> (DefaultProjectDependenciesResolver) is a plexus component.
>
> T
>
> On Wed, May 1, 2024 at 3:21 PM Guillaume Nodet <gn...@apache.org> wrote:
>
> > Did you add the sisu inject plugin which generates the
> > META-INF/sisu/javax.inject.Named index to your build ?
> >
> > Le mer. 1 mai 2024 à 03:11, Francois-Xavier Bonnet <
> > francois-xavier.bonnet@centraliens.net> a écrit :
> >
> > > Thanks Tamas,
> > >
> > > I am using Maven 3.9.6 and building my extension with java 11
> > > The project I am using to test the extension is built with Maven 3.9.6
> > and
> > > java 11 and I can tell the extension is loaded because it also contains
> > an
> > > EventSpy that logs some stuff.
> > > This is the code for my custom ProjectDepedencyResolver but it is not
> > > loaded. What am I doing wrong?
> > >
> > > import org.apache.maven.project.*;
> > > import org.codehaus.plexus.component.annotations.Component;
> > > import org.eclipse.sisu.Priority;
> > >
> > > import javax.inject.Inject;
> > > import javax.inject.Named;
> > >
> > > @Named
> > > @Component(role = ProjectDependenciesResolver.class)
> > > @Priority(999)
> > > public class CustomProjectDependencyResolver extends
> > > DefaultProjectDependenciesResolver implements
> > ProjectDependenciesResolver {
> > >
> > >     @Inject
> > >     public CustomProjectDependencyResolver() {
> > >     }
> > >
> > >     @Override
> > >     public DependencyResolutionResult
> resolve(DependencyResolutionRequest
> > > request) throws DependencyResolutionException {
> > >         throw new RuntimeException(); // just crashing for now to check
> > if
> > > this class is properly injected
> > >     }
> > > }
> > >
> > > On Wed, 1 May 2024 at 08:54, Tamás Cservenák <ta...@cservenak.net>
> > wrote:
> > >
> > > > Howdy,
> > > >
> > > > IF you target latest Maven 3.9.x (uses Sisu 0.9.0.M2), then:
> > > > - use max Java 17 bytecode
> > > > - use JSR330 instead of plexus annotations
> > > > - use org.eclipse.sisu.Priority annotation to override a component
> > > >
> > > > IF you target Maven 3.8.x or so, similar, but use Java 11 bytecode
> max
> > > >
> > > > Thanks
> > > > T
> > > >
> > > >
> > > > On Wed, May 1, 2024 at 12:42 AM Francois-Xavier Bonnet <
> > > > fx.bonnet@gmail.com>
> > > > wrote:
> > > >
> > > > > Hey there,
> > > > >
> > > > > I am writing an extension that needs to replace the
> > > > > default ProjectDependenciesResolver with a custom one.
> > > > > According to the documentation I think I should be able to do it:
> > "The
> > > > > mechanism allows extensions to either replace default Sisu
> components
> > > > with
> > > > > custom ones or add new components which are used at run time."
> > > > > But I could not figure out how to do it or find any example.
> > > > >
> > > > > I have tried to create a new class like this:
> > > > >
> > > > > @Component(role = ProjectDependenciesResolver.class)
> > > > > public class CustomProjectDependencyResolver implements
> > > > > ProjectDependenciesResolver {
> > > > > ...
> > > > > }
> > > > >
> > > > > This did not work.
> > > > >
> > > >
> > >
> >
> >
> > --
> > ------------------------
> > Guillaume Nodet
> >
>

Re: How to replace a Maven core component with a custom one?

Posted by Tamás Cservenák <ta...@cservenak.net>.
Guillaume,

it does not help, see my reproducer.

Or in other words, it works if the Maven Core component is defined as a
JSR330 component, but does not work if the Core component is defined as a
Plexus component.

The ProjectDependenciesResolver implementation
(DefaultProjectDependenciesResolver) is a plexus component.

T

On Wed, May 1, 2024 at 3:21 PM Guillaume Nodet <gn...@apache.org> wrote:

> Did you add the sisu inject plugin which generates the
> META-INF/sisu/javax.inject.Named index to your build ?
>
> Le mer. 1 mai 2024 à 03:11, Francois-Xavier Bonnet <
> francois-xavier.bonnet@centraliens.net> a écrit :
>
> > Thanks Tamas,
> >
> > I am using Maven 3.9.6 and building my extension with java 11
> > The project I am using to test the extension is built with Maven 3.9.6
> and
> > java 11 and I can tell the extension is loaded because it also contains
> an
> > EventSpy that logs some stuff.
> > This is the code for my custom ProjectDepedencyResolver but it is not
> > loaded. What am I doing wrong?
> >
> > import org.apache.maven.project.*;
> > import org.codehaus.plexus.component.annotations.Component;
> > import org.eclipse.sisu.Priority;
> >
> > import javax.inject.Inject;
> > import javax.inject.Named;
> >
> > @Named
> > @Component(role = ProjectDependenciesResolver.class)
> > @Priority(999)
> > public class CustomProjectDependencyResolver extends
> > DefaultProjectDependenciesResolver implements
> ProjectDependenciesResolver {
> >
> >     @Inject
> >     public CustomProjectDependencyResolver() {
> >     }
> >
> >     @Override
> >     public DependencyResolutionResult resolve(DependencyResolutionRequest
> > request) throws DependencyResolutionException {
> >         throw new RuntimeException(); // just crashing for now to check
> if
> > this class is properly injected
> >     }
> > }
> >
> > On Wed, 1 May 2024 at 08:54, Tamás Cservenák <ta...@cservenak.net>
> wrote:
> >
> > > Howdy,
> > >
> > > IF you target latest Maven 3.9.x (uses Sisu 0.9.0.M2), then:
> > > - use max Java 17 bytecode
> > > - use JSR330 instead of plexus annotations
> > > - use org.eclipse.sisu.Priority annotation to override a component
> > >
> > > IF you target Maven 3.8.x or so, similar, but use Java 11 bytecode max
> > >
> > > Thanks
> > > T
> > >
> > >
> > > On Wed, May 1, 2024 at 12:42 AM Francois-Xavier Bonnet <
> > > fx.bonnet@gmail.com>
> > > wrote:
> > >
> > > > Hey there,
> > > >
> > > > I am writing an extension that needs to replace the
> > > > default ProjectDependenciesResolver with a custom one.
> > > > According to the documentation I think I should be able to do it:
> "The
> > > > mechanism allows extensions to either replace default Sisu components
> > > with
> > > > custom ones or add new components which are used at run time."
> > > > But I could not figure out how to do it or find any example.
> > > >
> > > > I have tried to create a new class like this:
> > > >
> > > > @Component(role = ProjectDependenciesResolver.class)
> > > > public class CustomProjectDependencyResolver implements
> > > > ProjectDependenciesResolver {
> > > > ...
> > > > }
> > > >
> > > > This did not work.
> > > >
> > >
> >
>
>
> --
> ------------------------
> Guillaume Nodet
>

Re: How to replace a Maven core component with a custom one?

Posted by Guillaume Nodet <gn...@apache.org>.
Did you add the sisu inject plugin which generates the
META-INF/sisu/javax.inject.Named index to your build ?

Le mer. 1 mai 2024 à 03:11, Francois-Xavier Bonnet <
francois-xavier.bonnet@centraliens.net> a écrit :

> Thanks Tamas,
>
> I am using Maven 3.9.6 and building my extension with java 11
> The project I am using to test the extension is built with Maven 3.9.6 and
> java 11 and I can tell the extension is loaded because it also contains an
> EventSpy that logs some stuff.
> This is the code for my custom ProjectDepedencyResolver but it is not
> loaded. What am I doing wrong?
>
> import org.apache.maven.project.*;
> import org.codehaus.plexus.component.annotations.Component;
> import org.eclipse.sisu.Priority;
>
> import javax.inject.Inject;
> import javax.inject.Named;
>
> @Named
> @Component(role = ProjectDependenciesResolver.class)
> @Priority(999)
> public class CustomProjectDependencyResolver extends
> DefaultProjectDependenciesResolver implements ProjectDependenciesResolver {
>
>     @Inject
>     public CustomProjectDependencyResolver() {
>     }
>
>     @Override
>     public DependencyResolutionResult resolve(DependencyResolutionRequest
> request) throws DependencyResolutionException {
>         throw new RuntimeException(); // just crashing for now to check if
> this class is properly injected
>     }
> }
>
> On Wed, 1 May 2024 at 08:54, Tamás Cservenák <ta...@cservenak.net> wrote:
>
> > Howdy,
> >
> > IF you target latest Maven 3.9.x (uses Sisu 0.9.0.M2), then:
> > - use max Java 17 bytecode
> > - use JSR330 instead of plexus annotations
> > - use org.eclipse.sisu.Priority annotation to override a component
> >
> > IF you target Maven 3.8.x or so, similar, but use Java 11 bytecode max
> >
> > Thanks
> > T
> >
> >
> > On Wed, May 1, 2024 at 12:42 AM Francois-Xavier Bonnet <
> > fx.bonnet@gmail.com>
> > wrote:
> >
> > > Hey there,
> > >
> > > I am writing an extension that needs to replace the
> > > default ProjectDependenciesResolver with a custom one.
> > > According to the documentation I think I should be able to do it: "The
> > > mechanism allows extensions to either replace default Sisu components
> > with
> > > custom ones or add new components which are used at run time."
> > > But I could not figure out how to do it or find any example.
> > >
> > > I have tried to create a new class like this:
> > >
> > > @Component(role = ProjectDependenciesResolver.class)
> > > public class CustomProjectDependencyResolver implements
> > > ProjectDependenciesResolver {
> > > ...
> > > }
> > >
> > > This did not work.
> > >
> >
>


-- 
------------------------
Guillaume Nodet

Re: How to replace a Maven core component with a custom one?

Posted by Francois-Xavier Bonnet <fr...@centraliens.net>.
Thanks Tamas,

I am using Maven 3.9.6 and building my extension with java 11
The project I am using to test the extension is built with Maven 3.9.6 and
java 11 and I can tell the extension is loaded because it also contains an
EventSpy that logs some stuff.
This is the code for my custom ProjectDepedencyResolver but it is not
loaded. What am I doing wrong?

import org.apache.maven.project.*;
import org.codehaus.plexus.component.annotations.Component;
import org.eclipse.sisu.Priority;

import javax.inject.Inject;
import javax.inject.Named;

@Named
@Component(role = ProjectDependenciesResolver.class)
@Priority(999)
public class CustomProjectDependencyResolver extends
DefaultProjectDependenciesResolver implements ProjectDependenciesResolver {

    @Inject
    public CustomProjectDependencyResolver() {
    }

    @Override
    public DependencyResolutionResult resolve(DependencyResolutionRequest
request) throws DependencyResolutionException {
        throw new RuntimeException(); // just crashing for now to check if
this class is properly injected
    }
}

On Wed, 1 May 2024 at 08:54, Tamás Cservenák <ta...@cservenak.net> wrote:

> Howdy,
>
> IF you target latest Maven 3.9.x (uses Sisu 0.9.0.M2), then:
> - use max Java 17 bytecode
> - use JSR330 instead of plexus annotations
> - use org.eclipse.sisu.Priority annotation to override a component
>
> IF you target Maven 3.8.x or so, similar, but use Java 11 bytecode max
>
> Thanks
> T
>
>
> On Wed, May 1, 2024 at 12:42 AM Francois-Xavier Bonnet <
> fx.bonnet@gmail.com>
> wrote:
>
> > Hey there,
> >
> > I am writing an extension that needs to replace the
> > default ProjectDependenciesResolver with a custom one.
> > According to the documentation I think I should be able to do it: "The
> > mechanism allows extensions to either replace default Sisu components
> with
> > custom ones or add new components which are used at run time."
> > But I could not figure out how to do it or find any example.
> >
> > I have tried to create a new class like this:
> >
> > @Component(role = ProjectDependenciesResolver.class)
> > public class CustomProjectDependencyResolver implements
> > ProjectDependenciesResolver {
> > ...
> > }
> >
> > This did not work.
> >
>

Re: How to replace a Maven core component with a custom one?

Posted by Tamás Cservenák <ta...@cservenak.net>.
Howdy,

IF you target latest Maven 3.9.x (uses Sisu 0.9.0.M2), then:
- use max Java 17 bytecode
- use JSR330 instead of plexus annotations
- use org.eclipse.sisu.Priority annotation to override a component

IF you target Maven 3.8.x or so, similar, but use Java 11 bytecode max

Thanks
T


On Wed, May 1, 2024 at 12:42 AM Francois-Xavier Bonnet <fx...@gmail.com>
wrote:

> Hey there,
>
> I am writing an extension that needs to replace the
> default ProjectDependenciesResolver with a custom one.
> According to the documentation I think I should be able to do it: "The
> mechanism allows extensions to either replace default Sisu components with
> custom ones or add new components which are used at run time."
> But I could not figure out how to do it or find any example.
>
> I have tried to create a new class like this:
>
> @Component(role = ProjectDependenciesResolver.class)
> public class CustomProjectDependencyResolver implements
> ProjectDependenciesResolver {
> ...
> }
>
> This did not work.
>