MavenProject injection not working
I am trying to write my own maven plugin and I am trying to access MavenProject with normal annotation. However, when I execute the plugin, the project field is not entered and remains null. Here's my sample code:
package xyz;
import org.apache.maven.plugin.*;
import org.apache.maven.plugins.annotations.*;
import org.apache.maven.project.*;
/**
* @goal develop
*
*/
public class Experiment extends AbstractMojo {
@Parameter( defaultValue = "${project}", readonly = true, required = true )
protected MavenProject project;
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
if (project == null) {
getLog().error("Variable ${project} is null!");
} else {
getLog().info("Variable ${project} is filled!");
}
}
}
No matter what I try, I can't get it to work, that maven automatically injects the project field into the appropriate object, I always get the information that the field is null. Any idea what I am doing wrong?
The pom file doesn't seem interesting here because I get this kind of behavior with large pores, which I have been using in my assemblies for ages. But I can reproduce it even with the simplest possible pom file:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
</project>
source to share
Just in case someone is throwing this question: I had the same problem, MavenProject did not have an annotated property added like this:
@Parameter(defaultValue = "${project}", required = true, readonly = true)
private MavenProject project;
I had, guided by the apparently pre-annotated mojo samples, - defined the target name with a Javadoc @goal tag like this:
/**
* execute my goal
* @goal mygoal
*/
public class MyGoal extends AbstractMojo {
// goal code goes here
}
It all worked after you changed it to use annotation for the mojo class like this:
@Mojo(name = "mygoal")
public class MyGoal extends AbstractMojo {
// goal code goes here
}
It seems to me that maven only looks at annotations if mojo is listed as annotation. Mixing annotations (for fields) with Javadoc tags doesn't work correctly (although I haven't researched it yet).
source to share
Since there is no pom.xml
project maven-plugin
, try the following:
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>stackoverflow.maven</groupId>
<artifactId>31111852</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>maven-plugin</packaging>
<parent>
<groupId>org.codehaus.mojo</groupId>
<artifactId>mojo-parent</artifactId>
<version>34</version>
</parent>
<dependencies>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.4</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>3.3.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.2</version>
<configuration>
<skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-component-metadata</artifactId>
<version>1.5.5</version>
<executions>
<execution>
<goals>
<goal>generate-metadata</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
File stackoveflow.Experiment.java
package stackoveflow;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
@Mojo(name = "develop")
public class Experiment extends AbstractMojo {
@Parameter( property ="project", defaultValue = "${project}", readonly = true, required = true )
protected MavenProject project;
public void execute() throws MojoExecutionException, MojoFailureException {
if (project == null) {
getLog().error("Variable ${project} is null!");
} else {
getLog().info("Variable ${project} is filled!");
}
}
}
install it locally with maven clean install
,
and after that run it mvn stackoverflow.maven:31111852:develop
source to share
Parameter input should look like this:
@Parameter( defaultValue = "${project}", readonly = true, required = true )
private MavenProject project;
If you have a parameter like readonly / required, etc., then providing the property doesn't make sense. A property means that you can change it using system properties like this:
mvn -DpropName=value ...
source to share
To use Java5 annotations, you need to enable annotation processing so that your targets are included in the META-INF / maven / plugin.xml file built into your jar.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.4</version>
<executions>
<execution>
<id>default-descriptor</id>
<phase>process-classes</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
See https://maven.apache.org/plugin-tools/maven-plugin-plugin/examples/using-annotations.html
source to share