代码之家  ›  专栏  ›  技术社区  ›  naXa stands with Ukraine

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

  •  2
  • naXa stands with Ukraine  · 技术社区  · 7 年前

    目标

    我正在将集成测试添加到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>
    

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

    问题

    当我执行 mvn clean test -P Test mvn clean verify -P Test 要运行测试,我看到没有应用属性(例如,虽然 跳过单元测试 true )。

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

    问题

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

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

    1 回复  |  直到 4 年前
        1
  •  0
  •   miskender    7 年前

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

    或者可以通过命令行传递skip test属性,如 -Dskip.unit.tests=true (您将mvn称为clean verify)。

    作为旁注,当您执行 mvn test 命令,您不必指定要跳过的集成测试,因为它是生命周期的后期。(不会以任何方式执行)