MavenSession is not populated

I am trying to create my first Maven plugin and for this I need to access MavenSession in my Mojos. In many places, I found that the following snippet should be enough, but I always get the mavenSession object as null, although in the Maven log (from POM.xml using my plugin) it seems that the maven session has been transferred or at least is full - But not injected into the MavenSession object.

Can someone 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 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 is from the magazine:

 [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 
+5
source share
4 answers

Thanks to the answers here, I eventually found the problem:

I tried to access mavenSession and mavenProject from a method that was called from the execute () method. I assumed that after they were introduced, these members are accessible and populated throughout my Mojo area - this is wrong. I moved my code to the execute () method and the problem was resolved.

Thank you all!

+2
source

Annotations of missing parameters:

 @Parameter(defaultValue = "${session}") private MavenSession session; 
+1
source

It looks like you have references to two fields: in your code, you call this mavenSession field, but when viewing Maven output, it refers to session . It looks like you are asking the question that mavenSession is being entered mavenSession .

+1
source

Maven uses plexus under the hood to insert components / elements from the maven / pom.xml project into the plugin project, so you need to make sure that you include the appropriate dependencies.

Here is a complete list of dependencies that you can include in your pom.xml to transit all the necessary dependencies into your project.

And prefer mojo annotations to mojo javadoc tags . You can get a complete understanding of the plug-in tools for 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> 
0
source

Source: https://habr.com/ru/post/1209879/


All Articles