以编程方式解决 Maven 插件中的自定义依赖项

Resolving custom dependencies in maven plugin programmatically

我有一个自定义的 Maven 插件。为了检索项目的依赖项,我使用 jcabi-aether 库。它适用于获取项目范围的依赖项。但我需要的是解决插件范围的依赖关系,所以调用看起来像:

<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>

有人知道吗? 谢谢

要从自定义 Mojo 的 execute 方法中检索插件范围依赖项,您需要按以下方式遍历构建的元素:

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.
        }
    }
}

一旦你有了它们,我相信你可以使用 Aether API 来获得传递依赖,就像你已经为项目依赖所做的那样。