5

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?

Grim
  • 1,938
  • 10
  • 56
  • 123
  • The `install` phase contains the `package` phase. You you really need the `install` phase before running your plugin or is `compile` sufficient? If it is, you can use `mvn compile plugin:scaffold package` or define your phase to require the `compile` phase. – dan1st Sep 08 '20 at 10:04
  • 1
    not sure if understand this correctly, but couldn't you simply execute `mvn plugin:scaffold package -U` where the `-U` flag updates dependencies – Lino Sep 14 '20 at 08:30

1 Answers1

5

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

František Hartman
  • 14,436
  • 2
  • 40
  • 60
  • "The first time you run the plugin or when the plugin changes" you are telling a two-step-workflow. But I need to do this "*in one step*"!!! – Grim Sep 14 '20 at 08:25
  • 1
    What do you mean by two step? All you need is `mvn package` (or with clean if you e.g. rename classes). – František Hartman Sep 14 '20 at 08:34
  • 1
    I have changed the wording a bit, hope it's clear now that you should run single `mvn package` command – František Hartman Sep 14 '20 at 08:37
  • I checked-out your project and your projects works as expected. But if you move `` from `/app/pom.xml` to `/pom.xml` you can not compile without having the plugin installed into the local `.m2`-repository. Hope that helps. – Grim Sep 14 '20 at 09:40
  • Yes, it won't work. If you have multiple web-app like modules (which is why I imagine you want to put the build to parent pom, otherwise it doesn't make sense) you could either put whole configuration into `pluginManagement` and refer to the plugin just by name in the web-app pom build part, or you can create a module in-between root and web-app module(s). – František Hartman Sep 14 '20 at 10:21
  • You have the point. A module in-between might do the trick, thanks. Let me come to you back later :) – Grim Sep 14 '20 at 11:05
  • Did not yet checked it. Have trouble to argue that we need to change the structure. – Grim Sep 19 '20 at 10:15