【问题标题】:Dependency injection in a maven pluginmaven插件中的依赖注入
【发布时间】:2014-04-03 21:04:44
【问题描述】:

我正在开发一个 maven 插件并使其更具可测试性,我想使用轻量级依赖注入框架(如 Guice)来管理服务等,但我可以让它们与我没有使用过的应用程序集成能够将它们与我的插件集成。除了以静态方式进行依赖注入之外,还有什么方法可以做到这一点?

【问题讨论】:

    标签: java maven dependency-injection maven-plugin


    【解决方案1】:

    还有另一个框架(一个小型​​库)为 Maven 插件开发带来了比 carlosvin 的评论(Maven JSR 330)中引用的更好的 CDI 支持。

    这是图书馆:Maven CDI Plugin Utils

    它不仅提供了 DI,还提供了一些附加概念,例如基于工作流的处理模型,默认情况下具有回滚功能和可扩展性。

    我将很快发布一篇关于这个库的博客文章,但我认为文档应该解释所有概念。顺便提一句。这是 CDI 中已知的 DI,具有全功能 Weld SE 容器和基于类路径的 bean 发现!

    【讨论】:

      【解决方案2】:

      Maven 已经为您提供了名为 Plexus 的嵌入式 IoC 容器。您可以包含其他组件

      public class MonitorMojo
          extends AbstractMojo
      {
      
          /**
           * The website monitor component instance that will be injected 
           * by the Plexus runtime.
           * @component
           */
          private WebsiteMonitor monitor;
      
          public void execute()
              throws MojoExecutionException, MojoFailureException
          {
              // TODO Auto-generated method stub
      
          }
      
      }
      

      并参考属性

      @Parameter( property = "sayhi.greeting", defaultValue = "Hello World!" )
      private String greeting;
      

      您可以在maven plugin dev siteplexus documentation 找到更多信息。

      【讨论】:

      • Maven 3.1.+ 带来了新的依赖系统,它仍然支持 plexus api/annotations,但是 maven 正在迁移到 guice/jsr330,查看以下链接:maven.apache.org/maven-jsr330.html
      猜你喜欢
      • 1970-01-01
      • 2021-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-30
      • 1970-01-01
      • 2015-10-13
      • 2011-12-08
      相关资源
      最近更新 更多