How to get the current MavenSession or MavenExecutionRequest from a Plexus Component
Solution 1:
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
.