58

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.

Michael
  • 1,014
  • 1
  • 8
  • 23
Thomas
  • 2,095
  • 18
  • 24
  • I know the question is quite old. Thomas, do you want to "reuse" the identical session across several phases or do you just need a reference to the current MavenSession of the phase? – Mirko Friedenhagen Dec 07 '12 at 23:22
  • Hmm.. if there was, I guess it would not be thread safe any more. – eckes Feb 21 '14 at 20:43

1 Answers1

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.

Michael
  • 1,014
  • 1
  • 8
  • 23