如何在依赖解析之前运行 Maven 插件 [英] How to get a Maven plugin to run before Dependency Resolution

查看:73
本文介绍了如何在依赖解析之前运行 Maven 插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个以前的答案,但我不认为 接受的答案是正确的

我创建了一个 Maven 插件,它根据版本的某些方面以编程方式摆弄" 配置(所以如果该版本附加了一个分支名称,即 1.0.0-RC1).这允许在功能分支构建中使用单独的 Nexus 存储库,而无需在合并回 int 之前修改 POM.

我不能只更改分支构建上的 groupId,因为这是一个 OSGi 项目,并且 groupId 必须与源包完美匹配.

问题是在依赖解析之前似乎没有运行的 maven 生命周期的一部分.因此,永远不会配置为解析提供正确 的目标,因此 Maven 只是抱怨它无法解析.

链接的答案表明干净"在解决之前运行,但似乎并非如此.如果我将目标配置为具有清理"或验证"的默认阶段,则解决方案似乎仍会首先发生.

我可以让我的插件目标在依赖解析之前运行吗?

这似乎无法完成.我认为这需要在 Maven 中修复.在依赖解析之前,应该有地方运行需要项目"的插件,而不必深入研究 Plexus.这将允许在后续解析期间使用的存储库列表的动态配置.大概这需要在 EventDispatcher 实现中的某处进行更改(我注意到它已被弃用?).

解决方案

我建议看一下 Maven 中的EventSpy,它为工件解析RepositoryEvent.EventType 但这不能作为插件使用.>

onEvent"的实现可能如下所示:

@Overridepublic void onEvent(对象事件)抛出异常{尝试{if ( 事件实例 ExecutionEvent ){executionEventHandler((ExecutionEvent)事件);}else if ( 事件 instanceof RepositoryEvent ){repositoryEventHandler((RepositoryEvent) 事件);}else if ( MavenExecutionRequest 事件实例){executionRequestEventHandler((MavenExecutionRequest)事件);}else if ( MavenExecutionResult 事件实例){executionResultEventHandler((MavenExecutionResult)事件);}else if(DependencyResolutionRequest 事件实例){dependencyResolutionRequest((DependencyResolutionRequest) 事件);}else if(DependencyResolutionResult 事件实例){dependencyResolutionResult((DependencyResolutionResult) 事件);}}捕获(异常 e){logger.error( "异常", e );}}

在哪里

There is a previous answer to this but I don't think the accepted answer is correct

I have created a Maven plugin that programatically 'fiddles' with the <repositories> and <distributionManagement> configuration based upon some aspect of the version (so if the version has a branch name appended ie. 1.0.0-RC1). This allows for separate Nexus repositories to be used in feature branch builds without requiring POM modifications before merge back into int.

I cannot just change the groupId on branch builds because this is an OSGi project and groupId must ideally match the source package.

The problem is that there seems to be no part of the maven lifecycle that runs before dependency resolution. So the goal that would provide the correct <repository> for resolution never gets configured, so Maven just complains that it can't resolve.

The linked answer suggests that 'clean' runs before resolution, but it doesn't seem to be true. If I configure my goal to have a default phase of 'clean' or 'validate', resolution still seems to happen first.

Can I make my plugin goal run before dependency resolution?

EDIT : It seems this cannot be done. I think this needs fixing in Maven. There ought to be place to run plugins that require 'project', before dependency resolution, without having to delve around in Plexus. This would permit the dynamic configuration of the repositories list used during subsequent resolution. Presumably this needs a Change somewhere in the EventDispatcher implementation (which I note is deprecated?).

解决方案

I would suggest to take a look at the EventSpy in Maven which has such events for artifact resolution or RepositoryEvent.EventType But this will not work as a plugin.

An implementation for 'onEvent' could look like this:

@Override
public void onEvent( Object event )
    throws Exception
{
    try
    {
        if ( event instanceof ExecutionEvent )
        {
            executionEventHandler( (ExecutionEvent) event );
        }
        else if ( event instanceof RepositoryEvent )
        {
            repositoryEventHandler( (RepositoryEvent) event );
        }
        else if ( event instanceof MavenExecutionRequest )
        {
            executionRequestEventHandler( (MavenExecutionRequest) event );
        }
        else if ( event instanceof MavenExecutionResult )
        {
            executionResultEventHandler( (MavenExecutionResult) event );
        }
        else if ( event instanceof DependencyResolutionRequest )
        {
            dependencyResolutionRequest( (DependencyResolutionRequest) event );
        }
        else if ( event instanceof DependencyResolutionResult )
        {
            dependencyResolutionResult( (DependencyResolutionResult) event );
        }
    }
    catch ( Exception e )
    {
        logger.error( "Exception", e );
    }
}

where the

这篇关于如何在依赖解析之前运行 Maven 插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆