0

I want to chain two Maven plugins which should execute in sequence. The output from the first plugin should be used as input for the second plugin. Let me explain:

  • I want to write a plugin which generates resources and sources, such as configuration files, Java classes, ... Let's call this plugin generator-plugin.
  • This plugin needs input information to generate all this. This information can be retrieved from file system or from a SQL database. Possibly, in the future one might introduce several other input sources. My idea is to write two plugins, one for getting all information from the file system and another from a SQL database.

This gives:

information-plugin-file ---\
                           |--- generator-plugin
information-plugin-sql  ---/

How can this be done with Maven? Can you chain plugins? I am familiar with writing basic Mojo's, but I have no idea how to approach this, hence this question.

One possibility is to output to a standardized file in information-plugin-file/information-plugin-sql and let the subsequent generator-plugin plugin read from the same file (the Unix way of working, everything is a file).

But I am looking for more direct, Maven specific approaches of doing this. Are there such approaches?

With regards to execution order, all plugins will run in the generate-sources phases and will be defined in correct order in the <plugins> section. So that is already covered I think.

De Neiza
  • 3
  • 2
  • I won't create two plugins. I would make a single plugin maybe with different goals to separate source: file or sql and bind it to generate-source/test-sources...... – khmarbaise Oct 09 '18 at 19:32
  • @khmarbaise Could you please elaborate a bit more? In my example, I have 3 plugins. How do you propose to change it exactly? Thank you kindly! – De Neiza Oct 09 '18 at 19:36
  • You know that plugin can have several goals (The class which implements the functionality) ? This different goals can do different things..For example the [maven-war-plugin](http://maven.apache.org/plugins/maven-war-plugin/plugin-info.html) has three goals plus the help goal...Take a look into the source code https://github.com/apache/maven-war-plugin/ or offical source https://gitbox.apache.org/repos/asf?p=maven-war-plugin.git – khmarbaise Oct 10 '18 at 06:05

2 Answers2

1

AFAIK, plugins in maven are designed to be totally independent, so the following methods of sharing the information can be used:

Sharing via maven properties:

Its possible to set a property in the first plugin, and probably it will be accessible from within the second plugin

import org.apache.maven.project.MavenProject;
// now inject it into your mojo of the first plugin 
@Parameter(defaultValue = "${project}")
private MavenProject project;

// Inside the "execute" method:
project.getProperties().setProperty("mySampleProperty", <SOME_VALUE_GOES_HERE>);

Sharing via Files

The first plugin can generate some output file in the 'target' folder And the second plugin can read this file

Write a "wrapping" plugin that executes other plugins (like both first and second plugin). After all mojos are just java code that can be called from the aggregator plugin

You can find Here more information about this method

Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97
  • I agree that's why I started the post with a statement that plugins in maven were designed to be totally independent... – Mark Bramnik Oct 10 '18 at 06:25
-1

I believe the only way you can have something ordered in Maven is through lifecycles. You could have your first plugin (for the input information) run in the generate-sources phase, and the second in process-sources phase.

SiKing
  • 10,003
  • 10
  • 39
  • 90
  • I understand the concept of lifecycles, but that is not really my question. My question is: how can the first plugin provide input to the second plugin? The input of the second plugin depends on the output of the first plugin. – De Neiza Oct 09 '18 at 18:47
  • I misunderstood your question then. – SiKing Oct 09 '18 at 18:52
  • With regards to lifecycles, both plugins will run in the `generate-sources` phase and will be defined in correct order. So execution order is already covered I think. Thank you SiKing! – De Neiza Oct 09 '18 at 18:53
  • Pretty much every lifecycle phase / plugin ends up generating something in the target directory. It would seem appropriate for yours to do the same. – SiKing Oct 09 '18 at 18:56
  • This means that the `generator` plugin will read from the `target` directory, which could work. I am wondering if there is a way to directly chain the plugins, without having to write/read to/from `target`. – De Neiza Oct 09 '18 at 18:58
  • Having the file will make things much easier to debug and test! – SiKing Oct 09 '18 at 19:02
  • I certainly agree with that! – De Neiza Oct 09 '18 at 19:02