Howto inject Plexus component into Mojo

1.4k views Asked by At

Is it possible to inject a Plexus component into a Mojo. Here's what I tried but myComponent is always null.

My Component:

import org.codehaus.plexus.component.annotations.Component;

@Component(role = MyComponent.class, hint = "mine")
public class MyComponent {

}

My Mojo:

import org.codehaus.plexus.component.annotations.Requirement;
import org.apache.maven.plugins.annotations.Component;

public class MyMojo extends AbstractMojo {

    @Requirement(role = MyComponent.class, hint = "mine", optional = false)
    protected MyComponent myComponent;

    @Component
    protected MavenProject project;
}
1

There are 1 answers

0
scrutari On

You Java part is correct, but you need to add some sources processing to build of your Maven plugin. This can be achieved by adding the following to your build in pom.xml:

<plugin>
    <groupId>org.codehaus.plexus</groupId>
    <artifactId>plexus-component-metadata</artifactId>
    <version>2.0.0</version>
    <executions>
        <execution>
            <id>process-classes</id>
            <goals>
                <goal>generate-metadata</goal>
            </goals>
        </execution>
    </executions>
</plugin>