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
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!
Missed Parameter annotation:
@Parameter(defaultValue = "${session}")
private MavenSession session;
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With