Search code examples
javamavenmaven-pluginaetherjcabi

Resolving custom dependencies in maven plugin programmatically


I have a custom maven plugin. In order to retrieve project's dependencies I use jcabi-aether library. It works fine for getting the project-scope dependencies. But what I need is to resolve plugin-scope dependencies so the call will look like:

<plugin>
    <groupId>com.maven</groupId>
    <artifactId>some-maven-plugin</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <configuration>
          <some>${some}/path</some>
    </configuration>
    <dependencies>
          <dependency>
               <groupId>joda-time</groupId>
               <artifactId>joda-time</artifactId>
               <version>2.8.1</version>
               <classifier>sources</classifier>
          </dependency>
   </dependencies>
</plugin>
...
<dependency>
  <groupId>com.jcabi</groupId>
  <artifactId>jcabi-aether</artifactId>
  <version>0.10.1</version>
</dependency>

Does anybody has any idea? Thank you


Solution

  • To retrieve plugin scope dependencies from the execute method of your custom Mojo, you need to loop over the elements of the build as following:

    Build build = super.getProject().getBuild();
    if (null != build) {
        List<Plugin> plugins = build.getPlugins();
        for (Plugin plugin : plugins) {
            List<Dependency> dependencies = plugin.getDependencies();
            // you can then use your custom code here or just collected them for later usage. 
            // An example of what you can get, below
            for (Dependency dependency : dependencies) {
                getLog().info(dependency.getGroupId());
                getLog().info(dependency.getArtifactId());
                getLog().info(dependency.getVersion());
                getLog().info(dependency.getClassifier());
                getLog().info(dependency.getScope());
                // etc.
            }
        }
    }
    

    Once you have them, I believe you can then use the Aether API to get transitive dependencies as you already did for project dependencies.