如何从Plexus组件获取当前的MavenSession或MavenExecutionRequest [英] How to get the current MavenSession or MavenExecutionRequest from a Plexus Component
问题描述
我创建了一个Plexus组件来容纳要创建的多个Mojo的通用逻辑.我已经能够传递localRepository
之类的信息,并从Mojo中传递项目(以及测试用例).
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).
我想知道是否有一种方法可以从组件内部获取MavenSession
或MavenExecutionRequest
,而不必从Mojo传递它们作为参数.我知道我的组件将通过Maven Mojo运行.
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.
在这种情况下,我正在使用Maven 2.
I'm using Maven 2 in this instance.
推荐答案
我不相信有,我也不认为这是个好主意.
I don't believe there is, nor would I think it's a good idea.
根据我的理解,您要实现的目标是拥有一个类似Commons的组件,该组件将用于多个mojo.这需要在&中传递当前的Maven会话.用于随后传回值.
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.
然而,Maven的工作方式是,它在几乎类似于容器的环境中运行来自插件的目标.每个插件都可以在特定的 Maven阶段内运行,由<executions />
定义.这使Maven可以分离构建,测试,检查,部署等的复杂性.除非我没有记错,否则调用mvn clean install
是两个单独的执行,一个用于运行clean
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
.
因此,假设MavenSession
和后续的MavenExecutionRequest
代表上一个调用中使用的相同实例是不安全的. clean
是一个单独的调用,不应在新的会话中维护其逻辑.
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.
所以,我的基本意思是,尽管拥有一个通用的神经丛组件是一种好习惯,特别是要在您的mojo上保持一致性,但这是正确的做法(几乎
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.
我希望这是您要寻找的答案.相当长,但是要了解为什么需要提供会话,请考虑Maven
.
I hope that's the answer you're looking for. Rather long, but to understand why you need to provide the session, think Maven
.
这篇关于如何从Plexus组件获取当前的MavenSession或MavenExecutionRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!