I know that @Parameter can be used directly in mojo, and I know that the documentation of @Parameter says
Beans injected into Mojo parameters are prepared by Sisu JSR330-based container: this annotation is only effective on fields of the Mojo class itself, nested bean injection requires Sisu or JSR330 annotations.
I tried the following methods (see MyParam
), but there are not woring:
/* MyParam */
@ToString
@Setter
@Getter
public class MyParam {
// not working
@Parameter(defaultValue = "${project.build.outputDirectory}")
private String outputDir0;
// not working
@Inject
@Parameter(defaultValue = "${project.build.outputDirectory}")
private String outputDir1;
// not working
@Parameters
@Parameter(defaultValue = "${project.build.outputDirectory}")
private String outputDir2;
}
/* mojo */
@Getter
@Setter
@Mojo(name = "my-mojo", defaultPhase = LifecyclePhase.PROCESS_RESOURCES, threadSafe = true)
public class MyMojo extends AbstractMojo {
@Parameter(required = true)
private MyParam myParam;
// working
@Parameter(defaultValue = "${project.build.outputDirectory}", required = true)
protected String outputDirectory;
public void execute() throws MojoExecutionException {
getLog().info(String.format("myParam: %s", myParam));
getLog().info(String.format("outputDirectory: %s", outputDirectory));
}
}
<!-- pom.xml -->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xxx</groupId>
<artifactId>my-mojo-maven-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>maven-plugin</packaging>
<name>my-mojo-maven-plugin</name>
<description>my-mojo-maven-plugin</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<javaVersion>8</javaVersion>
<maven.compiler.source>${javaVersion}</maven.compiler.source>
<maven.compiler.target>${javaVersion}</maven.compiler.target>
<maven-plugin.version>3.8.1</maven-plugin.version>
<maven-plugin-annotations.version>3.10.2</maven-plugin-annotations.version>
<lombok.version>1.18.30</lombok.version>
<junit.version>4.13.2</junit.version>
<slf4j.version>2.0.13</slf4j.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>${maven-plugin.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>${maven-plugin-annotations.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${slf4j.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.10.2</version>
<configuration>
<skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
</configuration>
<executions>
<execution>
<id>generate-helpgoal</id>
<goals>
<goal>helpmojo</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
<!-- using -->
<plugin>
<groupId>com.xmcx</groupId>
<artifactId>my-mojo-maven-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<executions>
<execution>
<phase>process-resources</phase>
<goals>
<goal>my-mojo</goal>
</goals>
<id>my-mojo</id>
<configuration>
<myParam>
</myParam>
</configuration>
</execution>
</executions>
</plugin>