【问题标题】:Maven plugin @Parameter defaultValue not injectedMaven插件@Parameter defaultValue未注入
【发布时间】:2020-10-19 15:17:42
【问题描述】:

我有一个简单的 Mojo dto 类:

public Output
{

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

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

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

相反,我得到了 null。

我的期望是错误的还是我错过了一些重要的事情?

【问题讨论】:

  • pom.xml是怎么定义的?你使用 Maven 配置文件吗?
  • @NikolasCharalambidis 没有文件,简单的配置,添加示例
  • 下面有Outputs和输出列表的绑定类吗?

标签: java maven maven-plugin


【解决方案1】:

在您的示例中,有一点看起来很可疑:

@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;

【讨论】:

  • 我不太明白你的回答。我想通过 配置我的插件,而不是 Maven 属性。您的意思是 defaultValue 仅适用于属性,而不适用于名称?
  • @9ilsdx9rvj0lo 请看我回答的更新,希望它会变得更清楚......
猜你喜欢
  • 2016-07-15
  • 1970-01-01
  • 2016-10-21
  • 1970-01-01
  • 2014-04-03
  • 2012-06-11
  • 1970-01-01
  • 1970-01-01
  • 2014-04-26
相关资源
最近更新 更多