Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 591
Daniel Kaplan Avatar asked Nov 25 '15 04:11

Daniel Kaplan


2 Answers

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.

like image 70
Adrian Shum Avatar answered Oct 19 '22 03:10

Adrian Shum


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>
like image 34
Stefan Ferstl Avatar answered Oct 19 '22 03:10

Stefan Ferstl