如何将Plexus组件注入Mojo

时间:2013-12-31 22:20:51

标签: maven-3 plexus

是否可以将Plexus组件注入Mojo。 这是我尝试的内容,但myComponent始终是null

我的组件:

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 个答案:

答案 0 :(得分:0)

你的Java部分是正确的,但你需要添加一些源处理来构建你的Maven插件。这可以通过将以下内容添加到build中的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>