Maven Mojo Mapping Complex Objects
Asked Answered
R

1

4

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>

My custom mojo looks like that:

/**
 * @parameter property="person"
 */
protected Person person;
public class Person {
    protected String firstName;
    protected String lastName;
}

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

Can anybody help me?


EDIT:

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

    }
}
Rone answered 20/6, 2016 at 11:51 Comment(8)
Make sure Person has a default constructor with getter / setter. See also https://mcmap.net/q/2036857/-java-maven-mojo-complex-map-attribute. Can you post your package hierarchy also?Spriggs
Furthermore don't use old style XDoclet use annotation based parameter definition etc. maven.apache.org/plugin-tools/maven-plugin-tools-annotations/…Repro
@Spriggs I've added the default constructor and getter & setter for the properties (see EDIT in the origin post). Unfortunately the error still occurs.Alfredoalfresco
@Repro thx for the hint!Alfredoalfresco
Remove this @parameter property="ios.firstName", this isn't needed. And where is that class located? It must be in the same package as the Maven plugin.Spriggs
What do you mean with Maven plugin? I've several Mojo's extending the above BaseMojo (in the same package). Person is a inner class of BaseMojo. I've several other properties (simple types) in the BaseMojo and the parameters from the pom get correctly injected only the custom object "Person" produces an error.Alfredoalfresco
Ah that is a problem then. Make Person a type that is inside the same package as your other MOJOs. Not an inner type.Spriggs
thanks! that solved the problem. If you post it as an answer, I will accept it. Also: default constructor and getter & setters are not needed.Alfredoalfresco
E
1

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();
    }
}
Eurasian answered 2/7, 2018 at 14:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.