How to automate a workflow routine using aliases

TL;DR: Imagine using Grunt/Gulp.JS, but for several different tasks and languages on your computer!

Workflow routines may be fairly straightforward or as complex as humanly possible. Take compiled languages, for example. My workflow with Java always requires me to recompile. Every time. Compile. It can feel like a real drag on productivity. Perhaps, if I'm lucky, my IDE will include a ▶ button to compile and run the project: IntelliJ, Eclipse, and Visual Studio contain such a feature. But what if I wanted to use something else? What if I had a more complex requirement, such as uploading a completed set of reports to a Google drive, or settling transactions and then uploading a screenshot (as a backup) to a drive somewhere, then finally sending an email to shareholders, with fudge factoring to handle the variances in data for reporting? 

For such complex use cases, I’ve found that it makes life a lot easier to define an alias. An alias is a label for a system script, something you can invoke from a command line or by clicking a desktop icon; all modern operating systems support them. You may not want to create an alias for something as simple as the example I’ll show you in this blog post, but the concept of using an alias with a system script such as a .bat or .sh file will help you accomplish difficult complex processes in a similar manner. You simply create a script to execute the commands you need to accomplish your complex workflow, and then create the alias so you can invoke that script whenever you like.

To create an alias in Windows, create a batch file and save it in an easy-to-remember location. In MacOS or Linux, you can use the alias command instead of a batch file. Since I'm using Windows, I've named my batch file "Yavaa.bat": yeah, that's right, Yavaa, like some sort of Pokémon call that one gives when calling out the name of a popular programming language, without the pesky namespace collisions. 

So now, what is in this beloved Yavaa.bat file? A tidy mix of Maven commands that I like to run in a sequence each time. That sequence goes: shutdown server, cleanup, compile, restart. This workflow has given me the cleanest results in terms of making iterative changes and seeing their effect when working with a Java environment and not wanting to context-switch or remember "where is the compile button?". This has just become a second-nature habit of mine to type in 'yavaa' when I'm ready to test the code, and it's instant.

Here are the contents of my Yavaa.bat file:

@echo off
mvn spring-boot:stop clean package spring-boot:start

That may seem complex, so let's digest it all. The first line, @echo off, will turn off repeating the command to the user, so they won't see the script say maven spring-boot… each time I run it. The second line, echo., will print a blank line. The last line of the script is a condensed Maven one-liner to stop, clean, package (recompile), then start the server. I can execute this command inside a command-line terminal for my language IDE, such as IntelliJ. Since it’s Maven, I need to make sure that the directory I'm working in has a Maven POM configuration and its required files, or else I'll run into errors.

Setting (Bash) aliases in Mac/Linux 

To do this same task in Linux, open a new terminal instance and edit the Bash profile using sudo nano .bashrc in the home directory (~/). Scroll to the bottom of the file using your arrow keys on the keyboard and type the following new alias:

alias yavaa='mvn spring-boot:stop clean package spring-boot:start'

It will not delete the other aliases listed when you create a new one at the bottom. Just as with Windows, one line of Maven commands was used to stop the server, delete the old compiled files, compile the new ones, then restart the server. Press Cmd + X to get to the same prompt and press Y to confirm that the .bashrc file is being overwritten with the modified one containing a shiny new yavaa alias you just created. That's all you'll need to do for Linux or Mac. 

Creating an alias on a Mac

Setting environment variables in Windows 

When using Windows, you need to have means by which to fire the script anywhere, and I mean anywhere! First, save the Yavaa.bat script in a descriptive folder location. For mine, I've named the folder "alias" and parked it at the root of the "C:" drive. You can then add your folder to the Windows Path variable. To do so, use Windows search to find "Edit the system environment variables". Clicking the result opens the System Properties control panel. Select "Environment Variables" at the bottom, then edit the Path variable in the SystemVariables list. 

Setting Windows environment variables


Once you’ve set this up, you can use your alias folder to store different batch scripts to automate tasks. Some other examples of automation include combining a lint/cleanup tool for your code and pushing the changes to a branch on git, all within one command, or using different scripts that can call from other scripts to make difficult tasks easier. Here's what I do to kick off a build for my Java projects:

Alias in action for a Java build

Next time you find yourself doing two or more tasks to get the "task" done in a chain (I'm looking at you, exporting a file then uploading it to Youtube), make an alias for it! I've shown you some computational “glue” to patch processes on your Windows, Linux, and Mac devices. Remember to use a dedicated script file to handle the calling and orchestrating of several separate scripts if you cannot (or will not) cram them into a single script. I've shown a simple example that allows me to clean and recompile everything in a single command for Java projects using Maven, but the sky's the limit in terms of what you can imagine connecting together using aliases to automate workflows. 

Additional resources

Aaron Wilde
Aaron Jackson-Wilde
Programmer Writer
Related Topics