0

I have two plugins that are using same duplicated methods. I want to refactor then and move that methods to a shared class and host it in a dependency jar.

Those method required some maven properties as MavenProject, PluginDescriptor and some others maven classes that used to be injected into the mojo.

Is there anyway of such properties to be injected directly in the shared class?

or do I need to inject them into the mojo and then call some initialization method?

I declared the shared class with @Named and created a constructor with @Inject. The mojo class has a contructor too (the code below). Then I tried run it. All the values are being inject into the mojo properly but the shared class object inner properties values are null.

@Inject
public SharedValidationHelperDefault(final MavenProject project,
        final BuildContext buildContext,
        final RuntimeInformation runtimeInformation, final Log log) {
    this.buildContext = buildContext;
    this.project = project;
    this.runtimeInformation = runtimeInformation;
    this.log = log;
}

...

@Inject
public AbstractContainerPackMojo(
        final RuntimeInformation runtimeInformation,
        final MavenProjectHelper projectHelper,
        final BuildContext buildContext, SharedValidationHelper validationHelper) {
    this.validationHelper = validationHelper;
    this.buildContext = buildContext;
    this.runtimeInformation = runtimeInformation;
    this.projectHelper = projectHelper;
}
Cristiano
  • 1,414
  • 15
  • 22

1 Answers1

1

There's a difference between Mojo annotations and Component/JSR330 annotations: they have different purposes and are differently injected. However, it is possible to have an abstract/shared Mojo. Have a look at the surefire project: both the surefire-plugin and failsafe-plugin have some shared Mojo code&configuration in a separate module ( http://maven.apache.org/surefire/maven-surefire-common/index.html )

Robert Scholte
  • 11,889
  • 2
  • 35
  • 44
  • 2
    I think I'm aware about those differences. What I didn't understand was why the injection with @inject on the mojo is working, but that injection is not working in the shared class. – Cristiano Mar 16 '15 at 14:37