Java Maven Mojo:复杂地图属性 [英] Java Maven Mojo : Complex Map Attribute

查看:119
本文介绍了Java Maven Mojo:复杂地图属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

maven中提到的Mojo地图属性示例. apache.org 非常简单,因为它定义了一个以字符串作为键和值的Map,如下所示:

The example of map attribute for a mojo mentioned in maven.apache.org is quite simple as it defines a Map with a String as a key and as a value as specified below :

/**
 * My Map.
 */
@Parameter
private Map myMap;

,它的分配配置如下所示:

and it's assigned configuration would look like this :

<myMap>
 <key1>value1</key1>
 <key2>value2</key2>
</myMap>

我想要实现的是一个更高级的地图,该地图将String作为键,并将我自己定义的类Person作为值:

What I am trying to achieve is a more advanced map which takes a String as a key and my own defined class Person as value:

/**
* My Advanced Map.
*/
@Parameter
private Map<String,Person> myMap;

Person类与我的MOJO位于同一包中,它看起来像:

The Person class is located in the same package as my MOJO and it looks like:

public class Person {
  private String name;
  private int age;

  public void setName( String name )
  {
      this.name = name;
  }

  public void setAge( int age )
  {
      this.age = age;
  }

  public String getName( )
  {
      return this.name;
  }

  public int getAge( )
  {
      return this.age ;
  }
}

我假设我的MOJO的配置如下所示:

I assume that the configuration for my MOJO would look like :

<myMap>
  <firstPerson>
    <person>
      <name>steve</name>
      <age>26</age>
    </person>
  </firstPerson>
  <secondPerson>
    <person>
      <name>meruem</name>
      <age>1</age>
    </person>
  </secondPerson>
</myMap>

以上述配置运行此MOJO将使用定义的键创建地图,但我总是得到空值: {firstPerson = null,secondPerson = null}

Running this MOJO with the above configuration will create the map with the defined keys but I always get null values : {firstPerson=null,secondPerson=null}

目前,我不知道我是在做错什么,还是该示例是否受支持,因为没有找到描述高级"地图属性的文档,而我现在的最后一招就是浏览源代码. /p>

Currently, I don't know whether I am doing something wrong or if the example is even supported as no documentation has been found that describes an 'advanced' map attribute and my last resort for now would be browsing the sources.

推荐答案

您实际上已经很接近解决方案了.您只需要这样配置您的插件(无需内部<person>元素):

You are actually really close to the solution. You just need to configure your plugin like this (without the inner <person> element):

<myMap>
  <firstPerson>
    <name>steve</name>
    <age>26</age>
  </firstPerson>
  <secondPerson>
    <name>meruem</name>
    <age>1</age>
  </secondPerson>
</myMap>


要为您提供完整的工作示例,请考虑以下Maven插件POM:


To provide you with a full working example, consider the following Maven plugin POM:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>sample.plugin</groupId>
    <artifactId>test-maven-plugin</artifactId>
    <version>1.0.0</version>
    <packaging>maven-plugin</packaging>
    <dependencies>
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-plugin-api</artifactId>
            <version>2.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.plugin-tools</groupId>
            <artifactId>maven-plugin-annotations</artifactId>
            <version>3.4</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>

具有以下MOJO,声明一个目标foo,并具有类型为Map<String, Person>的参数,该参数只是将Map记录为信息:

having the following MOJO, declaring a goal foo and having a parameter of type Map<String, Person>, which simply logs the Map as info:

@Mojo(name = "foo")
public class MyMojo extends AbstractMojo {

    @Parameter
    private Map<String, Person> map;

    public void execute() throws MojoExecutionException, MojoFailureException {
        getLog().info(map.toString());
    }

}

和以下Person类:

public class Person {

    private String name;
    private int age;

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + "]";
    }

}

一旦将此Maven插件安装在仓库中(使用mvn clean install),我们就可以在这样的项目中使用它:

Once this Maven plugin is installed in the repo (using mvn clean install), we can use it in a project like this:

<plugin>
    <groupId>sample.plugin</groupId>
    <artifactId>test-maven-plugin</artifactId>
    <version>1.0.0</version>
    <executions>
        <execution>
            <id>foo</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>foo</goal>
            </goals>
            <configuration>
                <map>
                    <person1>
                        <name>Name 1</name>
                        <age>10</age>
                    </person1>
                    <person2>
                         <name>Name 2</name>
                         <age>20</age>
                     </person2>
                </map>
            </configuration>
        </execution>
    </executions>
</plugin>

运行mvn clean generate-sources时此插件的输出为:

The output of this plugin when running mvn clean generate-sources is:

[INFO] --- test-maven-plugin:1.0.0:foo (add-source) @ test ---
[INFO] {person1=Person [name=Name 1, age=10], person2=Person [name=Name 2, age=20]}

这篇关于Java Maven Mojo:复杂地图属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆