Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven plugin development: is there a way to automatically run plugin when it's just declared in pom.xml?

I'm developing a new mojo. It has only one goal so it seems logical not to force a user to add an executions section (if they don't want to change a default phase).

This should be possible because when I add a very simple description of a surefire plugin, maven understands that its single goal test should be run, i.e.:

 <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.4.3</version>
 </plugin>

and this is enough to run the plugin.

How can I achieve similarly small configuration for my plugin?

Here is what I have now (and it doesn't work without executions section):

/**
 *
 * @goal test
 * @phase test
 */
public class MyMojoPlugin extends AbstractMojo {

   ... (implementation details)    
}

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>org.somegroup</groupId>
    <artifactId>mymojo-maven-plugin</artifactId>
    <packaging>maven-plugin</packaging>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-plugin-api</artifactId>
            <version>2.0</version>
        </dependency>

        .... (other dependencies)

    </dependencies>

    <build>
        <plugins>
            ... (some plugins)
        </plugins>
    </build>

</project>
like image 503
Roman Avatar asked Nov 05 '22 14:11

Roman


1 Answers

The "default" lifecycle bindings are defined within Maven. This is described in the Build Lifecycle document, which document also indicates that the "executions" section is required for plugins.

I suppose that you could edit components.xml and recompile Maven for your particular site.

In my opinion a better alternative, for in-house development at least, is to use a shared Parent POM that contains site-specific configuration.

Edit: I was looking for a reference to Parent POMs and saw this. I'm not sure if that's a Maven3-only feature, but it's probably worth investigating.

like image 188
parsifal Avatar answered Nov 09 '22 12:11

parsifal