以编程方式解决maven插件中的自定义依赖关系



我有一个自定义的maven插件。为了检索项目的依赖项,我使用了jcabi以太库。它可以很好地获取项目范围依赖项。但我需要的是解决插件范围的依赖关系,这样调用就会看起来像:

<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来获得传递性依赖,就像你已经对项目依赖所做的那样。

最新更新