119

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

4

3 回答 3

152

可以通过将combine.self="override"属性添加到 pom 中的元素来覆盖父 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

于 2011-11-14T09:47:40.620 回答
6

i had the same issue. By default my maven war plugin excluded a html file. But in my acceptance-tests profile i wanted this file included. So when i added in the maven war plugin again it did not override the default.

To resolve this issue i passed in the combine.self attribute and worked fine.

Default build:

<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>

Acceptance test profile:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.4</version>
    <configuration combine.self="override"/>
</plugin>
于 2013-09-17T06:03:59.353 回答
1

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

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

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

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

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

于 2009-11-20T18:25:05.357 回答