Gili
Gili

Reputation: 90150

How to specify a default goal for a Maven plugin?

I've defined a Maven plugin with multiple goals. Currently users run my plugin as follows:

<plugin>
    <groupId>myGroupId</groupId>
    <artifactId>myArtifactId</artifactId>
    <version>someVersion</version>
    <executions>
        <execution>
            <goals>
                <goal>myGoal</goal>
            </goals>
        </execution>
    </executions>
</plugin>

but I've seen other plugins, like maven-compiler-plugin and Flyway, that don't require specifying an execution: https://flywaydb.org/getstarted/java

<plugin>
    <groupId>org.flywaydb</groupId>
    <artifactId>flyway-maven-plugin</artifactId>
    <version>5.2.4</version>
    <configuration>
        <url>jdbc:h2:file:./target/foobar</url>
        <user>sa</user>
        <locations>
            <location>classpath:db/migration</location>
        </locations>
    </configuration>
</plugin>

How do I specify the goal that should run by default when users exclude the <executions> block?

Upvotes: 12

Views: 5064

Answers (2)

J Fabian Meier
J Fabian Meier

Reputation: 35843

AFAIK, there are not default goals for Maven plugins.

You can configure a plugin without adding a goal. But this does not execute the plugin.

The plugin must be either executed explicitly on command line (like flyway:migrate) or is executed automatically through the lifecycle (like compile:compile or jar:jar).

Upvotes: 5

wemu
wemu

Reputation: 8160

I assume you are using the Java5 Annotations to mark your plugin as available mojo? (and not the javadoc way of living).

The @Mojo annotation has a defaultPhase attribute.

Once a user adds the plugin into the build these defaults (if set) will be used.

The Flyway Migrate Mojo does it this way too.

The compiler plugin is a bit of a bad example, as it is part of the default plugin bindings of the maven life-cycle itself. So the phase itself will know what mojo to run.

These are the docs for the maven plugin api, the one for using annotations is nearby.

If it is not your plugin, you can put the configs you want into a parent pom into the pluginManagement section.

Upvotes: 1

Related Questions