Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MavenSession not populated

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
like image 245
Ofer Lando Avatar asked Sep 29 '22 03:09

Ofer Lando


3 Answers

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!

like image 104
Ofer Lando Avatar answered Oct 16 '22 22:10

Ofer Lando


Missed Parameter annotation:

@Parameter(defaultValue = "${session}")
private MavenSession session;
like image 1
Hersh Avatar answered Oct 16 '22 22:10

Hersh


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.

like image 1
Robert Scholte Avatar answered Oct 16 '22 23:10

Robert Scholte