Alex Pritchard
Alex Pritchard

Reputation: 4250

Maven plugin multiple executions don't inject variables when executing plugin alone

I wrote a maven plugin that I'm using on some projects. In the first project, it only has one execution and I can execute the plugin directly with

mvn com.mycompany:my-plugin:0.0.1-SNAPSHOT:do-stuff

In the second project, the plugin has multiple executions and when I try the above command, I end up blowing up because the plugin tries to execute with empty parameters.

Note that in both cases, the plugin works fine when executed as part of the process-resources phase. It only fails when I try to execute just the plugin goal. Can someone help me understand why the second example tries to execute with blank parameters?

First project (one execution - works fine):

        <plugin>
            <groupId>com.mycompany</groupId>
            <artifactId>my-plugin</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <configuration>
                <sourceFiles>
                    <sourceFile>loadfile</sourceFile>
                </sourceFiles>
                <outputFile>outputFile</outputFile>
            </configuration>
            <executions>
                <execution>
                    <phase>process-resources</phase>
                    <goals>
                        <goal>do-stuff</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

Second project (multiple executions - does not wok):

<plugin>
        <groupId>com.my-company</groupId>
        <artifactId>my-plugin</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <executions>
            <execution>
                <id>default</id>
                <phase>process-resources</phase>
                <configuration>
                    <sourceFiles>
                        <sourceFile>file1</sourceFile>
                    </sourceFiles>
                    <outputFile>outputFile</outputFile>
                </configuration>
                <goals>
                    <goal>do-stuff</goal>
                </goals>
            </execution>
            <execution>
                <id>novice</id>
                <phase>process-resources</phase>
                <configuration>
                    <sourceFiles>
                        <sourceFile>file1</sourceFile>
                        <sourceFile>file2</sourceFile>
                    </sourceFiles>
                    <outputFile>outputFile</outputFile>
                </configuration>
                <goals>
                    <goal>do-stuff</goal>
                </goals>
            </execution>
        </executions>
    </plugin>

Upvotes: 1

Views: 184

Answers (1)

mikołak
mikołak

Reputation: 9705

In the second example, the only configuration you have is in executions. There's no configuration block outside the execution bindings, so invoking the plugin goal directly naturally receives no params (since the present configuration blocks are specific to the bindings).

A solution would be to have a "default" configuration block (as in your first example) + execution-specific configs, or providing configuration params from the command line, if applicable.

As a final note - and not intending to insult, every programmer misses simple solutions sometimes - since it's your plugin, you can just change the configuration spec to let you define everything you need in one configuration block :). I don't think you should necessarily do this given that the only reason seems to be your specific problem with m2e, but it's certainly an option on the table.

Upvotes: 1

Related Questions