maven插件API:使用setter方法不起作用

huangapple go评论56阅读模式
英文:

maven plugin api: @Paramerter using setters doesn't work

问题

我正在为我的项目编写一个定制的maven插件。根据这里提到的说明
https://maven.apache.org/guides/plugin/guide-java-plugin-development.html#using-setters 我使用setter添加了一个@Parameter,如下所示。

```java
@Parameter(property = "destinationDirectory", defaultValue = "${project.build.directory}/generated-resources")
private String _destinationDirectory;
private Path dstDirRoot;

public void setDestinationDirectory(String destinationDirectory) {
    Path dstDir = Paths.get(destinationDirectory);
    if (dstDir.isAbsolute()) {
         this._destinationDirectory = dstDir.toString();
    } else {
         this._destinationDirectory = Paths.get(baseDir, dstDir.toString()).toString();
    }
    dstDirRoot = Paths.get(this._destinationDirectory);
}

在使用方面的Pom.xml条目

<plugin>
    <groupId>com.me.maven</groupId>
    <artifactId>my-maven-plugin</artifactId>
    <version>${project.version}</version>
    <executions>
        <execution>
            <goals>
                <goal>run</goal>
            </goals>
            <phase>generate-resources</phase>
        </execution>
    </executions>
    <configuration>
         <destinationDirectory>${project.build.directory}/myDir</destinationDirectory>
    </configuration>
</plugin>

现在,我期望在插件执行期间会调用setDestinationDirectory方法。但事实并非如此。 @Parameter(property="...") 似乎没有任何影响。

这是一个错误吗?还是我漏掉了什么?


<details>
<summary>英文:</summary>

I am writing a custom maven-plugin for my project. Following the instructions mentioned here 
https://maven.apache.org/guides/plugin/guide-java-plugin-development.html#using-setters I added a @Parameter using setters as shown below.

```java
@Parameter(property = &quot;destinationDirectory&quot;, defaultValue = &quot;${project.build.directory}/generated-resources&quot;)
private String _destinationDirectory;
private Path dstDirRoot;

public void setDestinationDirectory(String destinationDirectory) {
    Path dstDir = Paths.get(destinationDirectory);
    if (dstDir.isAbsolute()) {
         this._destinationDirectory = dstDir.toString();
    } else {
         this._destinationDirectory = Paths.get(baseDir, dstDir.toString()).toString();
    }
    dstDirRoot = Paths.get(this._destinationDirectory);
}

Pom.xml entries on the usage side

&lt;plugin&gt;
    &lt;groupId&gt;com.me.maven&lt;/groupId&gt;
    &lt;artifactId&gt;my-maven-plugin&lt;/artifactId&gt;
    &lt;version&gt;${project.version}&lt;/version&gt;
    &lt;executions&gt;
        &lt;execution&gt;
            &lt;goals&gt;
                &lt;goal&gt;run&lt;/goal&gt;
            &lt;/goals&gt;
            &lt;phase&gt;generate-resources&lt;/phase&gt;
        &lt;/execution&gt;
    &lt;/executions&gt;
    &lt;configuration&gt;
         &lt;destinationDirectory&gt;${project.build.directory}/myDir&lt;/destinationDirectory&gt;
    &lt;/configuration&gt;
&lt;/plugin&gt;

Now, I was expecting that during the plugin execution, it would call setDestinationDirectory method. But it doesn't. @Parameter(property=&quot;...&quot;) doesn't seem to have any impact.

Is this a bug? Or am I missing something?

答案1

得分: 2

如果我记得正确,当注解具有 property = destinationDirectory 时,它将从系统属性(例如 -D)或 POM 属性中读取系统属性,除非在 XML 中指定了配置部分。

如果在 XML 中指定了配置,就像你的示例一样,配置的名称将与变量的名称或指定的别名匹配(如果有)。您可以尝试以下选项,并检查是否解决了问题:

设置别名:

@Parameter(alias = "destinationDirectory", defaultValue = "${project.build.directory}/generated-resources")
private String _destinationDirectory;

重命名变量:

@Parameter(defaultValue = "${project.build.directory}/generated-resources")
private String destinationDirectory;

通常最好保持配置名称和变量名称一致,以便更容易进行维护。

英文:

If I remember correctly, when the annotation has property = destinationDirectory, it will read a system property from system properties (e.g. -D) or pom properties, unless a configuration section is specified in the XML.

mvn generate-resources -DdestinationDirectory=/path/to/dir

If a configuration is specified in the XML, which is the case in your example, the name of the configuration will match either the name of the variable or the specified alias, if any. You can try the following options and check if it solves the issue:

Setting an alias:

@Parameter(alias = &quot;destinationDirectory&quot;, defaultValue = &quot;${project.build.directory}/generated-resources&quot;)
private String _destinationDirectory;

Renaming the variable:

@Parameter(defaultValue = &quot;${project.build.directory}/generated-resources&quot;)
private String destinationDirectory;

It's usually a good practice to keep the name of the configuration and the variables consistent, for easier maintenance.

答案2

得分: 2

maven-plugin-plugin版本3.7.0开始,您可以简单地在公共setter方法上添加@Parameter注解。

您的代码可以像这样:


@Parameter(...)
public void setDestinationDirectory(String destinationDirectory) {
...
}

您还需要在您的pom.xml中定义maven-plugin-pluginmaven-plugin-annotations依赖的版本 - 两者应该具有相同的版本。

<project>

<properties>
  <maven-plugin-tools.version>3.7.1</maven-plugin-tools.version>
</properties>

<dependencies>
  <dependency>
    <groupId>org.apache.maven.plugin-tools</groupId>
    <artifactId>maven-plugin-annotations</artifactId>
    <scope>provided</scope>
    <version>${maven-plugin-tools.version}</version>
  </dependency>
</dependencies>

<build>
  <pluginManagement>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-plugin-plugin</artifactId>
      <version>${maven-plugin-tools.version}</version>
      <executions>
        <execution>
          <id>help-mojo</id>
          <goals>
            <goal>helpmojo</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </pluginManagement>
</build>

</project>
英文:

From maven-plugin-plugin version 3.7.0 you can simply add @Parameter annotation on public setter methods.

You code can looks like:


@Parameter(...)
public void setDestinationDirectory(String destinationDirectory) {
...
}

You also need to define version of maven-plugin-plugin and maven-plugin-annotations dependency in your pom.xml - both should have the same version.

&lt;project&gt;

&lt;properties&gt;
  &lt;maven-plugin-tools.version&gt;3.7.1&lt;/maven-plugin-tools.version&gt;
&lt;/properties&gt;

&lt;dependencies&gt;
  &lt;dependency&gt;
    &lt;groupId&gt;org.apache.maven.plugin-tools&lt;/groupId&gt;
    &lt;artifactId&gt;maven-plugin-annotations&lt;/artifactId&gt;
    &lt;scope&gt;provided&lt;/scope&gt;
    &lt;version&gt;${maven-plugin-tools.version&lt;/version&gt;
  &lt;/dependency&gt;
&lt;/dependencies&gt;

&lt;build&gt;
  &lt;pluginManagement&gt;
    &lt;plugin&gt;
      &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
      &lt;artifactId&gt;maven-plugin-plugin&lt;/artifactId&gt;
      &lt;version&gt;${maven-plugin-tools.version}&lt;/version&gt;
      &lt;executions&gt;
        &lt;execution&gt;
          &lt;id&gt;help-mojo&lt;/id&gt;
          &lt;goals&gt;
            &lt;goal&gt;helpmojo&lt;/goal&gt;
          &lt;/goals&gt;
        &lt;/execution&gt;
      &lt;/executions&gt;
    &lt;/plugin&gt;
  &lt;/pluginManagement&gt;
&lt;/build&gt;

&lt;/project&gt;

huangapple
  • 本文由 发表于 2023年2月14日 01:50:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75439519.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定