Maven插件@Parameter defaultValue未注入

问题描述

我有一个简单的Mojo dto类:

public Output
{

  @Parameter(name="target",required=true)
  private File location;
  
  @Parameter(name="encoding",defaultValue="UTF-8")
  private String encoding;
 
// getters and setters 
}  

如果没有在encoding中定义,我希望字段UTF-8将包含pom.xml

        <plugin>
            <!-- my plugin -->
            <configuration>
                <outputs>
                    <output>
                        <target>${basedir}/src/main/resources/target.json</target>
                    </output>
                </outputs>
            </configuration>
        </plugin>

相反,我得到空值。

我的期望是错误的还是缺少重要的东西?

解决方法

在您的示例中有一件事情看起来可疑:

@Parameter批注通常应包含property(和defaultValue),而您应设置name

假设您使用的注释正确(应该是the source code), 确保您使用this example中的property链接。

更新1

看看surefire插件源代码here,例如,请参阅参数定义(第544行):

 @Parameter( property = "forkCount",defaultValue = "1" )
 private String forkCount;

请注意,他们使用property而不是您在问题中所做的name

pom.xml中,您可以像这样使用它:

<plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-surefire-plugin</artifactId>
       <version>...</version>
       <configuration>              
          <forkCount>12345</forkCount>
          ...
       </configuration>
    </plugin>

如果您想要更复杂的XML(带有内部元素),我不知道像您一样将其映射到DTO的方法(尽管我可能是错的)。

例如fabric8 maven插件(see the source code)不使用复合对象(DTO),而是直接注入值:

@Parameter(property = "fabric8.kubernetesManifest",defaultValue = "${basedir}/target/classes/META-INF/fabric8/kubernetes.yml")
private File kubernetesManifest;