6

In a Maven <plugin> element there is an <executions> element which contains multiple <execution> elements. Each <execution> element can have an <id> element containing a string. What references those <id>...</id> elements? What does it mean to omit that element? What are the semantics of the <id> element?

For example:

<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <executions>
          <execution>
            <id>default-jar-execution</id>
            <configuration>
              <finalName>mainjar</finalName>
            </configuration>
          </execution>
          <execution>
            <id>extra-jar-execution</id>
            <goals>
              <goal>jar</goal>
            </goals>
            <configuration>
              <finalName>anotherjar</finalName>
            </configuration>
          </execution>
        </exectutions>
      </plugin>
      [...]
    </plugins>
  </build>
</project>

What references those <id>default-jar-execution</id> and <id>extra-jar-execution</id> values? What is the behavioral difference of changing either of those strings? What does it mean to remove those elements?

jameshfisher
  • 34,029
  • 31
  • 121
  • 167

2 Answers2

5

The id element has two functions:

  1. Documentation
  2. Allow Maven to know when you want to create a new execution and when you want to modify an existing one.

The first case is simple: It just allows you to give the execution a meaningful name.

The second case means that Maven comes with default executions which you can see when you run mvn help:effective-pom, for example. If you want to replace/extend an existing execution, you need to use the same id. Maven will then merge the two.

See http://maven.apache.org/guides/mini/guide-configuring-plugins.html#Using_the_executions_Tag

Executions of the same id from different POMs are merged.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • Thanks. What does it mean to merge multiple executions? – jameshfisher Oct 28 '14 at 13:43
  • If you use the same ID, then Maven will load the existing execution and add your XML to it. Use `help:effective-pom` to see the result. If there are too many executions or they have wrong parameters, then you need to change the ID and add more configuration. – Aaron Digulla Oct 28 '14 at 14:00
  • I don't know but it is pretty straight forward: Load the first XML and then add the second one. Same leaf elements in the second one will overwrite the leafs in the first one (leaf elements are those which contains text, i.e. config option values). – Aaron Digulla Oct 28 '14 at 15:40
1

For sake of completeness, I would note, that since Maven 3.3.1 (March 2015), there is also an option to use the execution-id, when calling maven specific goal from the command line.

See https://maven.apache.org/docs/3.3.1/release-notes.html

See discussion about this option here

Eliyahu Machluf
  • 1,251
  • 8
  • 17