mavenmaven-pluginmojo

How can you chain/stitch Maven plugins?


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:

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.


Solution

  • 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