Custom maven plugin: install and execute the plugin in one build

Viewed 717

I wrote a custom maven plugin that scaffolds java-code from a custom schema.

The project-structure is like this:

Project
+ plugin
+ web-application

The reactor compiles first the plugin, then the application.

The usual mvn-command is:

mvn

... who is triggering the <defaultGoal>plugin:scaffold package</defaultGoal>

On fresh machines the build fails because the plugin is not yet known at the time the reactor plan the build-phases. So I have to call mvn install first. Then mvn plugin:scaffold package works like a charm.

The problem is: Whenever I modify the scaffolder-plugin and call mvn plugin:scaffold package the modifications of the scaffolder-plugin is not yet used because it is not yet installed into the repository. So I have to call mvn install first again.

Is there a way to:

  1. Install the modification to the plugin
  2. Build the webapplication using the modifications of the plugin

in one step?

1 Answers

First your plugin must be a module of the root project for the resolution to work correctly:

<modules>
    <module>plugin</module>
    <module>app</module>
</modules>

Then declare the plugin in the build/plugins section your application pom

<build>
    <plugins>
        <plugin>
            <groupId>org.example.plugin</groupId>
            <artifactId>plugin</artifactId>
            <version>${project.parent.version}</version>
            <executions>
                <execution>
                    <id>sayhi</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>sayhi</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

The first time you run the plugin or when the plugin changes you need to run at least the package phase so the plugin jar is created. It must be run from the root project:

mvn package

The plugin will be executed during the generate-sources phase:

[INFO] --- plugin:1.0-SNAPSHOT:sayhi (sayhi) @ app ---
[INFO] Hello, world.
[INFO] 

When you change the plugin just run (again from root project):

mvn package

and you will see the changes:

[INFO] --- plugin:1.0-SNAPSHOT:sayhi (sayhi) @ app ---
[INFO] Hello, worldxxxx.
[INFO] 

See a full example on Github

Related