能否覆盖在父POM中定义的某个profile下已经设置好的插件配置?

129
在我的项目的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应该是可能的。


WLS7配置文件如何激活? - Pascal Thivent
wls7和wls10配置文件在父POM中都是“activeByDefault”。但根据客户需求,只有wls10或两者都会通过脚本(使用“-P”参数)构建。 - Guillaume Cernier
3个回答

163

通过在您的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


看起来对于Maven2.2.1,如果您在配置文件中这样做,它不会与父配置文件中定义的插件合并,而是覆盖它们。如果您直接在构建部分中定义相同的插件,则可以正常工作。对于Maven3,它按预期解决了该问题。 - Greg Domjan
对我来说没有起作用。我想使用Maven 3.3.9重新构建Jenkins NodeJS插件v1.0,并在pom.xml中使用org.jenkins-ci.plugins的1.580.1版本。直到我手动将~/.m2/repository/org/jenkins-ci/jenkins/1.34/jenkins-1.34.pom中的<source>更改为1.7,才能使一切正常运行。 - Alexander Samoylov

7
我遇到了同样的问题。默认情况下,我的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>

1

你尝试过停用wls7配置文件吗(自从maven 2.0.10版本以来):

Starting with Maven 2.0.10, one or more profiles can be deactivated using the command line by prefixing their identifier with either the character '!' or '-' as shown below:

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

This can be used to deactivate profiles marked as activeByDefault or profiles that would otherwise be activated through their activation config.

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


如我上面所说,我无法摆脱父POM,因为在框架的不同层次上,我继承了公司定义的许多配置。而复制配置文件则不是一个好主意,因为我需要报告父POM中的变更,而我经常不知道它们。我只想在项目中测试类的编译过程中覆盖默认配置行为。 - Guillaume Cernier
请重新阅读我的回答,那不是我建议的。我建议停用一个配置文件,而不是摆脱父POM。那么,为什么你要报告父POM中的更改呢?没有任何强制要求你这样做。 - Pascal Thivent
是的 Pascal,谢谢你的帮助,但问题在于如果我停用wls7配置文件,我会失去很多其他插件、Maven通用内容等的配置,而这些我仍然需要。而且我所说的更改是指从父POM到我的POM。因为,根据你提出的解决方案,我需要复制所有父POM(除了测试类编译部分),如果父POM负责更改其POM中的某些内容,我需要被告知任何不属于当前流程的更改,这并不是非常实用。 - Guillaume Cernier
无论如何,非常感谢Pascal尝试帮助我。实际上,我需要这样的行为是因为特定的原因。也许有另一种方法来执行它: - Guillaume Cernier
是的,我猜到你想做什么了。但是,不幸的是,我的最佳答案是:如果wls7配置文件不适合您的需求,请不要使用它(可能有更好的解决方案,但由于我不知道如何激活wls7配置文件,因此无法描述所有选项)。 - Pascal Thivent
显示剩余2条评论

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