简体   繁体   English

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

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

Objectives 目标

I'm adding integration tests to a Maven build. 我正在向Maven构建中添加集成测试。 I want to achieve the following goals: 我要实现以下目标:

  • Only unit tests are run by default. 默认情况下,仅运行单元测试。
  • It must be possible to run only integration tests. 必须只能运行集成测试。

Current state 当前状态

The task becomes more complicated because this is an existing application with a confusing structure (hierarchy) of maven modules. 任务变得更加复杂,因为这是一个现有的应用程序,具有混乱的Maven模块结构(层次结构)。 I'll try to explain. 我会尽力解释。

So I have an aggregator pom (super pom) that looks like 所以我有一个聚合器pom(super 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>

and a few module poms that do not inherit from super pom (they inherit from spring-boot-starter-parent ) and look like 和一些不继承自super pom的模块poms(它们继承自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>

As you see I define skip.integration.tests and skip.unit.tests properties in the aggregator pom (property values are different for different profiles) and try to use them in module poms. 如您所见,我在聚合器pom中定义了skip.integration.testsskip.unit.tests属性(不同配置文件的属性值不同),并尝试在模块poms中使用它们。

Problem 问题

When I execute mvn clean test -P Test or mvn clean verify -P Test to run tests I see that properties are not applied (eg unit tests are executed although skip.unit.tests is true ). 当我执行mvn clean test -P Testmvn clean verify -P Test来运行测试时,我看到未应用属性(例如,尽管skip.unit.teststrue但执行了单元测试)。

I could declare aggregator pom as parent of module pom and maybe it would solve the problem, but module poms already have spring-boot-starter-parent declared as their parent. 我可以将聚合器pom声明为模块pom的父级,也许可以解决该问题,但是模块poms已经将spring-boot-starter-parent声明为其父级。

Questions 问题

  • Is it possible to use properties from aggregator pom without making it a parent? 是否可以使用聚合器pom的属性而无需使其成为父级?
  • How can I separate integration tests from unit tests by using properties in aggregator pom? 如何通过使用聚合器pom中的属性将集成测试与单元测试分开?

ps. ps。 I assure you that I adhere to naming conventions: unit tests are named like *Test.java and integration tests are named like *IT.java . 我向您保证,我遵守命名约定:单元测试的名称类似于*Test.java而集成测试的名称类似于*IT.java So maven-failsafe-plugin and maven-surefire-plugin distinguish them. 因此,maven-failsafe-plugin和maven-surefire-plugin可以将它们区分开。

If you can make your aggregator as a parent for your modules, and than configure aggregator to have spring-boot-starter-parent as parent this problem will be solved.(assuming your aggregator does not have any parent currently). 如果您可以将聚合器作为模块的父级,并且可以将聚合spring-boot-starter-parent配置为以spring-boot-starter-parent作为父级,则此问题将得到解决。(假设您的聚合器当前没有任何父级)。

Or you can pass skip test property from command line like -Dskip.unit.tests=true (where you call your mvn clean verify). 或者,您可以从命令行传递跳过测试属性,例如-Dskip.unit.tests=true (您在其中调用mvn clean验证)。

As a side note, when you execute mvn test command you don't have to specify integration test to skip, because it is later in the lifecycle.(it won't be executed any way) 附带一提,执行mvn test命令时无需指定要跳过的集成测试,因为它在生命周期的后期(不会以任何方式执行)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM