8

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

Community
  • 1
  • 1
Richard
  • 1,070
  • 9
  • 22
  • Did you try executing your plugin during the **pre-clean** phase ? – lgd Feb 17 '15 at 15:43
  • I'll try that next.. something odd is happening now, the plugin doesn't seem to be running anymore. I'll let you know what happens when I get it running again. – Richard Feb 17 '15 at 15:49
  • Can elaborate a little bit more what exactly your are trying to solve? – khmarbaise Feb 17 '15 at 16:00
  • @khmarbaise It's hard to put into words. Essentially I'm trying to fix the problem that our jenkins build for the stable branch (integration) finds more recent artifacts created by our 'branch' builds. – Richard Feb 17 '15 at 16:04
  • Than you should configure [jenkins to populate the build as a Maven repository](https://wiki.jenkins-ci.org/display/JENKINS/Jenkins+Maven+Repository+Server) and reuse it by an other build. BTW: Have you changed the versions on the branch? – khmarbaise Feb 17 '15 at 16:10
  • 1
    @heRoy No. pre-clean is still after dependency resolution. This is frustrating. Maven allows you to programatically alter the repositories list in a plugin, but doesn't provide a phase where that would make any sense! – Richard Feb 17 '15 at 16:17
  • @khmarbaise What I'm trying to get to is a situation where nothing needs changing between a branch build and an integration build except for an appendix to the version number. So just as the usual process is to checkout, upversion and append -SNAPSHOT. So our process would be to checkout, upversion and appedn -BRANCHNAME etc. I'm reading your link now though. It looks interesting. I'd still like an answer to my current problem though. – Richard Feb 17 '15 at 16:19
  • Hm...Take a look at [versions-maven-plugin](http://mojo.codehaus.org/versions-maven-plugin/) ? – khmarbaise Feb 17 '15 at 17:18
  • I'm not sure how I'd use versions to fix my problem. Is there no way that I can somehow tell maven to delay dependency resolution until a particular phase? – Richard Feb 17 '15 at 17:22
  • You wrote about changing the version with something? Can you make a simple example project on github so we can see exactly what your problem is...currently i really don't understand what you really like to achieve? – khmarbaise Feb 17 '15 at 17:25
  • If I get it working, I'll post something somewhere because I think it's elegant. But at the moment it looks like it's not going to work because Maven tries to resolve dependencies before the plugins run. – Richard Feb 17 '15 at 17:38
  • 2
    Have you been able to solve this? I find myself needing to execute a plugin before artifact resolution, because the plugin would actually install some artifacts locally, and does not get a chance to do it because of the artifacts missing error! – Asu Mar 01 '19 at 23:57
  • 2
    @Asu no. I went as far as raising a request in Maven's backlog but it got stonewalled. I could have patched it myself, it's a one line fix. It's frustrating. The maven developers seem a bit cliquey. This is a long time ago now. Maybe you should try to get this fixed, perhaps you'll have more luck now? Good luck. – Richard Mar 02 '19 at 00:45
  • I have the same problem as you @Asu any updates on this? – rhowell Dec 21 '20 at 15:24
  • @rhowell No I have abandoned what I was trying to do – Asu Dec 21 '20 at 20:41

1 Answers1

1

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

khmarbaise
  • 92,914
  • 28
  • 189
  • 235