Advanced Usage

Executing Plugins in Different Maven Phases

Meta-plugins can execute embedded plugins in any Maven phase by specifying the <phase> element in the embedded plugin's execution. If no phase is explicitly identified, the embedded plugin's default phase is used.

For example, to run a plugin during the compile phase:

<plugin>
    <groupId>com.github.ekryd.echo-maven-plugin</groupId>
    <artifactId>echo-maven-plugin</artifactId>
    <version>2.0.0</version>
    <executions>
        <execution>
            <goals>
                <goal>echo</goal>
            </goals>
            <phase>compile</phase>
        </execution>
    </executions>
    <configuration>
        <message>@{greeting}</message>
    </configuration>
</plugin>

The meta-plugin will generate a goal for each phase used in the embedded configuration, allowing you to invoke the meta-plugin in those phases.

Defining and Using Variables

Variable interpolation in meta-plugins follows standard Maven conventions with one additional change:

  • Variables prefixed with ${} are replaced with the value of the variable at the time of mete-plugin compilation.
  • Variables prefixed with @{} are replaced with the value of the variable at the time of meta-plugin execution.

Additionally, execution parameters must be defined <parameters> section and referenced in embedded plugin configurations using the @{parameterName} syntax. This enables dynamic configuration at meta-plugin execution time.

In the following example, the value of <message> is converted to ${gretting} Maven during meta-plugin compilation, and Hello Maven during meta-plugin execution.

<properties>
    <name>Maven</name>
</properties>
...
<parameters>
    <parameter>
        <name>greeting</name>
        <defaultValue>Hello</defaultValue>
    </parameter>
</parameters>
...
<configuration>
    <message>@{greeting} ${name}</message>
</configuration>

Capturing the Maven Execution Environment

You can capture values from the Maven execution environment by referencing Maven properties in your meta-plugin's parameter configurations. For example, to capture the project base directory:

<parameter>
    <name>buildPath</name>
    <alias>build.path</alias>
    <defaultValue>@{project.basedir}</defaultValue>
    <required>true</required>
    <readonly>true</readonly>
</parameter>

During compilation, this parameter is mapped to a mojo parameter named buildPath with /home/runner/work/meta-maven-plugin/meta-maven-plugin/meta-maven-plugin-maven-plugin as a default value. Making the parameter required and readonly hides it from direct user configuration. This allows the meta-plugin to capture and access environment-specific Maven values at runtime.

Running Multiple Executions of the Meta Maven Plugin

Like any other Maven plugin, the meta-plugin can be executed multiple times in a single build. This allows you to generate multiple meta-plugin mojos with different configurations in the same build. Each execution can have its own set of embedded plugins, and independent configuration. When doing so, Maven requires that each execution have a unique <id> element. During code generation, the <id> is used to create a unique mojo name prefix for each execution.

For example, the following configuration generates two meta-plugin mojos named hello-initialize and goodbye-initialize:

<plugin>
    <groupId>io.github.rmichela</groupId>
    <artifactId>meta-maven-plugin-maven-plugin</artifactId>
    <version>1.0.2</version>
    <executions>
        <execution>
            <id>hello</id>
            <goals>
                <goal>meta-meta-plugin</goal>
            </goals>
            <configuration>
                ...
            </configuration>
        </execution>
        <execution>
            <id>goodbye</id>
            <goals>
                <goal>meta-meta-plugin</goal>
            </goals>
            <configuration>
                ...
            </configuration>
        </execution>
    </executions>
</plugin>

Note! While Maven technically allows plugins to be defined multiple times independently within the same POM, the outcome is guaranteed to work. Maven warns you of this. The meta-maven-plugin-maven-plugin is designed only to work with multiple <executions> of the same plugin instance, not multiple instances of the same plugin.

Overriding Embedded Plugin Versions

Consumers of the generated meta-plugin can override the versions of embedded plugins by declaring them as dependencies in the plugin configuration:

<plugin>
    <groupId>io.github.rmichela</groupId>
    <artifactId>using-echo-plugin</artifactId>
    <version>1.0.2</version>
    <dependencies>
        <dependency>
            <groupId>com.github.ekryd.echo-maven-plugin</groupId>
            <artifactId>echo-maven-plugin</artifactId>
            <version>2.1.0</version>
        </dependency>
    </dependencies>
</plugin>

This ensures that the desired version of the embedded plugin is used during execution.