【问题标题】:Resolving custom dependencies in maven plugin programmatically以编程方式解决 Maven 插件中的自定义依赖项
【发布时间】:2016-03-27 16:41:10
【问题描述】:

我有一个自定义的 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>

有人知道吗? 谢谢

【问题讨论】:

  • 我在您发布的 maven 资料中没有看到 jcabi-aether 模块的任何配置。
  • @vinay 因为我使用 jcabi 的后端逻辑,如我发布的链接中所述。
  • 你是否为链接中提到的 jcabi-aether 添加了依赖项?
  • 当然..刚刚更新了问题
  • 我认为您需要添加更多代码让人们看到真正的错误。我相信项目范围的依赖关系是由 maven 而不是由库解决的。您的配置无法解析 joda-time?

标签: java maven maven-plugin aether jcabi


【解决方案1】:

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

【讨论】:

  • plugin.getDependencies() 返回空列表
  • 但是相关的 pom 是否提供它们?你用的是哪个版本的maven api?
  • 我有一个自定义插件,我需要检索它自己的依赖项,包括传递性(在插件的 pom.xml 中列出,而不是在使用它的地方)。 Apache Maven 3.3.9
  • 那么这种方法行不通,很可能你需要使用maven-dependency-plugin API,但是很难找到它的使用示例,你需要查看 maven plugins source code,它是你能得到的关于这个问题的最好的(而且很可能是唯一的)文档。
猜你喜欢
  • 2012-08-01
  • 1970-01-01
  • 2013-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多