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

时间:2015-12-21 14:55:52

标签: java maven maven-plugin aether jcabi

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

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

有人有任何想法吗? 谢谢

1 个答案:

答案 0 :(得分:2)

要从自定义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来获得传递依赖性,就像您已经为项目依赖项所做的那样。