Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a Maven plugin to run before Dependency Resolution

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?).

like image 855
Richard Avatar asked Feb 17 '15 15:02

Richard


1 Answers

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

like image 53
khmarbaise Avatar answered Oct 19 '22 10:10

khmarbaise