Daniel Kaplan
Daniel Kaplan

Reputation: 67440

What is the difference between executions and configurations in a maven plugin?

I found this description but it does not seem comprehensive. Could someone explain in detail what is the difference between executions and configurations in a maven plugin?

Upvotes: 18

Views: 17292

Answers (3)

Stefan Ferstl
Stefan Ferstl

Reputation: 5265

An <execution> causes the plugin to be executed during the maven build lifecycle, i.e. during your build. The <configuration> allows you to configure the plugin for how it should behave during execution. Many Maven plugins provide documentation about their configuration options, e.g. the maven-compiler-plugin.

You can define <configuration>s on the <plugin> level or the <execution> level. The former is globally valid for all executions, the latter is specific to the execution.

Example for global an execution-specific configurations:

Suppose, you have to compile your project with Java 1.7 but you want to early adopt Java 9 Jigsaw features and add a module-info.java to your project. This Java file will not compile using source level 1.7. What you can do is define two executions of the maven-compiler-plugin, one that compiles everything except module-info.java with source level 1.7 and one that does only compile module-info.java with source level 1.9:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <version>3.3</version>

  <!-- Global plugin configuration for source and target levels. -->
  <configuration>
    <source>1.7</source>
    <target>1.7</target>
  </configuration>

  <executions>
    <!-- Compile all code except module-info.java with the configured source level -->
    <execution>
      <id>default-compile</id>
      <phase>compile</phase>
      <goals>
        <goal>compile</goal>
      </goals>
      <configuration>
        <excludes>
          <exclude>module-info.java</exclude>
        </excludes>
      </configuration>
    </execution>

    <!-- Compile module-info.java with source level 1.9 -->
    <execution>
      <id>compile-module-info-java</id>
      <phase>compile</phase>
      <goals>
        <goal>compile</goal>
      </goals>
      <configuration>
        <source>1.9</source>
        <target>1.9</target>
        <includes>
          <include>module-info.java</include>
        </includes>
      </configuration>
    </execution>
  </executions>
</plugin>

Upvotes: 33

Adrian Shum
Adrian Shum

Reputation: 40056

I think answer from @Stefan is already quite clear. I would like to be even a bit more verbose in case it helps.

"execution" under plugin is declaring "what should be done at when". Basically, an execution usually at least contains: phase and goal (I know you don't always see it in config, but conceptually they are there), which you can see it as: When the build process reached phase, then the goal action of the plugin will be executed.

Of course, you can have multiple executions for a plugin, so that different/same goals can be run in different/same phases.

Then come to configuration. Sometimes you need to tell the plugin extra detail on how the plugin should act on because the plugin may not be able to guess what you want to do by default. configuration is doing such work. You can refer to document of plugin's goal to see what kind of configuration they accept.

Plugin level configuration will be applied to all executions of the plugin, while you can also define configuration under each execution, which serve as execution-specific configuration. Plugin-level configuration + execution-level configuration is the "real" configuration received by an execution.

Upvotes: 16

Daniel
Daniel

Reputation: 4221

A <configuration> section outside an <execution> block affects the plugin's behavior in general way. For instance, plugins that are either executed directly through the CLI or have a default phase that they bind to will use this type of configuration. An example of such a plugin would be the compiler plugin.

On the other hand, a <configuration> section inside an <execution> block only applies to that specific execution.

As always, a more specific configuration can override a general configuration. So if a general configuration (outside an execution block) says <doCheck>false</doCheck>, an execution might choose to only override this by doing <doCheck>true</doCheck>.

Another feature of general configurations is that their parameters will be inherited by all executions of that plugin.

Upvotes: 4

Related Questions