【问题标题】:Maven: Extract dependency resources before testMaven:在测试前提取依赖资源
【发布时间】:2012-05-18 22:10:05
【问题描述】:

我有一个多模块 Maven 项目。一个子项目托管 XSL/XML 资源文件。另一个项目托管需要在其单元测试中使用这些文件的 Java 代码。

在依赖项的 jar 中,资源位于文件夹 xml-resources

我找到了这个example 并尝试根据我的需要进行更改:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-dependency-plugin</artifactId>
  <version>2.4</version>
  <executions>
    <execution>
      <id>resource-dependencies</id>
      <phase>process-test-resources</phase>
      <goals>
        <goal>unpack-dependencies</goal>
      </goals>
      <configuration>
        <classifier>xml-resources</classifier>
        <outputDirectory>${project.build.directory}/classes/xml-resources</outputDirectory>
      </configuration>
    </execution>
  </executions>
</plugin>

当我运行 process-test-resources 阶段时,这不会做任何事情。我确信那里有一些错误 - 我看不到我可以在哪里指定资源应该从哪里获取,&lt;classifier&gt; 似乎没有实际指定应该从哪里复制资源的源。

我迷路了,谁能告诉我该怎么做?

【问题讨论】:

    标签: maven maven-2 maven-dependency-plugin


    【解决方案1】:

    试试这样的

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-dependency-plugin</artifactId>
      <version>2.4</version>
      <executions>
        <execution>
          <id>resource-dependencies</id>
          <phase>process-test-resources</phase>
          <goals>
            <goal>unpack-dependencies</goal>
          </goals>
          <configuration>
            <includeArtifactIds>my-artifact-id</includeArtifactIds>
            <includes>foobar.txt, loremipsum.xml</includes>
            <outputDirectory>${project.build.directory}/classes/xml-resources</outputDirectory>
          </configuration>
        </execution>
      </executions>
    </plugin>
    

    查看unpack-dependencies parameters 了解详细说明或更多信息。

    【讨论】:

    • 本例中的 outputDirectory 文件夹应该是 ../test-classes/.. 而不是 ../classes/..
    • @khmarbaise 有效反对意见 - 可能更好${project.build.testOutputDirectory}/xml-resources(见Super POM
    • 谢谢你,现在复制作品。这些文件位于target/test-classes/xml-resources。但是,我无法通过getClass().getResourceAsStream("/xml-resources/file.xml") 将它们加载到我的测试代码中。我还有什么需要做的吗?
    • 啊,算了,那是我的 IDE 的错。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-20
    • 2012-03-02
    • 1970-01-01
    • 1970-01-01
    • 2011-12-26
    • 2012-09-18
    • 1970-01-01
    相关资源
    最近更新 更多