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.

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4.  
  5. <groupId>de.viaboxx</groupId>
  6. <artifactId>itext-pdf-writer</artifactId>
  7. <version>1.0-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9.  
  10. <dependencies>
  11. <dependency>
  12. <groupId>com.lowagie</groupId>
  13. <artifactId>itext</artifactId>
  14. <version>2.0.7</version>
  15. </dependency>
  16. </dependencies>
  17.  
  18. <build>
  19. <plugins>
  20. <plugin>
  21. <groupId>org.apache.maven.plugins</groupId>
  22. <artifactId>maven-jar-plugin</artifactId>
  23. <configuration>
  24. <archive>
  25. <manifest>
  26. <mainClass>de.viaboxx.itext.Main</mainClass>
  27. <addClasspath>true</addClasspath>
  28. </manifest>
  29. </archive>
  30. </configuration>
  31. </plugin>
  32. <plugin>
  33. <artifactId>maven-assembly-plugin</artifactId>
  34. <configuration>
  35. <descriptorRefs>
  36. <descriptorRef>jar-with-dependencies</descriptorRef>
  37. </descriptorRefs>
  38. <archive>
  39. <manifest>
  40. <mainClass>de.viaboxx.itext.Main</mainClass>
  41. </manifest>
  42. </archive>
  43. </configuration>
  44. <executions>
  45. <execution>
  46. <id>make-my-jar-with-dependencies</id>
  47. <phase>package</phase>
  48. <goals>
  49. <goal>single</goal>
  50. </goals>
  51. </execution>
  52. </executions>
  53. </plugin>
  54. </plugins>
  55. </build>
  56. </project>

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.

  1. package de.viaboxx.itext;
  2.  
  3. import com.lowagie.text.Document;
  4. import com.lowagie.text.Paragraph;
  5. import com.lowagie.text.pdf.PdfWriter;
  6.  
  7. import java.io.FileOutputStream;
  8.  
  9. public class Main {
  10. public static void main(String[] args) {
  11. try {
  12. Document document = new Document();
  13. PdfWriter.getInstance(document, new FileOutputStream("hello.pdf"));
  14. document.open();
  15. document.add(new Paragraph("Hello"));
  16. document.close();
  17. } catch (Exception e) {
  18. e.printStackTrace();
  19. }
  20. }
  21. }

To package it run:

mvn install

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.

Kommentare sind deaktiviert