简体   繁体   English

在依赖的Maven项目上测试失败

[英]Test failures on dependent maven projects

I have a multiple projects where project X is dependent on project Y. The tests on project X uses project Y which reads a config file. 我有多个项目,其中项目X取决于项目Y。对项目X的测试使用项目Y读取配置文件。 ( X --> Y --> configfile) I am excluding some configuration files from the JARs using the maven-jar-plugin. (X-> Y-> configfile)我使用maven-jar-plugin从JAR中排除了一些配置文件。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.3.1</version>
    <inherited>true</inherited>
    <configuration>
        <excludes>
                    <exclude>*.xml</exclude>
                    <exclude>*.*conf*</exclude>
                    <exclude>*.*prop*</exclude>
        </excludes>
    </configuration>
</plugin>

When running "Maven test" on my project everything works well and all test are successful. 在我的项目上运行“ Maven测试”时,一切正常,所有测试均成功。 But when running "Maven package" project X is failing its tests because project Y cannot find the config file. 但是,在运行“ Maven软件包”时,项目X无法通过测试,因为项目Y无法找到配置文件。

My best guess is that when running the "package" goal, project X runs against the already built JAR of project Y instead of its .class files. 我最好的猜测是,在运行“包”目标时,项目X将针对项目Y的已构建JAR而不是其.class文件运行。 Am I correct? 我对么? What can I do to fix this issue? 我该怎么做才能解决此问题?

Maybe you could be interested by the section How to create a jar containing test classes of the maven-jar-plugin page . 也许您对如何创建包含 maven-jar-plugin页面的 测试类jar部分感兴趣。

The idea is to create two different jars for your project Y: one test-jar besides your current packaged jar. 这个想法是为项目Y创建两个不同的jar:除了当前打包的jar之外,还有一个测试jar。 You have two executions of the maven-jar-plugin, one for your current jar and one for the test-jar, for which configuration files are not filtered. 您有两个maven-jar-plugin执行,一个用于当前jar,一个用于test-jar,其配置文件未过滤。

To allow the project X to refer to the test-jar of project Y, you would also need to add a dependency to this jar in test scope . 为了允许项目X引用项目Y的测试罐,您还需要在测试范围中为此罐添加一个依赖项。

Note that the page advises another approach which consist in creating another Maven project dedicated for your test classes for which you will not exclude any configuration files. 请注意,该页面建议了另一种方法,该方法包括创建另一个专用于您的测试类的Maven项目,您不会为此排除任何配置文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM