一个Maven配置文件可以继承另一个Maven配置文件的配置吗?

16

我有两个几乎相同的配置文件。不想在每个文件中都重复配置,希望一个配置文件能够从另一个配置文件中"继承",但是我在使用Maven 3时没有找到明显的方法来实现这一点。

在Maven中是否支持配置文件的继承?

2个回答

18

不可以继承配置文件。

但是,你可以拥有多个活动配置文件,通过将两个配置文件中的共同元素拆分成第三个配置文件,并在激活其他两个配置文件时同时激活该配置文件,从而实现大致相同的效果。


1
当您激活其他两个配置文件之一时,可以激活第三个配置文件。您是指手动激活两个配置文件吗?例如,-P profile1,commonProfile?还是有一种方法可以配置一个配置文件来激活另一个配置文件?文档只提到创建额外的属性并在<activation>中使用它们。 - jmmut
请注意,当在多个Maven配置文件中定义了多个不同的活动Spring配置文件,并且这些配置文件一起使用时,可能会出现问题 :( https://dev59.com/363la4cB1Zd3GeqPJDaw - hello_earth

4

严格来说,按照Maven文档的规定,父pom中定义的profile不能在子pom中继承。然而,父pom中指定的属性和构建插件(不包括pluginManagement)可以被激活并修改子pom的构建环境。 例如,如果我有一个名为A的父pom,其中具有以下配置文件部分:

  </profiles>
      <profile>
            <id>integrationTest</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <configuration>
                            <parallel>none</parallel>
                            <excludes>
                                <exclude>**/*Test.java</exclude>
                            </excludes>
                        </configuration>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-failsafe-plugin</artifactId>
                        <configuration>
                            <includes
                                <include>**/*IT.java</include>
                            </includes>
                            <!-- need to exclude nothing as it is possible that some by default some tests are excluded and we need to override that -->
                            <excludes>
                                <exclude>none</exclude>
                            </excludes>
                            <parallel>none</parallel>
                        </configuration>
                    </plugin>            
                </plugins>
            </build>
        </profile>
   </profiles>

假设有一个子pom B,在默认构建中未配置failsafe插件,但通过parent标签继承自A pom。如果我从子pom目录中运行“mvn clean install -P integrationTest”,它将使用在A中指定的插件信息。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接