Build a Custom Maven Plugin

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone i'm brian Demers and today i'm gonna show you how to build a maven plugin I'm gonna use IntelliJ but you could use your favorite IDE I also have a blog post for this example if you'd like to read that instead and cut and paste the code and there's also a github link in that post you can check that out as well so first up I'm going to create a new IntelliJ project and just a basic maven project and I'm gonna use Java 1:8 and I'm gonna call the group ID come to octa example and the artifact ID is important this structure is going to be some identifier I'm gonna say example - maven plug-in and it's the - maven plug-in at the end that's the important part I'm going to clean up this directory name a little bit to keep it the same as the artifact ID and I built this a few times so I'm gonna give it a new directory this is your pretty standard maven project creation screen nothing new yet so here we go so we have a basic empty maven project and I'm going to enable auto imports because I'm going to change some of the palm information and I'm gonna add some other standard attributes so like any good developer I always give a name or a description in my case this is the example maven plug-in and a even simpler description I'm also going to define some common properties so I'm telling the source and the target of the compiler to be Java 1/8 and I'm setting the encoding to utf-8 so next up I'm gonna define a dependency block and I'm going to define my dependencies in my case I'm using the maven plug-in API because I'm building a plugin maven core is also pretty standard and then I'm also defining the plugin annotations these all have a scope of provided these dependencies that provided to you by the maven runtime that you're using to run the project alright the last thing I need to do is add some plugins since I've already defined the this project as a maven plug-in I just need to tweak a few of the versions of the plugins that I'm using so the maven plugin plugin is the maven plug-in that builds plug-ins how about that for a mouthful so I'm gonna bump the version to 3.6 and I'm also adding the site plug in here what while I'm here so that's it so just a few simple dependencies a few simple plug-in tweaks and again I'm using plug-in management here because the plugins in the goals that are used are already defined based on the project type hoops which I forgot which is packaging maven plug-in that is important so next up I'm going to create a new package I'm gonna call it com2 octa example a class so I'm going to call my and they get version mojo so a mojo is a maven term a maven plain old Java object just the same as your POJO so I'm gonna extend an abstract mojo and it's going to make me implement one method which is execute so that's where all the logic for my plug-in is going to go so I could do something incredibly basic right now get log info cool a maven plug-in alright so this is almost done but I need to add an annotation to this class so maven knows it's a mojo so I'm gonna call mine version so the Mojo name is the name of a goal a plug-in typically consists of one or more goals and each goal runs at some phase during the life cycle in my case I'm just executing command the command line so I want to do this early on in the Maven life cycle so I'm gonna say it's initialized and we also need to provide Javadoc and obviously that's important everywhere but in our case it'll get used for the documentation later on so I'm gonna cheat and and spit out some pre formatted talk so that's it so I could run this right now and it'll spit out this bit of text so let's do that so first I need to to build the plug-in so maven install great and then I'm going to execute the plugin so plugins are executed with the standard format of group ID which in our case is com2 ought to that example an artifact ID which is example maven plug-in and then the gold name which in our case is version so there we go cool I may even plug-in that's not much more useful than a hello world example so let's let's add something useful to our plugin so back in our mojo class I'm going to define some parameters so first up I'm gonna say I have a string called command oops string command and we're gonna find this as a parameter I'll give it a proper name of get command and then the default value we're gonna say is get rev parse short yeah not overly complicated but what this is doing is this is saying this command is a parameter you can either pass it in through the command line with a dash D get duck command notation or you could use it in an XML file just by using command and the default value if you don't specify one is going to be this this get Rev parse one more thing I'm also going to inject the maven project itself so private maven projects maven projects OOP let's just call this project I'm also going to annotate this filled with parameter and its property is project and it is read-only so we're also missing some Javadoc so let's add that as well this is the command used to get current prevision easy enough so now that we have a command let's do something with it so I have something ready to go so I have this method calls get version which takes a command and just executes that with a Java Runtime so just execute it captures the output and returns it as a string so from our plugin you can do string version equals I get version command and then we can use the Maven project we're gonna get all the properties and then we're gonna add a new one so we're going to put example version and we're gonna set the version rate and I'm also going to print something useful to the log which is get hash version all right so that's our quick maven plug-in so I'm going to build this again and then run it all right so let's run that the same way we did before well this project isn't a maven repo so I'm going to get get in it thank you add palm and the source directory git commit all right not super excited enough I do that again the art here is our get hash excellent now if I want to change the command in the command line I could do that with dash D get command equals get rev parse head and then it'll be in the longer hash no longer the short hash look at that cool so we have a plug in everything is crammed into this single class which is fine for four basic examples but often you want to break up your code into modular chunks to make it easily testable I'm going to show you how to do that right now we're just gonna pull out this get version method into it into a new class and then inject it into our plugin so we go back to our example package I'm gonna create a new class a new interface sorry I'm gonna call this version provider and then I'm just gonna lift the method signature from this class here the mojo itself keep things nice and simple and then I'm going to create implementation of this interface but I call it run time XE version provider and of course this implements our version provider and I'm going to lift this get method right from the Mojo itself pasted over here so the only thing left to do is annotate our class so using the standard at injector jsr 330 annotations I'm just going to do at named and then at singleton so if you are a spring fan this is the equivalent of using the component annotation so back in our mojo I need to just inject the version provider using the standard at inject and then version provider calls version provider and then fix our new compiler error here and that's it so once again I'm just gonna build the plug-in and execute it the same way I did before sorry maven install there we go and then I'm going to execute it once again there we go so executed the same our code is just broken up into chunks all I had to do is add some standard annotations to my components and now my execute method is literally just three lines very nice alright so typically maybe plugins are defined in your palm dot XML files so let's create an example that uses our new plugin so we'll cheat a little and we'll define a new palm right within our project oops new file put in a directory called usage example palm dot XML and I have one ready I'm gonna call this example palm this is just so you don't have to watch me type all of this and it's just the simple example palm with a definition of our maven plugin the example maven plug-in and I'm setting the command to a very short hash less less useful but it still illustrates the point and I'm also using this echo maven plug-in to print out the new example version property of defined this will simply print out the project version is version which in our case is 1 0 - snapshot - this very useless short hash so if we run this on the command line we have mvn package and we say it's - F usage example palm and there we go so we have the hash as reported in our plugin right here and then the echo plug-in also uses our new property to print this line great well that's cool so now we have a plugin we've used the plugin both in the command line directly and in another pom file so the only thing left to do is to show you how to document your project so if we go back to the projects that plug-in pom file we're gonna add a few things here so all you really need is a reporting section so if you use once again the maven plug-in plug in and define the report as report that is all you need but because we're good developers I'm also gonna add some more metadata to my palm so let's do to find the organization so give it the great name of example Inc and we'll use this Google search URL and then I'm also going to use some prereqs which in our case I'm going to say the lowest version you should use with this plug-in is maven 3 to 5 so now if I run the site build which is just mvn site this will take a couple seconds because it's building the project and generating a bunch of reports there's some there's some ways to to speed this up but that's outside the scope of this video so it should be almost done here and then I'll open the page in my favorite browser so just open target site plugin info which is the the generated HTML file and here we go so we have our name we have the version we have the description from our Java doc we have our maven version here which is 3.5 which I just set the example version goal and that has the information pulled from the annotation and the Java doc so as you can see the default value is get read parse short head and then the user property like we used before was get document so this is one of my favorite things about maven plugins all of the documentation generally looks the same it's really easy to navigate because we do to the consistency that's it I've showed you how to build a maven plugin how to break it up into components and how to generate the documentation be sure to check out the description below for the link to the original blog post that has all the code and everything you need to copy and paste this example if you like this video click that subscribe button we have new videos coming out weekly and check out our blog for even more content thank you
Info
Channel: OktaDev
Views: 5,809
Rating: undefined out of 5
Keywords: java, maven, tutorial
Id: wHX4j0z-sUU
Channel Id: undefined
Length: 17min 24sec (1044 seconds)
Published: Mon Sep 23 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.