quinta-feira, 17 de setembro de 2020

Using jbang for ETL Scripts

As a Java programmer I prefer to use good old Java to extract/transform/load data to make it usable in tools I work for data visualization. However, it is a burden having to use maven just to write a single Java file. Hopefully there's a new tool called jbang that makes it easier to do scripting in Java.



More than simple scripts

What is impressive about jbang is that you are not restricted to standard Java APIs in fact you can use any library you want coming from Maven! What you will miss is the burden of having to deal with pom.xml and maven builds. Dependencies are simply declared in the Java file itself using a "DEPS" comment, for example:

//DEPS org.slf4j:slf4j-nop:1.7.25


Installation

To install jbang you just have to check the best way for you in downloads page.  In my case I have sdkman, so it was just a matter of running:


$ sdk install jbang


Hello World

The simplest hello world we can thing can run with jbang without any additional configuration, just install jbang and you should be able to run java files. So let's say I have a hello.java just like this


I can run it using: jbang hello.java. With an additional comment at the begging of the file I can make the file work as an executable file. Add the comment as the file first line (linux environments) and make the file executable:

///usr/bin/env jbang "$0" "$@" ; exit $? 

chmod +x hello.java

Then you should be able to run using ./hello.java. This comment can be added to existing files, but if you are starting a new script with jbang just use: $ jbang init hello.java


Real world script with external dependencies

In real world you will need external dependencies. I had a Java file I use to process some CSV files, it is in a maven project and this version can be seen here. To run it with jbang here' what I did:

  • Went to the Java file and added these comments to the beginning:

//usr/bin/env jbang "$0" "$@" ; exit $?

//DEPS org.apache.commons:commons-csv:1.8

//JAVA 11


And that should be it... But for my project I did two additional steps:

  • Got rid of Maven stuff
  • Removed package declaration
My ETL folder is clear now and I can use Java and have contributions from others because they only need to have jbang installed to run my Java file. 

Development is easier, you will not lose content assist or IDE goodies with jbang. For example, I can edit a Java file in vscode by just using: code `jbang edit myfile.java`

Learning more


This is not all, specially with JavaFX, where all the setup is done by jbang! If you want to learn more I recommend you to check jbang examples and  this talk from  jbang father Max Andersen:




segunda-feira, 7 de setembro de 2020

Porting Battleship Game to Android

 The final goal of creating a battleship game was porting it to Android so I can run it on my phone. I was not in the mood to do all configuration and remember how to do it. This changed when GluonHQ introduced start.gluon.io.

In this brief post I will share my steps porting the Battleship Game to JavaFX. 


Android application development


To take advantage of start.gluon.io I created a project and made it part of battleship maven project:



The I modified the original JavaFX app so I could use it in the Android ready app:

  • Decoupled the application setup from a pure desktop app: Instead doing setup on desktop main, I moved it to a separated class, so I could reuse the setup in mobile app;
  • Resize: Since I didn't know the target device size, I had to react to scene resize to redraw the game boards. This was easily accomplished by adding a default method in Screen interface, which is called when Scene is resized. Then screen can modify boards size accordingly.
I made more small changes and adaption, you can see the changes in this commit.

Pros


This is what I found great during battleship mobile app creation:

  • Speed: You can quickly develop, debug and test your application locally, don't have to wait a long time for compiling and things like that;
  • Compatibility: Everything we do in JavaFX seems to work on Android. In battleship we have transitions, use of CSS and Canvas, everything seems to work in my mobile phone. It means that charts, binding, animation and other cool JavaFX APIs can be used for Mobile applications as well;
  • Events: I used MouseEvent to retrieve where user clicks on boards, but in mobile phones we don't have mouse events, I didn't have to adapt mouse events, it just works!
  • Modern: Use modern Java 11;
  • Rich API: I noticed Gluon also provides great APIs to build apps, I didn't use it, but it is great to see that we could make use of some tested and mobile specific components.

Cons


  • ComboBox and ChoiceBox does not seem to work. That's the only thing that really upset me, but I don't think that combox/choicebox are mobile friendly;

Final result


I finished the app in a few hours, I just reused my code and get used to Gluon plugin, hence there's room for improvements. You can download the APK from my drive - you will have to trust me on this one - but you can also build from the sources. See some screenshots:

         


Conclusion

In this post I shared how I ported my JavaFX application to run on Android. That was a great experience, I really glad Gluon is working hard and I truly feel that Java WORA is true again. Thanks Johan, Jose Pereda and Gluon Engineering team for making it possible.


Source code: https://github.com/jesuino/battleship-game