【问题标题】:Mojo complex dependency injectionMojo 复杂的依赖注入
【发布时间】:2018-09-28 16:33:44
【问题描述】:

我正在尝试在 mojo 中的字段上方使用 @Parameter

@Parameter(required = false)
public Map authentication;

在插件使用中我通过了:

<configuration>
   <authentication>
       <user>a</user>
       <server>
           <address>server.com</address>
           <port>123</port>
       </server>
   <authentication>
</configuration>

行为是maven似乎不能注入所有数据,只是&lt;K,V&gt;很简单,比如&lt;user&gt;有一个String,但在&lt;server&gt;的情况下,我期待同样的行为,例如,另一个Map&lt;K,V&gt;&lt;address&gt;&lt;port&gt;,但maven 注入null

【问题讨论】:

  • 是的,我已经阅读了,但是没有任何关于复杂任意配置的信息被告知,就像示例一样,深度不超过一个级别

标签: maven plugins dependency-injection parameters mojo


【解决方案1】:

您可能想查看Mapping Complex Objects 文档。

如果我理解正确,您想为“身份验证”配置一个设置,但它的值是任意键值,其中值可能是一些复杂的对象“服务器”并有它的“地址”和“端口” " 映射到正确的类型?

最好不要将其建模为 Map,而是使用一些对象来表示此结构

public final class Server {
    private String address;
    private Integer port;
    // getters 
}

public final class Authentication {
    private String user;
    private Server server;
    // getters 
}

public final class MyMojo extends AbstractMojo {
    @Parameter
    private Authentication authentication;
}

这允许您使用您提供的 sn-p 配置。

如果我理解不正确,而您实际上想要一个 java.util.Map&lt;K, V&gt;,您可以在其中提供任意值的身份验证,那么您可能会不走运。我想你可以让Authentication 类为额外的东西保存一个地图:

public final class Authentication {
    public String user;
    public Server server;
    public Map<String, String> other;
}


...
<properties>
  <authentication>
    <user>theUser</user>
    <server>
      <address>server.com</address>
      <port>123</port>
    </server>
    <other>
      <key1>value1</key1>
      <key2>value2</key2>
    </other>
</properties>
...

我的个人建议是采用明确建模您想要的结构的路线。这允许您让 Maven 插件环境为您做尽可能多的检查。如果您使用任意地图,则必须自己进行各种编组。

一个缺点是 Maven 没有为复杂对象提供一个很好的描述符,但无论如何你不会有一个简单的 Map 。

【讨论】:

    猜你喜欢
    • 2015-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-07
    • 2011-01-09
    • 1970-01-01
    • 2011-04-08
    相关资源
    最近更新 更多