Simple and fast prototyping with Maven

Sometimes you are not working in the big enterprise project with thousand lines of code. You just want to play with a new library or a single aspect. Integrating this from the beginning in the main project can sometimes be a slow and complicated task.

We have a special area called Sandbox for this small projects in our repository. After years of IDE usage, some people loose the speed in starting a small project from scratch. Too much tooling can be a problem, but the right tooling can be a great time saver.
Maven is most of the time a useful thing, if used in the right way. Don’t reinvent the wheel and make it too complicated for others to understand your prototype.

I played with the PDF library itext – great one. With just a few lines of code. I even needed to install this library on different computers and operating systems (font types, …). A jar is the purest form for Java projects. But with external dependencies you are dealing with the classpath. To avoid this I packaged the dependencies into an Uber-Jar.

[codesyntax lang="xml"]
<pre>&lt;project xmlns="" xmlns:xsi=""

The assembly plugin is just one of the possibilities. There are several Maven plugins to create Uber-Jars. But the assembly plugin worked great.

My example main class is pretty simple. Just a few lines to demonstrate the use case.

[codesyntax lang="java"]
<pre>package de.viaboxx.itext;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
public class Main {
    public static void main(String[] args) {
        try {
            Document document = new Document();
            PdfWriter.getInstance(document, new FileOutputStream("hello.pdf"));
            document.add(new Paragraph("Hello"));
        } catch (Exception e) {

To package it run:

<div id="highlighter_582527" class="syntaxhighlighter  plain"><code class="plain plain">mvn install</code></div>

You will find a file with the Name *-jar-with-dependencies.jar in the target folder.
To run it, call:

java -jar NAME-jar-with-dependencies.jar

Maven is a little bit verbose in its, but you can copy and paste the functionality most of the time.

Another useful command is the execution of the Main class directly from Maven (if you don’t want to package it):

mvn compile
mvn exec:java -Dexec.mainClass="de.viaboxx.itext.Main"

Download the full project as ZIP.

Nach oben scrollen