% !TeX root = ../arara-manual.tex
\chapter{Building from source}
\label{chap:buildingfromsource}

\arara\ is a Kotlin application licensed under the \href{http://www.opensource.org/licenses/bsd-license.php}{New BSD License}, a verified GPL-compatible free software license, and the source code is available in the project repository at \href{https://gitlab.com/islandoftex/arara}{GitLab}. This chapter provides detailed instructions on how to build our tool from source.

\section{Requirements}
\label{sec:requirements}

In order to build our tool from source, we need to ensure that our development environment has the minimum requirements for a proper compilation. Make sure the following items are available:

\begin{itemize}[label={\cbyes{-2}}]
\item On account of our project being hosted at \href{https://gitlab.com/}{GitLab}, an online source code repository, we highly recommend the installation of \rbox{git}, a version control system for tracking changes in computer files and coordinating work on those files among multiple people. Alternatively, you can directly obtain the source code by requesting a \href{https://gitlab.com/islandoftex/arara/-/archive/master/arara-master.zip}{source code download} in the repository. In order to check if \rbox{git} is available in your operating system, run the following command in the terminal (version numbers might vary):

\begin{codebox}{Terminal}{teal}{\icnote}{white}
$ git --version
git version 2.29.2
\end{codebox}

Please refer to the \rbox{git} \href{https://git-scm.com/}{project website} in order to obtain specific installation instructions for your operating system. In general, most recent Unix systems have \rbox{git} installed out of the shelf.

\item Our tool is written in the Kotlin programming language, so we need a proper Java Development Kit, a collection of programming tools for the Java platform. Our source code is known to be compliant with several vendors, including Oracle, OpenJDK, and Azul Systems. In order to check if your operating system has the proper tools, run the following command in the terminal (version numbers might vary):

\begin{codebox}{Terminal}{teal}{\icnote}{white}
$ javac -version
javac 1.8.0_171
\end{codebox}

The previous command, as the name suggests, refers to the \rbox{javac} tool, which is the Java compiler itself. The most common Java Development Kit out there is from \href{http://www.oracle.com/technetwork/java/javase/downloads/index.html}{Oracle}. However, several Linux distributions (as well as some developers, yours truly included) favour the OpenJDK vendor, so your mileage may vary. Please refer to the corresponding website of the vendor of your choice in order to obtain specific installation instructions for your operating system.

\item As a means to provide a straightforward and simplified compilation workflow, \arara\ relies on Gradle, a software project management and comprehension tool. Gradle is a build tool just like ours with a much more comprehensive build framework to provide support for the JVM ecosystem. In order to check if \rbox{gradle}, the Gradle binary, is available in your operating system, run the following command in the terminal (version numbers might vary):

\begin{codebox}{Terminal}{teal}{\icnote}{white}
$ gradle --version
------------------------------------------------------------
Gradle 6.0.1
------------------------------------------------------------

Build time:   2019-11-21 11:47:01 UTC
Revision:     <unknown>

Kotlin:       1.3.50
Groovy:       2.5.8
Ant:          Apache Ant(TM) version 1.10.7 compiled
              on September 1 2019
JVM:          1.8.0_232 (Oracle Corporation 25.232-b09)
OS:           Linux 5.5.0-1-MANJARO amd64
\end{codebox}

Please refer to the Gradle \href{https://gradle.org}{project website} in order to obtain specific installation instructions for your operating system. In general, most recent Linux distributions have the Gradle binary, as well the proper associated dependencies, available in their corresponding repositories.

\item For a proper repository cloning, as well as the first Gradle build, an active Internet connection is required. In particular, Gradle dynamically downloads Java libraries and plug-ins from one or more online repositories and stores them in a local cache. Be mindful that subsequent builds can occur offline, provided that the local Gradle cache exists.
\end{itemize}

\arara\ can be easily built from source, provided that the aforementioned requirements are available. The next section presents the compilation details, from repository cloning to a proper Java archive generation.

\begin{messagebox}{One tool to rule them all}{araracolour}{\icok}{white}
\setlength{\parskip}{1em}
For the brave, there is the \href{https://sdkman.io/}{Software Development Kit Manager}, an interesting tool for managing parallel versions of multiple software development kits on most Unix based systems. In particular, this tool provides off the shelf support for several Java Development Kit vendors and versions, as well as most recent versions of Gradle.

Personally, I prefer the packaged versions provided by my favourite Linux distribution (Fedora), but this tool is a very interesting alternative to set up a development environment with little to no effort.
\end{messagebox}

\section{Compiling the tool}
\label{sec:compilingthetool}

First and foremost, we need to clone the project repository into our development environment, so we can build our tool from source. The cloning will create a directory named \abox[araracolour]{arara/} within the current working directory, so remember to first ensure that you are in the appropriate directory. For example:

\begin{codebox}{Terminal}{teal}{\icnote}{white}
$ mkdir git-projects
$ cd git-projects
\end{codebox}

Run the following command in the terminal to clone the \arara\ project:

\begin{codebox}{Terminal}{teal}{\icnote}{white}
$ git clone https://gitlab.com/islandoftex/arara.git
\end{codebox}

Wait a couple of seconds (or minutes, depending on your Internet connection) while the previous command clones the project repository hosted at GitLab. Be mindful that this operation pulls down every version of every file for the history of the project. Fortunately, the version control system has the notion of a \emph{shallow clone}, which is a more succinctly meaningful way of describing a local repository with history truncated to a particular depth during the clone operation. If you want to get only the latest revision of everything in our repository, run the following command in the terminal:

\begin{codebox}{Terminal}{teal}{\icnote}{white}
$ git clone https://gitlab.com/islandoftex/arara.git --depth 1
\end{codebox}

This operation is way faster than the previous one, for obvious reasons. Unix terminals typically start at \abox[araracolour]{USER\_HOME} as working directory, so, if you did not \rbox{cd} to another directory (as in the earlier example), the newly cloned \abox[araracolour]{arara/} directory is almost certain to be accessible from that level. Now, we need to navigate to the directory named \abox[araracolour]{arara/}. Run the following command in the terminal:

\begin{codebox}{Terminal}{teal}{\icnote}{white}
$ cd arara
\end{codebox}

The previous command should take us inside the \abox[araracolour]{arara/} directory of our project, where the source code and the corresponding build file are located. Let us make sure we are in the correct location by running the following command in the terminal:

\begin{codebox}{Terminal}{teal}{\icnote}{white}
$ ls build.gradle.kts
build.gradle.kts
\end{codebox}

Great, we are in the correct location! From the previous output, let us inspect the directory contents. The \abox[araracolour]{application/} directory, as the name suggests, contains the source code of the main application organized in an established package structure, whereas \rbox{build.gradle.kts} is the corresponding Gradle build file written to efficiently compile the project. In order to build our tool, run the following command in the terminal:

\begin{codebox}{Terminal}{teal}{\icnote}{white}
$ gradle build
\end{codebox}

Gradle is based around the central concept of a build life cycle. The \rbox{compile} phase, as the name suggests, compiles the source code of the project using the underlying Java compiler. After compiling, the code can be packaged, tested and run. The \rbox{build} target actually compiles, tests and packages our tool. Afterwards, you will have a \abox[araracolour]{cli/build/libs/} directory with multiple JAR files, one containing \rbox{with-deps}. That file is ready to run as it bundles all dependencies. Subsequent builds will be significantly faster than the first build because they do not fetch dependencies and rely on a build cache. Finally, after some time, Gradle will output the following message as result (please note that the entire compilation and packaging only took 4 seconds on my development machine due to an existing local cache):

\begin{codebox}{Terminal}{teal}{\icnote}{white}
BUILD SUCCESSFUL in 4s
15 actionable tasks: 15 up-to-date
\end{codebox}

Now, let us move the resulting Java archive file from that particular directory to our current directory. Run the following command in the terminal (please note that the Java archive file was also renamed during the move operation):

\begin{codebox}{Terminal}{teal}{\icnote}{white}
$ mv cli/build/libs/arara-cli-with-deps-*.jar arara.jar
\end{codebox}

Now, our current directory contains the final \rbox{arara.jar} Java archive file properly built from source. This file can be safely distributed and deployed, as seen later on, in Chapter~\ref{chap:deployingthetool}, on page~\pageref{chap:deployingthetool}. You can also test the resulting file by running the following command in the terminal:

\begin{codebox}{Terminal}{teal}{\icnote}{white}
$ java -jar arara.jar
  __ _ _ __ __ _ _ __ __ _
 / _` | '__/ _` | '__/ _` |
| (_| | | | (_| | | | (_| |
 \__,_|_|  \__,_|_|  \__,_|

Usage: arara [OPTIONS] file...

Options:
  -l, --log                     Generate a log output
  -v, --verbose / -s, --silent  Print the command output
  -n, --dry-run                 Go through all the motions of running
                                a command, but with no actual calls
  -s, --safe-run                Run in safe mode and disable
                                potentially harmful features. Make
                                sure your projects uses only allowed
                                features.
  -H, --header                  Extract directives only in the file
                                header
  -p, --preamble TEXT           Set the file preamble based on the
                                configuration file
  -t, --timeout INT             Set the execution timeout (in
                                milliseconds)
  -L, --language TEXT           Set the application language
  -m, --max-loops INT           Set the maximum number of loops (> 0)
  -d, --working-directory PATH  Set the working directory for all
                                tools
  -P, --call-property VALUE     Pass parameters to the application
                                to be used within the session.
  -V, --version                 Show the version and exit
  -h, --help                    Show this message and exit

Arguments:
  file  The file(s) to evaluate and process
\end{codebox}

The following optional Gradle phase is used to handle the project cleaning, including the complete removal of the \abox[araracolour]{build/} directory. As a result, the project is then restored to the initial state without any generated Java bytecode. Run the following command in the terminal:

\begin{codebox}{Terminal}{teal}{\icnote}{white}
$ gradle clean
\end{codebox}

This section covered the compilation details for building \arara\ from source. The aforementioned steps are straightforward and can be automated in order to generate snapshots and daily builds. If you run into any issue, please let us know. Happy compilation!
