Maven2 - problem with pluginManagement and parent-child relationship

后端 未结 4 1553
不思量自难忘°
不思量自难忘° 2020-12-14 09:56

from maven documentation

pluginManagement: is an element that is seen along side plugins. Plugin Management contains plugin elements in much the same

相关标签:
4条回答
  • 2020-12-14 10:13

    Adding the plugin configuration to pluginManagement means that this configuration will be used if the plugin is declared, but you still need to declare the plugin in the build section of any POM that wants to use it.

    The key part that explains this from the section you quoted is:

    However, this only configures plugins that are actually referenced within the plugins element in the children

    So if you do this in the child pom the configuration from the parent will be applied:

    <build>
      <plugins>
        <plugin>
          <artifactId>maven-dependency-plugin</artifactId>
        </plugin>
      </plugins>
    </build>
    

    Update: To answer the actual question, the content from the pluginManagement section is always merged with any plugin declaration. To avoid the parent doing this, you can define the pluginManagement section within a profile, and activate that profile on child projects but not the parent. The child projects would then have to declare that profile.

    For example:

    <profiles>
      <profile>
        <id>for-children</id>
        <build>
          <pluginManagement>
            <plugins>
              <plugin>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.0</version>
                <executions>
                  <!--Some stuff for the children-->
                </executions>
              </plugin>
            </plugins>
          </pluginManagement>
        </build>  
      </profile>
    </profiles>
    <build>
      <plugins>
        <plugin>
          <artifactId>maven-dependency-plugin</artifactId>
            <version>2.0</version>
            <inherited>false</inherited> <!-- this perticular config is NOT for kids... for parent only -->
              <!--some stuff for adults only-->
            </executions>
         </plugin>
      </plugins>
    </build>
    
    0 讨论(0)
  • 2020-12-14 10:21

    In parent pom you should configure executions with <goals> declared but don't declare <phase>. Then in the child pom you'll declare:

    <plugin>
      <artifactId>some-plugin</artifactId>
      <executions>
        <execution>
          <id>execution-id</id>
          <phase>partcular-phase</phase>
        </execution>
      </executions>
    </plugin>
    

    The plugin won't be executed until you define a phase in the child pom. Thus you'll need to explicitly bind executions to phases in every child pom (or in the middle of hierarchy), but you won't need to copy-paste configuration of these executions.

    Note, that lots of plugins have Default Phase, e.g. Enforcer Plugin is bound to validate by default. Even if you don't bind plugin to a phase explicitly, it will be bound anyway and thus the plugin will be executed. To overcome this, use non-existing phase in your parent:

    <execution>
      <id>execution-id</id>
      <goals><goal>some-goal</goal></goals>
      <phase>none</phase>
    </execution>
    
    0 讨论(0)
  • 2020-12-14 10:27

    I always used to think that a child POM can inherit a plugin definition from its parent's pluginManagement section and specify only the executions it wants to run from that plugin by referencing them by ID and binding the execution to a phase. As long as the parent definition is in pluginManagement (and not directly in plugins) and is not bound to a phase, only the specific execution (with ID) will be run in that phase.

    From reading the above, and from my own current problem, it looks like that's not true: it looks like a child POM will inherit the entire configuration of the plugin, including all executions. In terms of executions, the only thing the child can do is to override specific values - it cannot pick which executions to run, and which not to.

    Is this a bug? What's the use of being able to bind each execution to a phase (or not), if all executions will be run? I've only seen it with maven-dependency-plugin:unpack (bound to package phase), but with other plugins I might just have been lucky...

    Damn.

    0 讨论(0)
  • 2020-12-14 10:33

    You must assign an ID to the execution so maven knows which ones overwrite each other and which are independent.

    0 讨论(0)
提交回复
热议问题