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.
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.
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 and Gradle ask you to define which dependencies you want and they will search through an online repository (Maven Repository) and download the Jars,
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 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
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.
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
coption indicates that we're creating a file.
foption specifies the file name (JarExample.jar).
eoption 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
The Java ecosystem has many building tools that are interesting and worth overviewing. We will cover the most important two however.
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 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.
Build tools automate the repetitive work (Compile, Link, Test, Bundle) we do to get from code to production ready artifacts.