Spring Boot pom - 如何从聚合器 pom 继承配置文件和属性?

Spring Boot pom - how to inherit profiles and properties from aggregator pom?

目标

我正在向 Maven 构建添加集成测试。我想实现以下目标:

当前状态

任务变得更加复杂,因为这是一个具有令人困惑的 maven 模块结构(层次结构)的现有应用程序。我会尽力解释。

所以我有一个看起来像

的聚合器 pom(超级 pom)
<groupId>com.group.id</groupId>
<artifactId>app-name</artifactId>
<modules>
    <module>SpringBootApp1</module>
    <module>SpringBootApp2</module>
</modules>
<packaging>pom</packaging>

<profiles>
    <profile>
        <id>Local</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <!-- Only unit tests are run when the development profile is active -->
            <skip.integration.tests>true</skip.integration.tests>
            <skip.unit.tests>false</skip.unit.tests>
        </properties>
    </profile>
    <profile>
        <id>Test</id>
        <properties>
            <!-- Only integration tests are run when the test profile is active -->
            <skip.integration.tests>false</skip.integration.tests>
            <skip.unit.tests>true</skip.unit.tests>
        </properties>
    </profile>
</profiles>

和一些不继承自 super pom 的模块 pom(它们继承自 spring-boot-starter-parent)并且看起来像

<groupId>com.group.id</groupId>
<artifactId>spring-boot-app</artifactId>
<packaging>jar</packaging>
<name>Spring Boot app</name>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.10.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<build>
    <finalName>SpringBootApp1</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.21.0</version>
            <configuration>
                <skipTests>${skip.unit.tests}</skipTests>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.21.0</version>
            <executions>
                <!--  Invokes both the integration-test and the verify goals of the Failsafe Maven plugin -->
                <execution>
                    <id>integration-tests</id>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                    <configuration>
                        <skipTests>${skip.integration.tests}</skipTests>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

如您所见,我在聚合器 pom 中定义了 skip.integration.testsskip.unit.tests 属性(属性 值对于不同的配置文件是不同的)并尝试在模块 poms 中使用它们。

问题

当我执行 mvn clean test -P Testmvn clean verify -P Test 到 运行 测试时,我发现未应用属性(例如,虽然 skip.unit.tests 是 [=19=,但执行了单元测试]).

我可以将聚合器 pom 声明为模块 pom 的父级,也许它会解决问题,但是模块 pom 已经将 spring-boot-starter-parent 声明为其父级。

问题

ps. 我向您保证我遵守命名约定:单元测试的命名方式为 *Test.java,集成测试的命名方式为 *IT.java.所以 maven-failsafe-plugin 和 maven-surefire-plugin 区分一下。

如果您可以将聚合器作为模块的父级,而不是将聚合器配置为将 spring-boot-starter-parent 作为父级,则此问题将得到解决。(假设您的聚合器当前没有任何父级)。

或者您可以从命令行通过跳过测试 属性,例如 -Dskip.unit.tests=true(您调用 mvn clean verify)。

附带说明一下,当您执行 mvn test 命令时,您不必指定要跳过的集成测试,因为它在生命周期的后期。(它不会以任何方式执行)