【问题标题】:Spring Boot pom - how to inherit profiles and properties from aggregator pom?Spring Boot pom - 如何从聚合器 pom 继承配置文件和属性?
【发布时间】:2018-08-24 09:01:33
【问题描述】:

目标

我正在向 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>

以及一些不继承自超级 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.tests 和 skip.unit.tests 属性(不同配置文件的属性值不同)并尝试在模块 pom 中使用它们。

问题

当我执行mvn clean test -P Test 或mvn clean verify -P Test 来运行测试时,我看到属性没有被应用(例如,虽然skip.unit.tests 是true,但执行了单元测试)。

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

问题

  • 是否可以使用聚合器 pom 中的属性而不使其成为父级?
  • 如何使用聚合器 pom 中的属性将集成测试与单元测试分开?

ps. 我向您保证,我遵守命名约定:单元测试命名为*Test.java,集成测试命名为*IT.java。所以 maven-failsafe-plugin 和 maven-surefire-plugin 将它们区分开来。

【问题讨论】:

    标签: java maven spring-boot properties integration-testing


    【解决方案1】:

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

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

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

    【讨论】:

    • 我无法将聚合器设为父级。感谢您的指导!
    猜你喜欢
    • 2011-11-26
    • 1970-01-01
    • 1970-01-01
    • 2012-12-22
    • 2015-02-02
    • 2011-07-26
    • 1970-01-01
    • 2013-09-28
    • 1970-01-01
    相关资源
    最近更新 更多