如何跳过 pom 打包的故障安全执行?

How to skip failsafe execution for pom packaging?

我想在父 pom 中使用 failIfNoTests 配置故障保护,因为我们目前正在升级 Spring 在我们的微服务中启动,如果测试仍在使用 JUnit 4 (您需要迁移它们或使用 junit-vintage)。

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <configuration>
                    <failIfNoTests>true</failIfNoTests>
                </configuration>
            </plugin>

但是问题在于,这会导致插件在父 pom.xml 的构建中也 运行。我不想把它放在 <pluginManagement> 中,因为我想确保插件在子项目中 运行ning(否则他们仍然必须声明插件,它可能会被遗忘)。

有没有办法防止 运行 pom 打包故障安全插件?

请注意,我对 surefire 也做了同样的事情,但它没有问题,因为它的目标是由 Maven 的默认生命周期管理的,这取决于 pom 包装。然而,对于故障保护,目标是在 spring-boot-starter-parent<pluginManagement> 中声明的,因此它适用于所有包装类型。

目前,我是使用基于 this question 的配置文件完成的,但我发现这很脏:

        <profile>
            <id>disable-integration-tests-for-pom-packaging</id>
            <!-- this profile detects that we are not building an artifact, so we shouldn’t run failsafe -->
            <activation>
                <file>
                    <!-- can’t rely on ${project.*} for profile activation -->
                    <missing>${basedir}/src</missing>
                </file>
            </activation>
            <properties>
                <skipITs>true</skipITs>
            </properties>
        </profile>

满满的 pom

家长:

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <!-- if changed, update spring-boot.version too! -->
        <version>2.6.6</version>
        <relativePath />
    </parent>

    <groupId>org.example</groupId>
    <artifactId>parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <configuration>
                    <failIfNoTests>true</failIfNoTests>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

mvn 验证输出:

[INFO] Scanning for projects...
[INFO] 
[INFO] -------------------------< org.example:parent >-------------------------
[INFO] Building parent 1.0-SNAPSHOT
[INFO] --------------------------------[ pom ]---------------------------------
[INFO] 
[INFO] --- maven-failsafe-plugin:2.22.2:integration-test (default) @ parent ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.619 s
[INFO] Finished at: 2022-04-14T14:07:56+02:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-failsafe-plugin:2.22.2:integration-test (default) on project parent: No tests to run! -> [Help 1]

微服务 pom:

<?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">
    <parent>
        <artifactId>parent</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
        <!-- just to be able to build since I can’t actually install the parent -->
        <relativePath>../parent</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>microservice</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

只要有集成测试,构建就应该成功(当然所有测试都成功)。

如果您希望插件在父级中不 运行,但在子级中 运行,您可以在父级 POM 的 <plugins> 部分添加以下内容:

      <plugin>
        <artifactId>maven-source-plugin</artifactId>
        <executions>
          <execution>
            <id>attach-sources</id>
            <inherited>false</inherited>
            <phase>none</phase>
          </execution>
        </executions>
      </plugin>

这是针对 maven 源代码插件的,但您也可以将其改编为故障安全插件。