1

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>

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;

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 ;
  }
}

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>

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}

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.

4

2 回答 2

2

您实际上非常接近解决方案。你只需要像这样配置你的插件(没有内部<person>元素):

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

为了为您提供一个完整的工作示例,请考虑以下 Maven 插件 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>,它只是将地图记录为信息:

@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 插件安装在 repo 中(使用mvn clean install),我们就可以在这样的项目中使用它:

<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为:

[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]}
于 2015-10-11T13:07:20.933 回答
0

将@Parameter 注释添加到 Person 类中的 name 和 age 属性。

于 2014-05-15T01:20:56.057 回答