Part 15 / Topic 7 of 7
Putting it together: package a CLI tool as a JAR, then as a Maven project
Real-world example: BillSplitter — a restaurant-bill-splitting CLI, splitting a total plus an optional tip equally across diners — built and packaged twice, the same source, two different ways:
- By hand (
examples/jar-packaging/):javactoout/,jar --create --main-class ... -C out ., run withjava -jar. Nopom.xml, no dependency management — exactly section 2's technique, with a slightly more real CLI tool than the one-liner used to introduce it. - With Maven (
examples/maven-project/): the same logic, moved intosrc/main/java, with the pure calculation split into a separately-testableBillCalculator(onerecord Split(...), one static method) and a realBillCalculatorTestundersrc/test/java— thenmvn package(section 4) produces a jar with an identicalMain-Classmanifest entry, runnable the same way.
# the hand-built jar (examples/jar-packaging/)
$ java -jar billsplit.jar 2400 4 10
Bill: Rs 2400.00
Tip (10.0%): Rs 240.00
Grand total: Rs 2640.00
Split 4 ways: Rs 660.00 per person
# the Maven-built jar (examples/maven-project/), same logic, same CLI, same output
$ java -jar target/bill-splitter-1.0.0.jar 2400 4 10
Bill: Rs 2400.00
Tip (10.0%): Rs 240.00
Grand total: Rs 2640.00
Split 4 ways: Rs 660.00 per person
Nothing about the program's behavior changes between the two — a java -jar call can't tell which build tool, if any, produced the jar it's running. What changes is everything around the program: Maven adds a repeatable build command, a place for tests to live and run automatically before packaging, and a standard layout any other Maven user already recognizes — exactly the gap section 1 opened with.
Done here? The practice set for Part 15 — including the solutions — is on the part page.