Week 31 — What are build tools and which problem do they solve?
Question of the Week #31
What are build tools and which problem do they solve?
8 Replies
The build tool is a piece of software that takes all parts of the source code, and produces executable objects. One of the main features of the build tools are dependency management. You can download and update different versions of libraries located remotely or locally.
Submission from gargatuma#0000
Build tools are frameworks or scripts used to automatically download and install listed dependencies (e.g. libraries; versions) which are then being shipped with the programm. It solves the problem of having to update lots of dependencies and checking compatability whenever there is a change
Submission from Soulvenom#0749
Build tools automate the process of compiling, assembling, and deploying software, which is tedious to do by hand, especially when dealing with large projects. Build tools also make the building process more consistent.
Submission from accq#0000
A build tool is a tool that helps set up setting for development. It is a program for automating application generation from source code. Examples of build tools are Gradle or Maven
Submission from wooj0408#0000
Software build tools are used to automate the creation of executable applications from source code. They script or automate a variety of tasks, such as downloading dependencies, compiling and packaging code, running tests, and deployment.
Submission from geekynerd_#0000
Build tools are a class of tools that automate some or all of the build process, including but not restricted to: environment configuration, dependency management, linting, compiling, testing, documentation generation, and packaging. Build tools provide reproducibility in the build process for developers. Commonly used build tools in Java development include Ant, Maven, Gradle, and sometimes SBT. Many build tools are also capable of automating the process of starting a new project, oftentimes utilising a template.
⭐ Submission from gachrud#0000
Build tools are managing the dependencies and build of a program.
Without a build tool, it is inconvenient to manage a project using many libraries and other tooling, especially when these libraries should be updated and when multiple people work together on a project.
A build tool can then automatically resolve and download the dependencies, run the project as well as the tests, deploy the project or similar.
With Java, the main build tools are Maven and Gradle.
Maven is configured using a file called
pom.xml
while Gradle is configured using a build.gradle
(Groovy) file.
Both can resolve and download dependencies from Maven repositories which are then used within the project and manage version conflicts.
Furthermore, it is possible to configure what artefacts to produce and how, use custom logic/tooling, and e.g. run tests.
Maven can be installed from https://maven.apache.org/download.cgi
By default, Maven expects source files to be in src/main/java
while resources are typically in src/main/resources
. Test source files and test resources are normally in src/test/java
and src/test/resources
.
A minimal pom.xml
looks like the following:
However, this configuration will use an outdated Java version. The Java version can be set to something newer with the maven-compiler-plugin
:
Then, one can also add dependencies using <dependency>
-blocks:
In this case, the pom.xml
declares JUnit as a dependency. The dependency is resolved using the combination of the dependency's group id, artifact id and version (artifact coordinates).
the <scope>test</scope>
block means that the dependency is only available in tests (src/test/java
). See https://canary.discord.com/channels/648956210850299986/1055111097562181684 for Maven scopes.
In order to avoid retyping the same version for both JUnit dependencies, it is possible to create a property and reference that somewhere else in the pom.xml
file:
In order to actually use the some dependencies, one can add more dependencies create a class somewhere in src/main/java
using these dependencies:
as well as a test class in src/test/java
:
The main class can be run using the mvn exec:java command, e.g.
However, a plugin is needed in order to run all the tests. A popular option for this is the maven surefire plugin:
With the Maven surefire plugin added to the
pom.xml
, it is possible to execute the tests using the command mvn test
.
In order to create a JAR with all necessary dependencies, one can use another plugin called the Maven assembly plugin by adding the following to the plugins
section:
Upon executing mvn package
, Maven should then create a JAR file ending with jar-with-dependencies.jar
. This can then be executed with the java -jar
command, e.g.
⭐ Submission from dan1st#0000