1

In Maven 3.x , how I can get all the dependencies of a project including the transitive in a custom maven extension (By not using Aether)

Currently I have this :

 @Component(role = AbstractMavenLifecycleParticipant.class, hint = "Test")
 public class sampleExtension extends AbstractMavenLifecycleParticipant implements LogEnabled {

   private Logger logger;

   @Override
   public void afterSessionStart(MavenSession session)
        throws MavenExecutionException {
  this.logger.info("Starting afterSessionStart()");
}

@Override
public void afterProjectsRead(MavenSession session)
        throws MavenExecutionException {
      MavenProject pr = session.getCurrentProject();

    List dependencies = pr.getDependencies();

    this.logger.info("Project name and Size of dependencies: " + pr.getArtifactId() + " : " + dependencies.size()); 
}

@Override
public void enableLogging(Logger logger) {
    this.logger = logger;
}

}

I am building this extension jar and placing it in $Maven_home/lib/ext . I picked up a random maven project (lets call it 'abc') and ran the command : mvn clean install , which shows the number of dependencies as 13 from the logs. Where as if I run mvn dependency:copy-dependencies on 'abc' , I see more than 200 dependencies copied to target folder.

Ultimately, what I want to do is, get all the dependencies of a project in the extension class and copy it to a folder.

Is there anything I am doing wrong? Please note that I am new to all this. Any help is greatly appreciated.

h-kach
  • 351
  • 2
  • 8
  • 25
  • 1
    Probably the `getDependencies()` method only returns direct dependencies and not the transitive ones. Why do you want to reinvent the wheel and not just use the dependencies plugin? – DrHopfen Aug 17 '18 at 09:07
  • yes reading about it getDependencies() only returns direct. I dont want to reinvent, I can reuse the plugin, but how do I use the dependency plugin to get all the dependencies inside my extension class? – h-kach Aug 17 '18 at 09:13
  • If you want to use it in the class I guess `getArtifacts()` is what you want (as it returns transitive dependencies as well). If copying the artifacts to a folder is what you want to achieve I still don't get why you don't use the dependency plugin to do exactly that. – DrHopfen Aug 17 '18 at 09:32
  • I tried this : session.getCurrentProject().getArtifacts() and I see that the size of the set to be 0 . I am not sure why this is the case. Well what I want to do is, when there is a build job created, I want to fetch all the dependencies in the background. I cannot do "mvn dependency:copy-dependencies" , I want to do it as part of an Maven extension. Can this be done? – h-kach Aug 17 '18 at 09:38
  • @DrHopfen any idea about this? How can I use the dependency plugin within my custom maven extension? Greatly appreciate your help – h-kach Aug 19 '18 at 16:16
  • I still don't get why you don't configure the plugin through your pom.xml. I think it is unusual to call plugin code from within a different plugin or extension. Honestly I don't know how to do that. – DrHopfen Aug 20 '18 at 08:41

1 Answers1

0

@Mojo(name = "xxx", defaultPhase = LifecyclePhase.xxx, requiresDependencyCollection = ResolutionScope.COMPILE_PLUS_RUNTIME, requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME)