【问题标题】:Maven Artifact.getFile() returns NULLin a multi module projectMaven Artifact.getFile() 在多模块项目中返回 NULL
【发布时间】:2014-01-17 13:24:45
【问题描述】:

我正在一个多模块项目中开发一个 Maven 插件,我想稍后在同一个项目中使用它。这是项目的布局:

sk:a:1.0:pom
   |
   --sk:a0:1.0:jar (the custom plugin)
   |
   --sk:a1:1.0:pom
   |  |
   |  --sk:a11:1.0:jar (a simple JAVA jar)
   |  |
   |  --sk:a12:1.0:jar (a simple JAVA jar)
   |
   sk:a2:1.0:pom

sk:a2:1.0 依赖于 sk:a11:1.0 和 sk:a12:1.0 和插件 sk:a0:1.0:

<project>
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>sk</groupId>
    <artifactId>a</artifactId>
    <version>1.0</version>
  </parent>
  <artifactId>a2</artifactId>
  <dependencies>
    <dependency>
      <groupId>sk</groupId>
      <artifactId>a11</artifactId>
      <version>1.0</version>
    </dependency>
    <dependency>
      <groupId>sk</groupId>
      <artifactId>a12</artifactId>
      <version>1.0</version>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>sk</groupId>
        <artifactId>a0</artifactId>
        <version>1.0</version>
        <executions>
        ...
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

来自 sk:a0:1.0 的自定义插件在其 execute() 中调用

Set<Artifact> artifacts = project.getDependencyArtifacts();

方法并迭代所有工件,其中:

for(Iterator<Artifact> iterator = artifacts.iterator(); iterator.hasNext();)
{
   Artifact artifact = iterator.next();
   getLog().info((artifact.getFile() == null ? "artifact NULL" : "artifact OK"));
}

现在它显示“artifact NULL”。在我的插件中,我需要能够读取工件的文件(如 java.io.File)。如何获取每个依赖项的文件?

谢谢,

SK

【问题讨论】:

  • 你能描述一下你想用这个插件实现什么吗?

标签: maven plugins dependencies


【解决方案1】:

看起来问题在于 Maven 尚未解决项目的所有依赖项,在执行你的 mojo 时。

因此,例如,如果您的 mojo 在 compile 阶段运行,那么 maven 尚未解决 test 依赖项(那些在您的 pom 中标记为 test 的依赖项),对于所有这些 test 工件,没有文件将还可以使用。

您可以使用@requiresDependencyResolution &lt;required-classpath&gt; mojo javadoc 标记,告诉 maven 解决所需的依赖关系。

documentation 中,它说明了有关此注释的以下内容:

将此 Mojo 标记为需要在执行之前解析指定类路径中的依赖项。

因此,在您的 Mojo 中,您只需在您的 mojo 的 javadoc 中添加以下注释:

/**
 * @goal your-goal
 * @requiresDependencyResolution test
 */
public class YourMojo extends AbstractMojo

这基本上会告诉 maven 解决将包含在 test 类路径中的所有依赖项(基本上是所有依赖项)。所以你的artifact.getFile() 调用现在应该总是返回一个文件。

【讨论】:

    猜你喜欢
    • 2018-02-06
    • 1970-01-01
    • 1970-01-01
    • 2011-02-01
    • 2021-02-09
    • 2011-01-04
    • 2017-10-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多