A.R.K.S
A.R.K.S

Reputation: 1802

session.getCurrentProject() returns null - Maven extensions

I'm trying to write a maven extension to calculate the duration of each build session. Here is my extension :

  @Parameter( defaultValue = "${project}", readonly = true, required = true )
  private MavenProject project;

  @Component(role = AbstractMavenLifecycleParticipant.class)
  public class BuildTimeLogger extends AbstractMavenLifecycleParticipant {

  public void afterSessionStart(MavenSession session) throws MavenExecutionException {

      System.out.println("${project}: "+project);
      System.out.println("Top level project:"+session.getTopLevelProject());
 System.out.println("session.getcurrentproject():"+session.getCurrentProject());       }  
    }

I'm not sure why all the above print statements print null. Am I using the session object properly? My understanding is that Maven is supposed to send all the session details of the project being built. I tried this on different projects but doesn't seem to work for me.

Upvotes: 1

Views: 236

Answers (1)

A.R.K.S
A.R.K.S

Reputation: 1802

I played around with this some more and figured that it does print these details when printed from afterProjectsRead() instead.

Pasting from official documentation:

afterSessionStart - Invoked after MavenSession instance has been created. This callback is intended to allow extensions to inject execution properties, activate profiles and perform similar tasks that affect MavenProject instance construction.

afterProjectsRead - Invoked after all MavenProject instances have been created. This callback is intended to allow extensions to manipulate MavenProjects before they are sorted and actual build execution starts.

So I think the MavenProject instance is not created yet at afterSessionStart(). Think it could definitely be more descriptive :-/

Upvotes: 1

Related Questions