简体   繁体   English

未注入 Maven 插件 @Parameter defaultValue

[英]Maven plugin @Parameter defaultValue not injected

I have a simple Mojo dto class:我有一个简单的 Mojo dto 类:

public Output
{

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

I expect that the field encoding will contain UTF-8 if not defined otherwise in pom.xml :如果在pom.xml没有另外定义,我希望字段encoding将包含UTF-8

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

Instead I get null.相反,我得到了空值。

Is my expectation wrong or I'm missing something important?我的期望是错误的还是我遗漏了一些重要的东西?

One things looks suspicious in your example:在您的示例中,有一件事看起来很可疑:

@Parameter annotation should usually have property (and defaultValue ) and you set the name instead. @Parameter注释通常应该具有property (和defaultValue ),而您可以设置name

Assuming you're using the correct annotation (it should be this one the source code ), make sure you use property link in this example for instance.假设您使用了正确的注释(应该是源代码中的注释),请确保在此示例中使用property链接。

Update 1更新 1

Take a look at surefire plugin source code here , for example see the parameter definition (line 544):这里查看surefire插件源代码,例如查看参数定义(第544行):

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

Note, that they use property not the name like you did in the question.请注意,他们使用的property不是您在问题中所做的name

In the pom.xml you'll be able to use it like this:pom.xml您可以像这样使用它:

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

If you want more complex XML (with inner elements) I'm not aware of the way to map it to the DTO like you did (I might be wrong though).如果你想要更复杂的 XML(带有内部元素),我不知道像你一样将它映射到 DTO 的方法(虽然我可能是错的)。

For example fabric8 maven plugin ( see the source code ) doesn't use compound objects (DTOs) and injects values directly:例如,fabric8 maven 插件( 参见源代码)不使用复合对象(DTO)并直接注入值:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM