【问题标题】:Maven Mojo Mapping Complex ObjectsMaven Mojo 映射复杂对象
【发布时间】:2016-10-21 16:03:53
【问题描述】:

我正在尝试编写一个 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() {

    }
}

【问题讨论】:

  • 确保Person 有一个带有getter / setter 的默认构造函数。另见stackoverflow.com/a/33065304/1743880。你也可以发布你的包层次结构吗?
  • 此外,不要使用旧式 XDoclet 使用基于注释的参数定义等。maven.apache.org/plugin-tools/maven-plugin-tools-annotations/…
  • @Tunaki 我已经为属性添加了默认构造函数和 getter & setter(请参阅原始帖子中的编辑)。不幸的是,错误仍然发生。
  • @khmarbaise thx 提示!
  • 那是个问题。使 Person 成为与其他 MOJO 位于同一包内的类型。不是内部类型。

标签: maven parameters configuration mojo


【解决方案1】:

如果使用内部类,它需要是静态的,这样它就可以在不必先创建外部类的情况下启动。如果内部类是使用外部类的私有变量创建的,那么它是有用的,但 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();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-13
    • 2012-10-16
    • 1970-01-01
    • 2018-10-27
    • 1970-01-01
    相关资源
    最近更新 更多