JBang and My Own Custom Catalog

Java has been coming back to my life slowly. The developer tools have improved a lot in the last decade and have become friends. Most of my blog readers would know that I use Karate almost daily to test. During the same time, I discovered JBang.

JBang, once installed, allows you to run any java file like a script. Superbly helpful if you want to hack something quickly. It can also install the same as app or binary so it can be found on your PATH. So you can forget about jbang and just run the app.

# Create a Java files that is a template. 
# You can look inside it and edit it if you want.
# It's not very different from regular java file. 
# It has some jbang related setup in the top commented area. 
# You can explore that yourself.

jbang init --template=cli hello.java

# Call it using jbang. 
# First time jbang downloads on the dependencies.
jbang hello.java Thej

# Next time it doesn't
jbang hello.java Raj

Another feature I like is the ability to maintain a catalog, Catalogs can be used to release your "Java App" to your customers using JBang. Here is Karate's Catalog, if you want to look at it. You can run Karate by just running in your shell

jbang karate@intuit/karate

This call does a couple of things.

  1. It goes to `intuit/karate` repo on `github.com` and downloads github.com/intuit/karate/jbang-catalog.json and adds it as an implicit catalog, called "intuit/karate" .
  2. Then from that catalog, it runs the 'karate' script.
  3. Now since `karate` uses apache maven to publish, it downloads `com.intuit.karate:karate-core:1.1.0` and runs that jar
  4. It caches everything so; it doesn't need to download again.
  5. You can also install it locally as an app, so you don't have to call it using jbang. You can do that by calling `jbang karate@intuit/karate`
  6. Now you have `karate` available on your `PATH`

This is a handy and fabulous way to provide tools or apps to end consumers. I like everything here except the dependency on Github (JBang also supports Gitlab and SCM others).

But the best part is JBang allows you to host your catalog anywhere. Add that catalog using the URL. For example, I have my catalog at https://lib.thejeshgn.com/jbang-catalog/thejeshgn-catalog.json 1. So now I can add this catalog using the command.

jbang --verbose catalog add thejeshgn https://lib.thejeshgn.com/jbang-catalog/thejeshgn-catalog.json

And I can call the script inside it using.

jbang --verbose tabula@thejeshgn --help

Which downloads the JAR file for the tabula and runs it.

I can install the tabula by running

jbang app install tabula@thejeshgn

tabula --help

This will have it installed as tabula locally to run from CLI without knowing anything about it.

This way, I can also share my favorite tools with my friends and my future self.

I like how powerful and simple JBang is.

  1. Though it's hosted on Github, it is served through my domain

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.