Search code examples
javamavenmaven-3maven-plugin

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


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.

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


Solution

  • 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>