Maven Mojo映射复杂对象 [英] Maven Mojo Mapping Complex Objects

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

问题描述

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

I'm trying to write a maven plugin, including a mapping of a custom class in mvn configuration parameters. Does anybody know how the equivalent class "Person" would look like: 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看起来像这样:

My custom mojo looks like that:

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

public class Person {
    protected String firstName;
    protected String lastName;
}

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

but I always get the following error: Unable to parse configuration of mojo ... for parameter person: Cannot create instance of class ...$Person

有人可以帮助我吗?

Mojo类,其中Person(包括默认构造函数,getter和setter)作为内部类.

Mojo class with Person (including default constructor, getter & setter) as inner class.

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() {

    }
}

推荐答案

如果使用内部类,则它必须是静态的,这样它就可以启动而不必先创建外部类.如果内部类是使用外部类的私有变量创建的,则非常有用,但是maven不会这样做.

If using an inner class it needs to be static so it can initiated without having to create the outer class first. Inner classes are useful if it's created using private variables from the outer class but maven just's not doing that.

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

Hopefully the below example helps explain why it won't work...

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映射复杂对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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