【问题标题】:Maven profiles are not inherited in child modules子模块中不继承 Maven 配置文件
【发布时间】:2014-09-27 20:52:28
【问题描述】:

我正在尝试使用 Maven 配置文件来区分不同类型的测试(单元、集成、验收)。这是主 pom 文件的一部分:

        <properties>
            <build.profile.id>dev</build.profile.id>

            <skip.unit.tests>false</skip.unit.tests>
            <skip.integration.tests>true</skip.integration.tests>
            <skip.acceptance.tests>true</skip.acceptance.tests>
        </properties>

        <profiles>
            <profile>
                <id>dev</id>
            </profile>
            <profile>
                <id>integration-test</id>
                <properties>
                    <build.profile.id>integration-test</build.profile.id>
                    <skip.unit.tests>true</skip.unit.tests>
                    <skip.integration.tests>false</skip.integration.tests>
                    <skip.acceptance.tests>true</skip.acceptance.tests>
                </properties>
            </profile>
            <profile>
                <id>acceptance-test</id>
                <properties>
                    <build.profile.id>acceptance-test</build.profile.id>
                    <skip.unit.tests>true</skip.unit.tests>
                    <skip.integration.tests>true</skip.integration.tests>
                    <skip.acceptance.tests>false</skip.acceptance.tests>
                </properties>
            </profile>
        </profiles>
        <build>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.17</version>
                <configuration>
                    <skipTests>${skip.unit.tests}</skipTests>
                    <includes>
                        <include>**/*UnitTests.java</include>
                    </includes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.17</version>
                <executions>
                    <execution>
                        <id>integration-tests</id>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                        <configuration>
                            <skipTests>${skip.integration.tests}</skipTests>
                            <includes>
                                <include>**/*IntegrationTests.java</include>
                            </includes>
                        </configuration>
                    </execution>
                    <execution>
                        <id>acceptance-tests</id>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                        <configuration>
                            <skipTests>${skip.acceptance.tests}</skipTests>
                            <includes>
                                <include>**/*AcceptanceTests.java</include>
                            </includes>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

如您所见,我正在使用配置文件信息来根据所使用的配置文件运行某些类型的测试。默认配置文件是 dev,它只会运行单元测试。它们可以像这样执行:

mvn clean test

对于集成和验收测试,我使用故障安全插件:运行集成测试的示例是:

mvn clean verify -P integration-test

当我从主 pom 模块运行它时它工作正常,但从子模块运行它时它不起作用。测试只是被忽略。查看子模块的有效 pom 我没有看到配置文件。我做错了什么还是这是 Maven 的预期行为?如果无法通过这种方式实现配置文件继承(需要级联到层次结构中最深的模块),如何实现?

更新:这是项目层次结构 项目目录

--main module
--commons module
--administration
----domain 
----data
----business
----web

【问题讨论】:

  • 您的 POM 层次结构是什么?基于此 - stackoverflow.com/questions/3695394/inheriting-maven-profiles - 它应该可以工作。
  • 我的意思是...您的模块将多模块 POM 作为其父级,对吗?
  • 我已经更新了问题。是的,他们有。
  • 从官方消息看来,继承应该可以工作 - maven.apache.org/guides/mini/… 。矛盾的信息就这么多:)。
  • Re “查看子模块的有效 pom 我没有看到配置文件。” 我假设聚合器项目(声明 s 的那个)不是在(子)模块或它们的其中一个模块中声明为 的那个。继承和聚合不一定具有相同的树层次结构,请参阅Project Inheritance vs Project Aggregation

标签: java maven inheritance automated-tests profiles


【解决方案1】:

对于多模块项目,您通常不会直接执行模块。相反,您应该始终执行主模块,但只指定您想要的子模块via -pl parameter。直接运行模块还有很多问题。


刚刚仔细检查了我参与的一些多模块项目,我正在使用&lt;pluginManagement&gt; 将插件配置从父 POM 传播到子项目。

【讨论】:

  • 感谢您的建议。正如你所说,这不是一个真正的问题,因为我可以运行主模块。我只是想看看这是否可能,因为它对我来说非常方便。我将有多个子模块,它们也将有它们的子模块。我想将它们放在单独的存储库中,以便它们可以独立开发。
  • 可以独立开发模块,但是它们所依赖的所有模块,包括父模块,都必须已经部署。顺便说一句,您的子模块是否正确声明了父母?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-21
  • 1970-01-01
  • 2019-09-21
  • 1970-01-01
  • 2020-06-27
  • 1970-01-01
  • 2011-11-26
相关资源
最近更新 更多