1/ Tips and tricks for writing java cli apps in 2020, a thread...
2/ There are a lot of little small things to deal with when writing cli apps that looks and behave nicely - the following are some of the things I've used and/or found out while writing jbang...
3/ Parsing command line arguments is on its face a simple task but if you want to do it well you do benefit greatly from a library to do it rather than manually munging the String[] you get handed.
There are a bunch of them but my clear winner is @picocli. It just does it all...
4/ By all I mean that it is annotation based making it declarative and kinda handholds you into a command based class/method structure that is very close to what I would most likely do anyway. It is well documented supports native compilation and has @QuarkusIO extension...
5/ When doing command line apps, especially scripty/automation things you need to run processes and albeit Java has a decent API in ProcessBuilder github.com/zeroturnaround… simplifies the interaction greatly...
6/ Example of zt-exec's simplicity is you in almost a oneline can invoke a process and iterate over its output in a lambda.
Doing that reliably with ProcessBuilder requires way more scaffolding. Thus zt-exec gets a +1 from here!
7/ Modifying, reading and writing files Java 8+ Files.* and Path apis now do fairly well. move, copy, symbolic linking, reading and writing files is in Java 8 and especially Java 11 trivial one liners.
8/ JSON processing - is an area I'm really not yet happy about; I tend to end up using fastjackson. That said I still feel like I haven't found the upfront ease of use and simplicity json/api's deserves in Java...like where is the restassured for non-testing ?...
9/ Testing - lots of ways to test your code; traditional junit tests goes a long way but I've found that doing some black box testing interacting with the cli as a user/script would do finds a ton of issues. Here @KarateDSL has a great approach and what I would recommend...
10/ @KarateDSL for now just provide the basics to execute command lines; but with a bit of setup code github.com/jbangdev/jbang… you can write compact "cucumber" style tests checking for exit codes, std out and err output and more.
11/ Packaging of your apps - a simple wrapper script + a jar is quite sufficient today. @jbangdev has a working windows cmd and linux/osx shell if you want to be inspired - but alternatively you can use something like @QuarkusIO to get a natively built command line binary...
12/ ...or if simply just writing a small script to do some automation using the wast java ecosystem of libraries then @jbangdev at jbang.dev would be perfect. With jbang installed the scripts run directly and it will install the right version of java for the user.
13/ Building - its 2020; any app/library should have CI/CD setup and for cross-platform building and testing out of a github repository GitHub actions wins hands down. Its ease of use and ability to test and run on Linux, OSX and Windows makes it a winner for cli's.
14/ Packaging and Distributions is probably worth a whole thread in itself, but just know that it is easier than you think. For @jbangdev it is fully automated to publish after a release the binary as standalone, windows scoop and chocolatey, osx brew and various linux formats...
Quarkus rocks with native but native are not the only thing that rocks in Quarkus. A thread...
Build time init-first - It might be controversial but being able to gain 5-15% more performance by optimize logging and making Hibernate live reload in sub-seconds all come from this. This repeats for all Quarkusified libraries - it has a massive compounding effect.
Quarkus dev mode - an optimized developer mode made possible because Quarkus participates in the build. Tons of features that make writing in Java fun. And all goes away for production leaving zero overhead.
And before someone says so - yes IntelliJ does not *require* eclipse JDT but have you tried enabling it as intellijs compiler ? Speed is much better; you can get error messages for the whole project and you can run code without having everything compile. It’s liberating :)
Che, gitpod, GitHub workspaces all web based IDES which behind the scene uses the Java language server developed by @rhdevelopers teams which uses @EclipseJavaIDE JDT “headless”