Subscribed unsubscribe Subscribe Subscribe

Kengo's blog

Technical articles about original projects, JVM, Static Analysis and JavaScript.

Useful libraries and Maven plugins to implement executable jar

When you implement executable jar, there is some good libraries and Maven plugins to implement. I will list them and explain how to use.

args4j

args4j is a good library to parse command line argument. It is more modern than commons-cli, and its committer is Mr.KAWAGUCHI the author of Jenkins, so its improving speed is good: your patch will be checked and merged (here is my case).

config loader

For me, xml is not so good format because of its redundancy. Yml is also not so good, JSON is better and easy to understand.

To load JSON configuration file, I know 2 major solutions:

typesafehub/config allows you to parse human-readable JSON (hocon) format, so user can reduce redundant configuration and add comment into JSON format. jackson-mapper can map your configuration to POJO, so your code can be simple than typesafehub/config.

You can write simple code to use good point of both. Here is an example.

Maven multi module project

When you develop command line tool, it is better to divide your product into (at least) 2 parts:

  1. user interface layer (which supports command line interface)
  2. others (logic, persistence, etc.)

Interface specific code should be separated from other code to improve portability. If you have plan to implement other interface (daemon, GUI, web application etc.) which shares logic, this separation will help you to reuse your code.

To separate, Maven multi module project can be solution. Make 2 submodule and name them as 'command-line' and 'core' (or 'logic' etc.). Only command-line module depends on args4j and configuration parser, so core module cannot be compile with class in these libraries.

Maven assembly plugin

Maven provides several way to make executable jar. In my opinion, assembly plugin is simplest and easiest solution. It also package dependencies to jar file so you can simply distribute single jar file. See Stackoverflow to know how to configure.

To change the name of jar file, you need to use finalName parameter.

Note that you must check license of your dependencies to know it is re-distributable or not. If your dependencies is not re-distributable, you should stop redistribute it.