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