英文:
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 = "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 entries on the usage side
<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>
Now, I was expecting that during the plugin execution, it would call setDestinationDirectory
method. But it doesn't. @Parameter(property="...")
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 = "destinationDirectory", defaultValue = "${project.build.directory}/generated-resources")
private String _destinationDirectory;
Renaming the variable:
@Parameter(defaultValue = "${project.build.directory}/generated-resources")
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-plugin
和maven-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.
<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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论