How to get the current MavenSession or MavenExecutionRequest from a Plexus Component

2.5k views Asked by At

I created a Plexus component to house common logic for several Mojos I want to create. I have been able to pass information like localRepository and project from the Mojo (and test cases).

I was wondering if there is a way to get the MavenSession or MavenExecutionRequest from within the component without having to pass them as parameters from a Mojo. I know my component will be running from a Maven Mojo.

I'm using Maven 2 in this instance.

1

There are 1 answers

0
Michael On

I don't believe there is, nor would I think it's a good idea.

From my understanding, what you're aiming to achieve is have a commons-like component, that will be used for several mojos. This requires the current Maven session to be passed in & used to subsequently pass back a value.

The way Maven works however, is that it runs goals from plugins in an almost container-like environment. Each plugin is allowed to act within a particular Maven Phase, as defined by the <executions />. This allows Maven to separate the complexities of building, testing, checking, deploying etc. etc. Calling mvn clean install, unless I'm mistake, is two separate executions, one to run the clean build lifecycle, followed by the install.

It is therefore not safe to assume that the MavenSession and subsequent MavenExecutionRequest represent the same instance used in a previous call. clean is a separate call, whose logic shouldn't be maintained in a new session.

So, what I'm essentially saying, is that whilst having a common plexus component is a good practice, particularly to maintain consistency across your mojos, it is right practice (almost ioc), to provide the appropriate session each time.

I hope that's the answer you're looking for. Rather long, but to understand why you need to provide the session, think Maven.