Maven Mojo 映射复杂对象

标签 maven parameters configuration mojo

我正在尝试编写一个 maven 插件,包括 mvn 配置参数中自定义类的映射。
有谁知道等效类“Person”的样子:
http://maven.apache.org/guides/mini/guide-configuring-plugins.html#Mapping_Complex_Objects

<configuration>
     <person>
          <firstName>Jason</firstName>
          <lastName>van Zyl</lastName>
     </person>
</configuration>

我的自定义 mojo 看起来像这样:
/**
 * @parameter property="person"
 */
protected Person person;
public class Person {
    protected String firstName;
    protected String lastName;
}

但我总是收到以下错误:
无法解析 mojo 的配置 ... 参数 person:无法创建类的实例 ...$Person

有谁能够帮助我?

编辑:

以 Person(包括默认构造函数、getter 和 setter)作为内部类的 Mojo 类。
public class BaseMojo extends AbstractMojo {

/**
 * @parameter property="ios.person"
 */
protected Person person;

public class Person {
    /**
     * @parameter property="ios.firstName"
     */
    protected String firstName;

    /**
     * @parameter property="ios.lastName"
     */
    protected String lastName;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Person() {

    }
}

最佳答案

如果使用内部类,它需要是静态的,这样它就可以启动而不必先创建外部类。如果内部类是使用外部类的私有(private)变量创建的,那么它是有用的,但 maven 只是没有这样做。

希望下面的示例有助于解释为什么它不起作用......

public class ExampleClass {
    public class InnerInstance {}

    public static class InnerStatic {}

    public static void main(String[] args) {
      // look, we have to create the outer class first (maven doesn't know it has to do that)
      new ExampleClass().new InnerInstance();

      // look, we've made it without needing to create the outer class first
      new ExampleClass.InnerStatic();

      // mavens trying to do something like this at runtime which is a compile error at compile time
      new ExampleClass.InnerInstance();
    }
}

关于Maven Mojo 映射复杂对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37921848/

相关文章:

spring - 这是我的 spring-boot-starter-tomcat 依赖项

java - maven 无法解析 jmonkeyengine 的依赖关系

maven - 使用 Maven 3 设置 Struts2 EAR 项目

linux - 如何在shell中处理10多个参数

c# - 从链接的 dll 读取 app.exe.config 文件设置的简单方法

Maven - 如何从一个项目构建多个独立的 Maven 项目

maven - 禁用父 POM 中定义的 Maven 插件

python - 如何将关键字参数作为参数传递给函数?

C# 更好地返回列表或修改现有列表?

c# - 寻找 .NET 配置框架