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><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"> <modelVersion>4.0.0</modelVersion> <groupId>de.viaboxx</groupId> <artifactId>itext-pdf-writer</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <dependencies> <dependency> <groupId>com.lowagie</groupId> <artifactId>itext</artifactId> <version>2.0.7</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <mainClass>de.viaboxx.itext.Main</mainClass> <addClasspath>true</addClasspath> </manifest> </archive> </configuration> </plugin> <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass>de.viaboxx.itext.Main</mainClass> </manifest> </archive> </configuration> <executions> <execution> <id>make-my-jar-with-dependencies</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project></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.