如何从 Plexus 组件中获取当前的 MavenSession 或 MavenExecutionRequest [英] How to get the current MavenSession or MavenExecutionRequest from a Plexus Component
问题描述
我创建了一个 Plexus 组件来容纳我想要创建的几个 Mojo 的通用逻辑.我已经能够从 Mojo(和测试用例)传递 localRepository
和项目等信息.
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.
据我了解,您的目标是拥有一个类似公共的组件,该组件将用于多个 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.
所以,我实质上要说的是,虽然拥有一个通用的 plexus 组件是一种很好的做法,尤其是为了保持你的 mojo 的一致性,但它是正确的做法(几乎 ioc),每次都提供适当的会话.
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屋!