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?

2
  • 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. Commented Sep 8, 2020 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 Commented Sep 14, 2020 at 8:30

1 Answer 1

5
+250

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

Sign up to request clarification or add additional context in comments.

7 Comments

"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"!!!
What do you mean by two step? All you need is mvn package (or with clean if you e.g. rename classes).
I have changed the wording a bit, hope it's clear now that you should run single mvn package command
I checked-out your project and your projects works as expected. But if you move <build> from /app/pom.xml to /pom.xml you can not compile without having the plugin installed into the local .m2-repository. Hope that helps.
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).
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.