Maven (Surefire):从 src/test/java 复制测试资源
Maven 的 Surefire(测试)插件mvn test-compile
将 src/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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
bmargulies 给出了答案,但让我填写一些细节。
可以添加到项目 POM 的
节点,如下所示:复制
src/test/java
中的所有内容code>——包括我们不需要的.java
源代码。它还(正如 bmargulies 仅暗示的那样)覆盖并替换所有其他 POM 继承自的标准父 POM 中的默认
设置(除非继承被更改)。标准父级复制src/test/resources
,因此通过覆盖它,我们不会像往常一样复制它,这是我们不想要的。 (特别是,我这样做的全部原因是使用unitils,它需要复制unitils.properties
文件 - 这(对我来说,无论如何)在src/test/resources< 因此,
我们重新添加
src/test/resources
:按照列出的顺序进行复制,以便对于
/src/test/java
中都存在的文件(和子目录)和/src/test/resources
(和子目录)中,src/test/resources
版本是最终在test- 中的版本 :
现在我们只需要不复制
.java
文件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: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 copiessrc/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 theunitils.properties
file copied -- and that's (for me, anyway) insrc/test/resources
.So we re-add
src/test/resources
: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), thesrc/test/resources
version is the one that ends up intest-classes
.Now we just need to not copy the
.java
files:资源复制全部由 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>
.当我将测试配置放入 src/test/resources 文件夹(类似于源文件的 src/test/java )时,它对我有用的唯一方法。在测试执行期间,此文件夹中的文件将复制到类路径上的
target/test-classes
文件夹中。我不知道为什么,但下一个配置对我不起作用:The only way it worked for me when I put my test configuration into
src/test/resources
folder (analogue ofsrc/test/java
for source files). Files from this folder is copied to thetarget/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:我遇到了同样的问题。以上都没有为我解决这个问题。我的 Pom 有以下内容:
我必须删除两者,然后插件才能工作。希望这有帮助。显然,当您更改输出目录时,该插件不喜欢。
I ran into the same issue. None of the above fixed it for me. My Pom had the following:
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.
是的,使用 antrun 插件 可以轻松实现这一点。
Yes, this is easily achieved with the antrun plugin.