Search code examples
mavenmaven-3maven-dependencymaven-mojo

MavenProject getArtifacts returns different list on module when call from parent


In a Mojo I need to scan all of the module's dependencies where it is a plugin, for that I use mavenProject.getArtifacts(). This works fine if I build the module directly, so not from the parent, but just call "mvn clean package" in de module's directory. In that case I get all the dependencies of that module.

However if I build the whole project, then when the build reaches the module I get a totally different set of dependencies. I do not understand why. The plugin is defined in the pom of the module.

The mavenProject is obtained in the Mojo by:

@Parameter(readonly = true, defaultValue = "${project}")
private MavenProject mavenProject;

Ofcourse including:

requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME

If I build the module directly I get this:

Examining jar:file:/C:/Users/tbeuge/.m2/repository/com/planon-fm/planonee/PlanonUX/201501.0.33.0/PlanonUX-201501.0.33.0.jar!/model.xml
Examining jar:file:/C:/Users/tbeuge/.m2/repository/com/planon/tms/scheduler/SchedulerService/1.1.2/SchedulerService-1.1.2.jar!/model.xml
Examining jar:file:/C:/Users/tbeuge/.m2/repository/org/mockito/mockito-core/2.12.0/mockito-core-2.12.0.jar!/model.xml
Examining jar:file:/C:/Users/tbeuge/.m2/repository/com/google/guava/guava/23.6-jre/guava-23.6-jre.jar!/model.xml
Examining jar:file:/C:/Users/tbeuge/.m2/repository/nl/planon/querybuilder/querybuilder-api-impl/3.0.4/querybuilder-api-impl-3.0.4.jar!/model.xml
Examining jar:file:/C:/Users/tbeuge/.m2/repository/com/planon/tms/lib/sx/sx-utils201210/2.1.0/sx-utils201210-2.1.0.jar!/model.xml
Examining jar:file:/C:/Users/tbeuge/.m2/repository/nl/planon/querybuilder/querybuilder-api/1.2.0/querybuilder-api-1.2.0.jar!/model.xml
Examining jar:file:/C:/Users/tbeuge/.m2/repository/com/planonsoftware/hades/17.0.4.9-1/hades-17.0.4.9-1.jar!/model.xml
Examining jar:file:/C:/Users/tbeuge/.m2/repository/junit/junit/4.12/junit-4.12.jar!/model.xml
Examining jar:file:/C:/Users/tbeuge/.m2/repository/com/planon/tms/myclient/myclient-shared/1/myclient-shared-1.jar!/model.xml

If the module is build as part of the whole project I get:

Examining jar:file:/C:/Users/tbeuge/.m2/repository/org/mockito/mockito-core/2.12.0/mockito-core-2.12.0.jar!/model.xml
Examining jar:file:/C:/Users/tbeuge/.m2/repository/junit/junit/4.12/junit-4.12.jar!/model.xml
Examining jar:file:/C:/Users/tbeuge/.m2/repository/org/apache/commons/commons-lang3/3.7/commons-lang3-3.7.jar!/model.xml

Can anybody explain why I get different dependencies for the same module?


Solution

  • Rested set of eyes: the problem is not in the MavenProject being injected, but in the custom URLStreamHandler processing it. The handler processed a "classpath:/path/to/resource" URI, such a handler needs to be registered in the URL.setURLStreamHandlerFactory and that method only is allowed to be called once as long as the JVM is alive. The Mojo class is instantiated every time, even the static {} is called every time, so the registration calls failed after the first time Mojo instantiation. The associated exception is caught, but this situation was not very obvious, because the handler was written in-line using lambda's.

    Anyhow, it locks the artifact jars list the handler processes to the first instantiation of the Mojo.

    Now a way around that. Need to somehow bridge from that first instantiation to an instance variable...