Maven MOJO dependency injection and parameters

1.1k Views Asked by At

When using Plexus for MOJO plugin DI, is there a way to inject MOJO configuration parameter (annotated with @Parameter in the Mojo class) in other components as well?

In other words: Is there a way I can share Mojo parameters with other Plexus components apart from injecting the Mojo itself in other components, or resorting to hand-rolled "init method"? I was kinda hoping parameters would be managed by DI context.

Let's say I have a Mojo like this:

@Mojo
public class MyMojo extends AbstractMojo {

    @Parameter
    private String param;

    @Inject
    private SomeComponent component;
}

Then it would be nice to do something like this:

@Named
public class SomeComponent {
    @Inject // or whatever else
    private String param;

    // some methods that use param here
}

Instead of having to do:

@Mojo
public class MyMojo extends AbstractMojo {

    @Parameter
    private String param;

    @Inject
    private SomeComponent component;

    @Override
    public void execute() throws MojoExecutionException {
        component.setParam(param);
        ...
    }
}
0

There are 0 best solutions below