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

佐手、 提交于 2019-12-09 07:23:08

问题


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:


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.



来源:https://stackoverflow.com/questions/3736576/how-to-get-the-current-mavensession-or-mavenexecutionrequest-from-a-plexus-compo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!