是否可以覆盖已为父POM中的配置文件定义的插件的配置?

问题描述 投票:93回答:3

在我的项目的POM父文件中,我有一个这样的配置文件定义了一些对这个项目有用的配置(这样我就无法摆脱这个父POM):

<profile>
<id>wls7</id>
...
<build>
  <plugins>
    <!-- use java 1.4 -->
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <configuration>
        <fork>true</fork>
        <source>1.4</source>
        <target>1.4</target>
        <meminitial>128m</meminitial>
        <maxmem>1024m</maxmem>
        <executable>%${jdk14.executable}</executable>
      </configuration>
    </plugin>
  </plugins>
</build>

...
</profile>

但在我的项目中,我只想覆盖maven-compiler-plugin的配置,以便使用jdk5而不是jdk4来编译测试类。

这就是我在项目的POM中完成此部分的原因:

<profiles>
  <profile>
    <id>wls7</id>
        <activation>
            <property>
                <name>jdk</name>
                <value>4</value>
            </property>
        </activation>
    <build>
      <directory>target-1.4</directory>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <executions>
            <execution>
              <id>my-testCompile</id>
              <phase>test-compile</phase>
              <goals>
                <goal>testCompile</goal>
              </goals>
              <configuration>
                <fork>true</fork>
                <executable>${jdk15.executable}</executable>
                <compilerVersion>1.5</compilerVersion>
                <source>1.5</source>
                <target>1.5</target>
                <verbose>true</verbose>
              </configuration>
            </execution>
          </executions>
        </plugin>
      </plugins>
    </build>
  </profile>
              ...
</profiles>

它不起作用......

我甚至试图在我的POM的常规插件部分中覆盖配置(我的意思是,不是针对特定的配置文件,而是针对我的整个POM)。

可能是什么问题呢 ?

澄清我的一些要求:

  • 我不想摆脱父POM和其中定义的配置文件(wls7)(因为我需要很多很多属性,配置......),这不是我公司的流程。
  • 基于复制父POM和/或其中定义的配置文件的解决方案并不是一个好的解决方案。既然负责了 父POM改变了一些东西,我 我必须在我的报道。

它只是一个继承问题(扩展或覆盖一个配置文件,一个来自上层POM的配置)所以我认为应该可以使用Maven 2。

maven-2 plugins configuration profile
3个回答
130
投票

从父pom中覆盖配置可以通过将combine.self="override"属性添加到pom中的元素来完成。

尝试将插件配置更改为:

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <executions>
        <execution>
          <id>my-testCompile</id>
          <phase>test-compile</phase>
          <goals>
            <goal>testCompile</goal>
          </goals>
          <configuration combine.self="override">
            <fork>true</fork>
            <executable>${jdk15.executable}</executable>
            <compilerVersion>1.5</compilerVersion>
            <source>1.5</source>
            <target>1.5</target>
            <verbose>true</verbose>
          </configuration>
        </execution>
      </executions>
    </plugin>

有关覆盖插件的更多信息,请参阅:http://maven.apache.org/pom.html


5
投票

我遇到过同样的问题。默认情况下,我的maven war插件排除了html文件。但在我的验收测试配置文件中,我希望包含此文件。因此,当我再次添加maven war插件时,它没有覆盖默认值。

为了解决这个问题,我传递了combine.self属性并且工作正常。

默认构建:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <packagingExcludes>swagger-ui/client.html</packagingExcludes>
    </configuration>
</plugin>

验收测试简介:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.4</version>
    <configuration combine.self="override"/>
</plugin>

2
投票

您是否尝试停用wls7配置文件(自maven 2.0.10起):

从Maven 2.0.10开始,可以使用命令行停用一个或多个配置文件,方法是在其标识符前加上字符“!”。或' - '如下所示:

mvn groupId:artifactId:goal -P !profile-1,!profile-2

这可用于停用标记为activeByDefault的配置文件或通过其激活配置激活的配置文件。

然后在具有不同名称的配置文件中或直接在pom.xml中添加配置。

© www.soinside.com 2019 - 2024. All rights reserved.