Power of bash

Published: February 27, 2017

We all tend to do our work faster because time costs money. I realized that we lose a lot of time waiting for IDE to start. In our company, we use Netbeans IDE and all our work is related to it. For Java modules, we use Maven and, of course, we have some custom goals. Some goals are pure Java, though we have some goals that use SVN for committing, logging, reverting and so on. And some of them are related to our RDBMS as we load a DB.

But the main problem is that we can do all of this only when Netbeans IDE finishes its class path scanning and other indexing background processes. This, depending on your hardware, could sometimes take up to 3-5 minutes. I didn’t like it, so I decided to make this process a bit faster.

I defined the following main features we use, which I could improve:

Load database;

SonarQube scanning;

Clean and build;

Pull the source code aka Git but this is for SVN;

Push the source code aka Git but this is for SVN;

SVN reset;

SVN log;

Java Deploy.

Fig. 1 Default Maven actions set up for a project

All these features are related to Maven, therefore, we can write Maven commands. Here is where the BASH comes in! We all know that bash is a very popular and flexible scripting language used by many people around the world. I decided to install and use it instead of the default CMD from Windows. However, it is really hard to remember and write a command like:

I could have used some plugins from GitHub that provide some features, like fast running a command. But, since I am not a Bash person, I decided to write my own bash scripts. This would introduce me to the Bash world and give me a brief view of its commands. As starting point I installed GIT that provides, by default, the Bash terminal and its environment.

Bash aliases

Bash terminal can be configured to load a file at startup. This file is called .bash_profile. You can create it in the %USER_PROFILE% directory. In this file, you can write your own bash scripts and they will be available in the terminal at runtime. I added some aliases that are bound to some functions I could run. Example:

Each alias value is a Bash function that is responsible for doing something. Taking in consideration that all commands are Maven based, I defined three aliases I would need:

M – Run a maven command based on a pom.xml file;

R – Stands for register. I use it to add a directory with a directory alias, list them or remove one;

J – Jump to a directory using a directory alias.

Core and customer structure

Before we go through the implementation, I have to explain why I defined these specific aliases. Internally, we build a product for clients that hold warehouses. More or less, they all have mostly the same functionality, so we have divided our product in 2 parts:

The Core (common functionality);

The Client (customer specific functionality).

Because of this, we have different versions for these components. The Core is based on branches and tags like 1.0-SNAPSHOT or 1.0.1.

Commands

R – Register

Now let’s see what was optimized by using Bash. Using the above mentioned aliases, I can run the commands. As I mentioned ‘r’ stands for register, which holds different aliases for different paths. Using “r” I can run:

However, this does not bring you a lot of magic since we might have tags, branches and environments for a project. Well, jump supports additional parameters to the command, like: j iut 1.0.2 – this way we can jump specifically inside the needed folder. But in this case ‘iut’ alias should be linked to ‘D:\ProjectA’ , because this will be the root folder of the project. We can also jump to a tag or an environment if it exists, by writing the following command: j iut 1.0.2 or j iut d where d stands for development environment.

Conclusion

Having all of this configured, we can run the load the database or any command over a pom.xml file without even having to start the Netbeans IDE. The .bash_profile can be customized per developer, so everyone can add commands of their own. The most important thing is that using Bash we can gain a lot of power due to its internal commands that Windows does not have. We save a lot of time. And while Netbeans starts, you could already have 3-4 terminals working: