java - 排除某些项目的测试

标签 java maven

抱歉,我是 Java 新手,我想知道如何避免我的依赖项项目的测试执行。

我有一个 Proj3,它有两个依赖项:Proj1 和 Proj2(以及其他)。

如果我执行mvn clean install,不仅会执行当前 Proj3 的测试,还会执行我的依赖项 Proj1 和 Proj2 的测试。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.proj.environment.tests.api</groupId>
<artifactId>environment_Api_Tests</artifactId>
<version>437r21</version>
<packaging>pom</packaging>

<properties>
    <cucumber.version>4.3.1</cucumber.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.version>437r21</project.version>
</properties>

<dependencies>
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>${cucumber.version}</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-junit</artifactId>
        <version>${cucumber.version}</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>com.proj.environment</groupId>
        <artifactId>Proj1</artifactId>
        <version>${project.version}</version>

    </dependency>

    <dependency>
        <groupId>com.proj.environment</groupId>
        <artifactId>Proj2</artifactId>
        <version>${project.version}</version>

    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
                <encoding>UTF-8</encoding>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.18.1</version>
            <configuration>
                <testFailureIgnore>true</testFailureIgnore>
                <includes>
                    <exclude>**/*Test.java</exclude>
                </includes>
            </configuration>
        </plugin>
    </plugins>
</build>

<modules>
    <module>../Proj1</module>
    <module>../Proj2</module>        
</modules>

非常感谢,

最佳答案

您显示的 POM 适用于 environment_Api_Tests项目是 aggregation (parent) project 。这意味着

... if a Maven command is invoked against the parent project, that Maven command will then be executed to the parent's modules as well.

因此,目前还不清楚您的情况到底是什么。 取决于它与 Proj3 有什么关系你提到的,有不同的答案:

如果environment_Api_Tests项目(您发布的 POM)就是您所说的 Proj3

针对其模块执行命令是完全正常的(不要将其与依赖项混淆),并且从模块中排除测试确实没有意义。此外,您不能在聚合(父)项目本身中进行测试。因此,如果您只想执行某些模块,那么最好引入另一个仅包含这些模块的聚合项目。您可以进一步聚合聚合项目。

<dependencies>这里与此无关。该部分在聚合项目中所做的唯一事情是为所有 <modules> 提供依赖项。 。在你的情况下,这意味着:

  • Proj1取决于Proj1Proj2 .
  • Proj2取决于Proj1Proj2 .

这可能不是你想要的!

如果还有一个Proj3这也是 environment_Api_Tests 中的一个模块

然后您只需在该项目上执行测试,而不是在 environment_Api_Tests 上执行测试。即使Proj3取决于Proj1Proj2他们的测试将不会被执行

如果 Proj3environment_Api_Tests但它并不意味着是一个聚合项目

然后您需要将包装从 pom 更改为至jar (或无论您的目标是什么)并删除 <modules> 但保留<dependencies> 。这样项目就可以拥有自己的测试,并且不会执行依赖项的测试。

关于测试依赖项的注意事项

需要记住的一件事是测试依赖项在多模块项目中如何工作。如果一个模块中的测试依赖于另一模块中的测试,则必须 create a jar containing test classes .

关于java - 排除某些项目的测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57718587/

相关文章:

java - 如何通过 Maven 运行 stilliard/pure-ftpd

java - Spring Security 过滤器有多个 URL 拦截映射

java - 为什么访问随机访问文件会使 Java 平台崩溃?

java - 如何使用 Univocity 例程验证 CSV header ?

maven - Gradle本地依赖性测试

java - 依赖冲突并且找不到 jar 来解决该问题

java - 如何用另一个(更新)替换 Java Jackson TextNode?

java - 重复的局部变量 n

java - WebSocket握手:意外的响应代码:404-Tomcat 8

java - 有条件地构建 Maven fat jar