Performing simple tasks with Maven and Java

Sometimes I need a small tool to perform a simple task for me. One recent example is a simple test client that sends a SOAP request that in an integrated environment would be sent by an external party. The client needs to send an ID and the outcome (accepted/rejected) of a request that was sent in a previous stage.

Now, there are several ways to achieve this. You could write a full web-based GUI, or, on the other end of the spectrum, use a tool like Boomerang. But if you’re already using Maven, you should also consider using the Exec Maven plugin.

The Exec plugin is an easy way to execute some code; it is lot faster to set up than a standalone application, but you are still able to access normal Maven dependencies.

Setting it up

You can use this in an existing project or create a new project. Since my client is basically test code, I created a new project that depends on the project that contains the application code.

Add an executable class to the project that performs the action you need. You can use the standard arguments array if you need paramaters. My class takes three parameters: a customer ID, the outcome, and the endpoint URL. It then performs a SOAP call and prints the results to the console.

Add the Exec plugin to your pom; if you want to use arguments, you must specify them in the configuration of the plugin. With the classpath tag you automatically add all project dependencies to the classpath.

Running your code

Run the code by calling the exec:exec goal and providing the parameters you defined in the pom. You must provide all parameters that you defined!

If one of the parameters is (almost) always the same for the user, you could define the value as a property in the user’s Maven settings file (user_home/.m2/settings.xml) so you don’t have to provide it every time you execute the code.

You can still override the property by providing it to the Exec goal as before.

Tweet about this on TwitterShare on LinkedIn


Het e-mailadres wordt niet gepubliceerd.