Monday, March 10, 2014

Maven Tutorial: how to create a maven plugin that rename the packaged jar files

It turns out it is super easy to create one plugin by using the maven archetype in eclipse , here is quick tutorial to create one plugin that we can add to any project, the plugin will rename the packaged jars to our desired format. I will put a data in the jar names as an example.

for the demo, I will create one parent Project named Container, and two modules under this container, one is named MyPlugin, another module will be Client which reference the Plugin.
so one project which is the parent, pom as the target, and two modules.

image

for the Client, it’s just a regular simple Maven module.
image

for the MyPlugin project, we want it to use the archetype maven-arechetype-mojo as the base type.
image

once done, we can right click the pom.xml in client library, and plugin dependency, you can see our new plugin is there.
image

Click ok to add the MyPlugin dependency.
here is the pom.xml for Client project
image

Now, the plugin is ready, we can go the client folder, and run our plugin,
before we run this, run a “mvn install” under Container folder to install the plugins to local repository.

on the Client folder, run “mvn MyPlugin:touch”
you can see the touch.txt is there, which is created by our plugin.
image

go back to demo for the file renaming, we want the plugin to rename the jar file from Client-0.0.1-SNAPSHOT.jar to Client-0.0.1-SNAPSHOT-20140310.jar

so let’s rewrite our plugin execution logic,

image

run this target again, we can see the file is renamed to our format

image

if we want add this plugin to our package target, we can change the execution for this plugin to package.

image

then once we run the package , this plugin will be invoked automatically.

image

No comments:

 
Locations of visitors to this page