How to specify a default goal for a Maven plugin?
Asked Answered
K

2

11

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?

Karlotta answered 17/7, 2019 at 15:22 Comment(3)
Look at the Flyway page more closely. The pom.xml is simply setting the required config. Further instructions all invoke mojos manually! I don't think I've seen a default execution except when supplied by a parent module (not directly from the plugin)Bugaboo
goal usually have a "preferred" phase it will bind to.Bebebebeerine
I think the below link will help you. maven.40175.n5.nabble.com/…Bebebebeerine
C
5

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).

Collide answered 16/8, 2020 at 15:41 Comment(2)
Which strongly suggests stupidly hard-coded Executions config, inside Maven, for some core plugins, better defined as an optional default execution \@Mojo property flag for plugins with a declared "defaultPhase" \@Mojo property.Peluso
@Peluso Default plugin executions are defined by the packaging type. I think it makes sense to define it like this because you don't even need to declare the plugin to run – it wouldn't be possible to scan all plugins that exist to know which one needs to execute. Maven just does not support a default goal+phase per packaging type when you declare the plugin without those.Stymie
D
1

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.

Dicast answered 18/7, 2019 at 16:53 Comment(5)
I asked about a default goal and your answer talks about a default phase. My Mojo already has a default phase. I want Maven to execute a default goal if users include the plugin in their pom without an <execution> block.Karlotta
I see. Sorry I got that wrong. My understanding is that only the plugins and goals in the life-cycle configuration don't need any more configuration (which comes via the packaging). Just adding a plugin (like the flyway maven plugin) into the build without an execution will do nothing yet. The defaultPhase attribute just saves that config from the execution in the pom. The goals you want to run still need to be specified. Even very common plugins like the failsafe plugin need to do this as they are not part of the default plugin bindings.Dicast
Options at this point may be creating your own packaging and default bindings or by creating an extension: maven.apache.org/guides/mini/guide-using-extensions.htmlDicast
Does it make sense to use custom packaging for Maven modules? Would that even work. What would an extension allow me to do? I thought I need to output a custom artifact type (non-JAR) for that to work but in my case I am generating a Maven plugin so I'm not sure that would work...Karlotta
every module can have its own packaging. I think you should be able to create a .jar file even with your custom packaging. But thats a rare thing. People might be confused. With an extension you can enhance the lifecycle: maven.apache.org/examples/maven-3-lifecycle-extensions.html - so probably enough to simplify plugin config. people would need to add the extension. also a rare thing to do. I think some executions and plugin config is perfectly acceptable for any maven plugin.Dicast

© 2022 - 2024 — McMap. All rights reserved.