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!

new ProcessExecutor().comma...
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...
15/ ...all done via github actions - explore the github.com/jbangdev/jbang… on how it is done...
16/ I probably forgot a bunch of things but there it it is - tips and tricks for writing java based command line apps in 2020.

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Max Rydahl Andersen

Max Rydahl Andersen Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @maxandersen

Sep 10, 2021
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.

If Quarkus was a game, dev mode is its God Mode.
Read 12 tweets
May 19, 2021
So 56% was wrong. All of these uses eclipse JDT in some way to provide their Java support.

You might not like the UX of Eclipse IDE but your other IDE wouldn’t work without it’s continued existence.
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”
Read 6 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(