Maven plugin/extension 动态更新依赖版本
Maven plugin/extension to dynamically update dependency version
我想动态分配我的 Maven 项目依赖项的版本(请不要问为什么 - 我知道这不是一个可取的模式)。据我所知,我必须创建 Maven Extension 才能实现此目的,因为调用常规插件的时间太晚了。
因此,我尝试在 EventSpy 中捕获 Maven 事件,我还尝试了 AbstractMavenLifecycleParticipant。使用这些我能够收到通知,但如何实际进行更改本身 - 如何让 Maven 开始使用这个新的更新版本?我想我需要以某种方式更改 Maven 反应器中的依赖版本....但是如何?
我知道 maven-version-plugin 中有解决方案,但这需要将其作为两步工作:首先操作 pom.xml,然后 运行 实际构建。但我需要在一个 maven 运行.
内完成
有什么想法吗?提前谢谢。
编辑
示例 pom.xml 文件来说明情况
库模块:
我的库pom.xml
<groupId>foo.bar</groupId>
<artifactId>my-lib</artifactId>
<version>1.0.5</version>
我的应用程序pom.xml
<groupId>foo.bar</groupId>
<artifactId>my-app</artifactId>
<version>2.5.0</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
...
<dependency>
<groupId>foo.bar</groupId>
<artifactId>my-lib</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependencies>
现在,当我构建我的应用程序时,我需要根据我在 nexus 中找到的最新发布版本动态分配我的库版本(我知道如何获得正确的版本 - 假设它是 1.0.5)。但是如何进行某种预处理来更改反应器中的版本,以便 Maven 将使用 1.0.5 版本?
好的。好像我明白了 ;-) 感谢 khmarbaise 的评论。我已经看过这个页面,但没有注意这个。
所以我创建了 MavenLifecycleParticipant,我在其中计算所需的版本并将其设置为用户 属性:
@Component(role = AbstractMavenLifecycleParticipant.class, hint = "my-dep")
public class DependencyLifecycleParticipant extends AbstractMavenLifecycleParticipant
{
@Override
public void afterSessionStart(MavenSession session) throws MavenExecutionException {
session.getUserProperties().setProperty("MyVersion", "1.2.3");
}
}
现在在我的 pom.xml 中,我可以使用类似的东西:
<version>${MyVersion}</version>
在依赖项和项目版本中(这对我来说很不错 ;-)
我想动态分配我的 Maven 项目依赖项的版本(请不要问为什么 - 我知道这不是一个可取的模式)。据我所知,我必须创建 Maven Extension 才能实现此目的,因为调用常规插件的时间太晚了。
因此,我尝试在 EventSpy 中捕获 Maven 事件,我还尝试了 AbstractMavenLifecycleParticipant。使用这些我能够收到通知,但如何实际进行更改本身 - 如何让 Maven 开始使用这个新的更新版本?我想我需要以某种方式更改 Maven 反应器中的依赖版本....但是如何?
我知道 maven-version-plugin 中有解决方案,但这需要将其作为两步工作:首先操作 pom.xml,然后 运行 实际构建。但我需要在一个 maven 运行.
内完成有什么想法吗?提前谢谢。
编辑
示例 pom.xml 文件来说明情况
库模块:
我的库pom.xml
<groupId>foo.bar</groupId> <artifactId>my-lib</artifactId> <version>1.0.5</version>
我的应用程序pom.xml
<groupId>foo.bar</groupId> <artifactId>my-app</artifactId> <version>2.5.0</version> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> </dependency> ... <dependency> <groupId>foo.bar</groupId> <artifactId>my-lib</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependencies>
现在,当我构建我的应用程序时,我需要根据我在 nexus 中找到的最新发布版本动态分配我的库版本(我知道如何获得正确的版本 - 假设它是 1.0.5)。但是如何进行某种预处理来更改反应器中的版本,以便 Maven 将使用 1.0.5 版本?
好的。好像我明白了 ;-) 感谢 khmarbaise 的评论。我已经看过这个页面,但没有注意这个。
所以我创建了 MavenLifecycleParticipant,我在其中计算所需的版本并将其设置为用户 属性:
@Component(role = AbstractMavenLifecycleParticipant.class, hint = "my-dep")
public class DependencyLifecycleParticipant extends AbstractMavenLifecycleParticipant
{
@Override
public void afterSessionStart(MavenSession session) throws MavenExecutionException {
session.getUserProperties().setProperty("MyVersion", "1.2.3");
}
}
现在在我的 pom.xml 中,我可以使用类似的东西:
<version>${MyVersion}</version>
在依赖项和项目版本中(这对我来说很不错 ;-)