【问题标题】:Injecting maven properties into mojo with Sisu使用 Sisu 将 maven 属性注入 mojo
【发布时间】:2015-03-15 19:10:38
【问题描述】:

我有两个使用相同重复方法的插件。然后我想重构并将该方法移动到共享类并将其托管在依赖项 jar 中。

这些方法需要一些 maven 属性,如 MavenProject、PluginDescriptor 和其他一些过去注入到 mojo 中的 maven 类。

是否有这样的属性可以直接注入到共享类中?

还是我需要将它们注入到 mojo 中然后调用一些初始化方法?

我用@Named 声明了共享类,并用@Inject 创建了一个构造函数。 mojo 类也有一个构造函数(下面的代码)。 然后我尝试运行它。所有值都被正确注入到 mojo 中,但共享类对象内部属性值为 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;
}

【问题讨论】:

    标签: maven inject mojo


    【解决方案1】:

    Mojo 注释和 Component/JSR330 注释之间存在区别:它们有不同的用途并且注入方式不同。但是,可以有一个抽象/共享的 Mojo。看看 surefire 项目:surefire-plugin 和 failsafe-plugin 在单独的模块中都有一些共享的 Mojo 代码和配置 (http://maven.apache.org/surefire/maven-surefire-common/index.html)

    【讨论】:

    • 我想我知道这些差异。我不明白为什么在 mojo 上使用 @inject 进行注入是有效的,但该注入在共享类中不起作用。
    猜你喜欢
    • 2014-06-26
    • 2012-08-06
    • 2014-08-18
    • 1970-01-01
    • 2014-11-02
    • 2014-01-18
    • 2012-04-23
    • 2014-12-05
    • 1970-01-01
    相关资源
    最近更新 更多