Maven (Surefire):从 src/test/java 复制测试资源

发布于 2024-10-03 02:51:11 字数 428 浏览 8 评论 0原文

Maven 的 Surefire(测试)插件mvn test-compilesrc/test/resources 中的文件复制到 target/test-classes代码>.它编译src/test/java中的.java,并将编译后的.class文件复制到target/test-classes.

但它不会从 src/test/java 复制资源,并且能够将测试资源与它们所在的 .java 类放在同一目录中会更方便的资源,而不是 src/test/resources 中的并行层次结构。

是否可以让 Maven 从 src/test/java 复制资源?

Maven's Surefire (testing) pluginmvn test-compile copies files in src/test/resources to target/test-classes. It compiles .java in src/test/java, and copies the compiled .class files to target/test-classes.

But it doesn't copy resources from src/test/java, and it's more convenient to be able to put test resources in the same directory as the .java classes they are resources for, than in a parallel hierarchy in src/test/resources.

Is it possible to get Maven to copy resources from src/test/java?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

鯉魚旗 2024-10-10 02:51:11

bmargulies 给出了答案,但让我填写一些细节。

可以添加到项目 POM 的 节点,如下所示:

 <testResources>
  <testResource>
    <directory>${project.basedir}/src/test/java</directory>
  </testResource>
 </testResources>

复制 src/test/java 中的所有内容code>——包括我们不需要的.java源代码。

它还(正如 bmargulies 仅暗示的那样)覆盖并替换所有其他 POM 继承自的标准父 POM 中的默认 设置(除非继承被更改)。标准父级复制src/test/resources,因此通过覆盖它,我们不会像往常一样复制它,这是我们不想要的。 (特别是,我这样做的全部原因是使用unitils,它需要复制unitils.properties文件 - 这(对我来说,无论如何)在src/test/resources< 因此,

我们重新添加 src/test/resources

<testResources>
  <testResource>
    <directory>${project.basedir}/src/test/java</directory>
  </testResource>
  <testResource>
    <directory>${project.basedir}/src/test/resources</directory>
  </testResource>
</testResources>

按照列出的顺序进行复制,以便对于 /src/test/java 中都存在的文件(和子目录)和 /src/test/resources (和子目录)中,src/test/resources 版本是最终在 test- 中的版本 :

现在我们只需要复制.java文件

<testResources>
  <testResource>
    <directory>${project.basedir}/src/test/java</directory>
    <excludes>
        <exclude>**/*.java</exclude>
    </excludes>
  </testResource>
  <testResource>
    <directory>${project.basedir}/src/test/resources</directory>
  </testResource>
</testResources>

bmargulies gave the answer, but let me fill in some details.

<testresources> can be added to the <build> node of the project's POM, like this:

 <testResources>
  <testResource>
    <directory>${project.basedir}/src/test/java</directory>
  </testResource>
 </testResources>

That copies everything in src/test/java -- including the .java source code, which we don't want.

It also (as bmargulies only hinted at) overrides and replaces the default <testResources> setting in the standard parent POM that all other POM inherit from (unless that inheritance is changed). The standard parent copies src/test/resources, so by overriding that, we don't get that copied as usual, which we don't want. (In particular, my whole reason for doing this is to use unitils, which wants the unitils.properties file copied -- and that's (for me, anyway) in src/test/resources.

So we re-add src/test/resources:

<testResources>
  <testResource>
    <directory>${project.basedir}/src/test/java</directory>
  </testResource>
  <testResource>
    <directory>${project.basedir}/src/test/resources</directory>
  </testResource>
</testResources>

That copies in the order listed, so that for files that exist in both /src/test/java (and subdirectories) and in /src/test/resources (and subdirectories), the src/test/resources version is the one that ends up in test-classes.

Now we just need to not copy the .java files:

<testResources>
  <testResource>
    <directory>${project.basedir}/src/test/java</directory>
    <excludes>
        <exclude>**/*.java</exclude>
    </excludes>
  </testResource>
  <testResource>
    <directory>${project.basedir}/src/test/resources</directory>
  </testResource>
</testResources>
回忆躺在深渊里 2024-10-10 02:51:11

资源复制全部由 maven-resource-plugin 完成,如果您阅读其文档,您将看到如何从 src/test/java 添加资源复制。

请参阅 http://maven.apache.org/plugins/maven-resources-plugin/testResources -mojo.html 用于测试资源目标,包含在默认生命周期中。

然后查看 http://maven.apache.org/pom.html,并查找 ;

The resource copying is all done by the maven-resource-plugin, and if you read the doc thereof you will see how to add copying of resources from src/test/java.

See http://maven.apache.org/plugins/maven-resources-plugin/testResources-mojo.html for the test-resources goal, which is included in the default lifecycle.

And then see http://maven.apache.org/pom.html, and look for <testResources>.

り繁华旳梦境 2024-10-10 02:51:11

当我将测试配置放入 src/test/resources 文件夹(类似于源文件的 src/test/java )时,它对我有用的唯一方法。在测试执行期间,此文件夹中的文件将复制到类路径上的 target/test-classes 文件夹中。我不知道为什么,但下一个配置对我不起作用:

<testResources>
  <testResource>
    <directory>${project.basedir}/src/test/java</directory>
    <excludes>
        <exclude>**/*.java</exclude>
    </excludes>
  </testResource>
  <testResource>
    <directory>${project.basedir}/src/test/resources</directory>
  </testResource>
</testResources>

The only way it worked for me when I put my test configuration into src/test/resources folder (analogue of src/test/java for source files). Files from this folder is copied to the target/test-classes folder which is on the classpath during the tests execution. I don't know why, but the next config didn't work for me:

<testResources>
  <testResource>
    <directory>${project.basedir}/src/test/java</directory>
    <excludes>
        <exclude>**/*.java</exclude>
    </excludes>
  </testResource>
  <testResource>
    <directory>${project.basedir}/src/test/resources</directory>
  </testResource>
</testResources>
紅太極 2024-10-10 02:51:11

我遇到了同样的问题。以上都没有为我解决这个问题。我的 Pom 有以下内容:

<build>
  <outputDirectory>${basedir}/build</outputDirectory>
  <directory>${basedir}/dist</directory>
  ...

我必须删除两者,然后插件才能工作。希望这有帮助。显然,当您更改输出目录时,该插件不喜欢。

I ran into the same issue. None of the above fixed it for me. My Pom had the following:

<build>
  <outputDirectory>${basedir}/build</outputDirectory>
  <directory>${basedir}/dist</directory>
  ...

I had to remove both, and then the plugin worked. Hope this helps. Apparently the pluggin does NOT like when you change the output directory.

寄人书 2024-10-10 02:51:11

是否可以让maven复制
来自 src/test/java 的资源?

是的,使用 antrun 插件 可以轻松实现这一点。

Is it possible to get maven to copy
resources from src/test/java?

Yes, this is easily achieved with the antrun plugin.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文