An Intro to Software Build Tools
Tags: java, android, webdev, tutorial
Published: 2022-03-15 08:50:05


## What are Build Tools 🏗

Build tools are programs that automate the process of building production-ready software from source code. 

This process includes 👇🏽
- Downloading third party dependencies. 
- Compiling code with the right configuration.
- Running tests and reporting their results.
- Packaging the compiled code into executable form.

___

## Why use them 🧐

To understand why we might want to use such tools, let's look on how to build a production ready JAR file from Java source code without using a build tool.

Let's address each task of the ones stated above.

####  Downloading third party dependencies 🗂
In the Java world, dependencies are packaged into Jar files which we have to find and download to our local machine. 

Instead of going through the hassle of finding the Jars online, build tools such as [Maven](https://maven.apache.org/) and [Gradle](https://gradle.org/) ask you to define which dependencies you want and they will search through an online repository ([Maven Repository](https://mvnrepository.com/)) and download the Jars,

---

#### Compiling code with the right configuration 🔧
A typical Java project might have tens or hundreds of Java classes, each of those have to be compiled with the right configuration.
 
Now, running the `javac` command with all the classes' names doesn't seem to be a reasonable thing to do as we will have a very long command which we have to update on every new addition of deletion of classes.

Build tools scripts takes care of this for us, as they ask us to follow a convention for our project directory names and they will ensure that everything is compiled and linked correctly.

---

#### Running tests and reporting their results🚦
Running the tests without a build tool requires us to run commands from the command line.
Let's take the JUnit testing framework for example:
Running tests in the class `MyClassWithUnitTests`
```
java -cp /path/to/my.jar:/path/to/junit.jar junit.textui.TestRunner com.mypackage.MyClassWithUnitTests
```
Notice the long command and the need to be careful with the classpath and the location of the Jars. This will get unmaintainable with a big number of tests.

On the other hand, build tools like **Maven** allow us to run the all tests with a single command `mvn test`. It also offers the ability to run tests in one or  more classes using only the classes' names.

---

#### Packaging the compiled code into executable form 💾
The `jar` command allows us to create a Jar file from our compiled classes.
Let's look at a simple example of creating a Jar file:
```
jar cfe JarExample.jar com.tutorial.JarExample com/tutorial/*.class
```
- The `c` option indicates that we're creating a file.
- The `f` option specifies the file name (JarExample.jar).
- The `e` option specifies our entry point, and the jar command will add it to the generated manifest file.

Building tools like Maven does the work for us using a single command `mvn package`

---

## Build Tools Examples
The Java ecosystem has [many building tools](https://www.filecloud.com/blog/2019/06/top-10-build-automation-software-for-2019/#.Yi8B9Y9Bzrc) that are interesting and worth overviewing. We will cover the most important two however.

### Maven 🖊
It was released in 2004 as an improvement to Apache Ant. It is an XML-based build tool. It has many plugins to customize the build process.

A Maven project is primarily defined by Project Object Model (POM) file written in XML. This POM.xml file contain the project’s dependencies, plugins, properties, and configuration data. 

### Gradle 🐘
Gradle was on Maven’s concepts, it was introduced as Maven’s successor. Rather than using Maven’s XML-based project configuration, it introduced a domain-specific language (DSL) based on the Groovy and Kotlin programming languages.

---
## Conclusion 🤝

Build tools automate the repetitive work (Compile, Link, Test, Bundle) we do to get from code to production ready artifacts.