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="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
 
    &lt;groupId&gt;de.viaboxx&lt;/groupId&gt;
    &lt;artifactId&gt;itext-pdf-writer&lt;/artifactId&gt;
    &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
    &lt;packaging&gt;jar&lt;/packaging&gt;
 
    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;com.lowagie&lt;/groupId&gt;
            &lt;artifactId&gt;itext&lt;/artifactId&gt;
            &lt;version&gt;2.0.7&lt;/version&gt;
        &lt;/dependency&gt;
    &lt;/dependencies&gt;
 
    &lt;build&gt;
        &lt;plugins&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                &lt;artifactId&gt;maven-jar-plugin&lt;/artifactId&gt;
                &lt;configuration&gt;
                    &lt;archive&gt;
                        &lt;manifest&gt;
                            &lt;mainClass&gt;de.viaboxx.itext.Main&lt;/mainClass&gt;
                            &lt;addClasspath&gt;true&lt;/addClasspath&gt;
                        &lt;/manifest&gt;
                    &lt;/archive&gt;
                &lt;/configuration&gt;
            &lt;/plugin&gt;
            &lt;plugin&gt;
                &lt;artifactId&gt;maven-assembly-plugin&lt;/artifactId&gt;
                &lt;configuration&gt;
                    &lt;descriptorRefs&gt;
                        &lt;descriptorRef&gt;jar-with-dependencies&lt;/descriptorRef&gt;
                    &lt;/descriptorRefs&gt;
                    &lt;archive&gt;
                        &lt;manifest&gt;
                            &lt;mainClass&gt;de.viaboxx.itext.Main&lt;/mainClass&gt;
                        &lt;/manifest&gt;
                    &lt;/archive&gt;
                &lt;/configuration&gt;
                &lt;executions&gt;
                    &lt;execution&gt;
                        &lt;id&gt;make-my-jar-with-dependencies&lt;/id&gt;
                        &lt;phase&gt;package&lt;/phase&gt;
                        &lt;goals&gt;
                            &lt;goal&gt;single&lt;/goal&gt;
                        &lt;/goals&gt;
                    &lt;/execution&gt;
                &lt;/executions&gt;
            &lt;/plugin&gt;
        &lt;/plugins&gt;
    &lt;/build&gt;
&lt;/project&gt;</pre>
[/codesyntax]

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;
 
import java.io.FileOutputStream;
 
public class Main {
    public static void main(String[] args) {
        try {
            Document document = new Document();
            PdfWriter.getInstance(document, new FileOutputStream("hello.pdf"));
            document.open();
            document.add(new Paragraph("Hello"));
            document.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}</pre>
[/codesyntax]

To package it run:

<div>
<div id="highlighter_582527" class="syntaxhighlighter  plain"><code class="plain plain">mvn install</code></div>
</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.

Scroll to Top