Reputation: 824
I'm trying to create my first Maven plugin, and for that I need to access MavenSession in my Mojos. I found in many places that the following snippet should be enough, but I always end up with the mavenSession object as null, although in the Maven log (of the POM.xml using my plugin) it seems like the maven session is passed, or at least populated - but is not injected into the MavenSession object.
Can anyone please tell me what I am missing?
Thanks!
/**
* The Maven Session
*
* @required
* @readonly
* @parameter
* expression="${session}"
*/
private MavenSession mavenSession;
I also added the following to the POM.xml of the plugin (based on a comment I found somewhere):
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>3.2.5</version>
</dependency>
And this from the log:
[DEBUG] Configuring mojo 'com.ofernicus.helpers:resource-helper:1.0-SNAPSHOT:iterate' with basic configurator -->
[DEBUG] (f) session = org.apache.maven.execution.MavenSession@1a785a79
[DEBUG] (f) mavenProject = MavenProject: com.ofernicus.consumers:resource-helper-consumer:1.0-SNAPSHOT @ C:\Users\oferlan\workspaces\Maven\PluginConsumer\resource-helper-consumer\pom.xml
Upvotes: 5
Views: 3312
Reputation: 824
Thanks to the responses here, I eventually found the issue:
I was trying to access the mavenSession and mavenProject from a method that was called from the execute() method. I assumed that once injected, these members are accessible and populated everywhere in the scope of my Mojo - which is wrong. I moved my code into the execute() method itself and the issue was resolved.
Thanks, everyone!
Upvotes: 3
Reputation: 12335
It looks like you have references to two fields: In your code you call this field mavenSession
, but when looking at the output of Maven it refers to session
. It looks like you're questioning one which isn't properly injected with a MavenSession
.
Upvotes: 1
Reputation: 4088
Maven uses plexus
under the hood to inject the components/elements from the maven project/pom.xml
into a plugin project, so you must make sure you include the relevant dependencies.
Here's a complete list of dependencies you can include to your pom.xml
to bring all the necessary dependencies transitively into your project.
And prefer mojo annotations to mojo javadoc tags. You can get a complete picture of the plugin tools involved reading this.
<properties>
<pluginTools.version>3.3</pluginTools.version> <!-- be sure to use the latest version in here -->
</properties>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-tools-api</artifactId>
<version>${pluginTools.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>${pluginTools.version}</version>
<scope>provided</scope>
</dependency>
Upvotes: 0
Reputation: 640
Missed Parameter annotation:
@Parameter(defaultValue = "${session}")
private MavenSession session;
Upvotes: 1