Get up to speed with the core of Maven quickly, and then go beyond the foundations into the more powerful functionality of the build tool, such as profiles, scopes, multi-module projects and quite a bit more:
Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.
Get started with mocking and improve your application tests using our Mockito guide:
Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.
Get started with understanding multi-threaded applications with our Java Concurrency guide:
Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:
Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.
But these can also be overused and fall into some common pitfalls.
To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:
Get started with Spring and Spring Boot, through the Learn Spring course:
>> LEARN SPRINGExplore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:
Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.
I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.
You can explore the course here:
Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.
Get started with Spring Data JPA through the guided reference course:
Refactor Java code safely β and automatically β with OpenRewrite.
Refactoring big codebases by hand is slow, risky, and easy to put off. Thatβs where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.
Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions β one for newcomers and one for experienced users. Youβll see how recipes work, how to apply them across projects, and how to modernize code with confidence.
Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.
β’ Creating a Fat Jar in Gradle
1. Introduction
In this article, weβll explore three Java build automation tools that dominated the JVM ecosystem β Ant, Maven, and Gradle.
Weβll introduce each of them and explore how Java build automation tools evolved.
2. Apache Ant
In the beginning, Make was the only build automation tool available beyond homegrown solutions. Make has been around since 1976 and as such, it was used for building Java applications in the early Java years.
However, a lot of conventions from C programs didnβt fit in the Java ecosystem, so in time Ant took over as a better alternative.
Apache Ant (βAnother Neat Toolβ) is a Java library used for automating build processes for Java applications. Additionally, Ant can be used for building non-Java applications. It was initially part of Apache Tomcat codebase and was released as a standalone project in 2000.
In many aspects, Ant is very similar to Make, and itβs simple enough so anyone can start using it without any particular prerequisites. Ant build files are written in XML, and by convention, theyβre called build.xml.
Different phases of a build process are called βtargetsβ.
Here is an example of a build.xml file for a simple Java project with the HelloWorld main class:
<project>
<target name="clean">
<delete dir="classes" />
</target>
<target name="compile" depends="clean">
<mkdir dir="classes" />
<javac srcdir="src" destdir="classes" />
</target>
<target name="jar" depends="compile">
<mkdir dir="jar" />
<jar destfile="jar/HelloWorld.jar" basedir="classes">
<manifest>
<attribute name="Main-Class"
value="antExample.HelloWorld" />
</manifest>
</jar>
</target>
<target name="run" depends="jar">
<java jar="jar/HelloWorld.jar" fork="true" />
</target>
</project>
This build file defines four targets: clean, compile, jar and run. For example, we can compile the code by running:
ant compile
This will trigger the target clean first which will delete the βclassesβ directory. After that, the target compile will recreate the directory and compile the src folder into it.
The main benefit of Ant is its flexibility. Ant doesnβt impose any coding conventions or project structures. Consequently, this means that Ant requires developers to write all the commands by themselves, which sometimes leads to huge XML build files that are hard to maintain.
Since there are no conventions, just knowing Ant doesnβt mean weβll quickly understand any Ant build file. Itβll likely take some time to get accustomed to an unfamiliar Ant file, which is a disadvantage compared to the other, newer tools.
At first, Ant had no built-in support for dependency management. However, as dependency management became a must in the later years, Apache Ivy was developed as a sub-project of the Apache Ant project. Itβs integrated with Apache Ant, and it follows the same design principles.
However, the initial Ant limitations due to not having built-in support for dependency management and frustrations when working with unmanagable XML build files led to the creation of Maven.
3. Apache Maven
Apache Maven is a dependency management and a build automation tool, primarily used for Java applications. Maven continues to use XML files just like Ant but in a much more manageable way. The name of the game here is convention over configuration.
While Ant gives flexibility and requires everything to be written from scratch, Maven relies on conventions and provides predefined commands (goals).
Simply put, Maven allows us to focus on what our build should do, and gives us the framework to do it. Another positive aspect of Maven was that it provided built-in support for dependency management.
Mavenβs configuration file, containing build and dependency management instructions, is by convention called pom.xml. Additionally, Maven also prescribes a strict project structure, while Ant provides flexibility there as well.
Hereβs an example of a pom.xml file for the same simple Java project with the HelloWorld main class from before:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>baeldung</groupId>
<artifactId>mavenExample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<description>Maven example</description>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
However, now the project structure has been standardized as well and conforms to the Maven conventions:
+---src
| +---main
| | +---java
| | | \---com
| | | \---baeldung
| | | \---maven
| | | HelloWorld.java
| | |
| | \---resources
| \---test
| +---java
| \---resources
As opposed to Ant, there is no need to define each of the phases in the build process manually. Instead, we can simply call Mavenβs built-in commands.
For example, we can compile the code by running:
mvn compile
At its core, as noted on official pages, Maven can be considered a plugin execution framework, since all work is done by plugins. Maven supports a wide range of available plugins, and each of them can be additionally configured.
One of the available plugins is Apache Maven Dependency Plugin which has a copy-dependencies goal that will copy our dependencies to a specified directory.
To show this plugin in action, letβs include this plugin in our pom.xml file and configure an output directory for our dependencies:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>target/dependencies
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
This plugin will be executed in a package phase, so if we run:
mvn package
Weβll execute this plugin and copy dependencies to the target/dependencies folder.
There is also an existing article on how to create an executable JAR using different Maven plugins. Additionally, for a detailed Maven overview, have a look at this core guide on Maven, where some Mavenβs key features are explored.
Maven became very popular since build files were now standardized and it took significantly less time to maintain build files, comparing to Ant. However, though more standardized than Ant files, Maven configuration files still tend to get big and cumbersome.
Mavenβs strict conventions come with the price of being a lot less flexible than Ant. Goal customization is very hard, so writing custom build scripts is a lot harder to do, compared with Ant.
Although Maven has made some serious improvements regarding making applicationβs build processes easier and more standardized, it still comes with a price due to being a lot less flexible than Ant. This lead to the creation of Gradle which combines the best of both worlds β Antβs flexibility and Mavenβs features.
4. Gradle
Gradle is a dependency management and a build automation tool that was built upon the concepts of Ant and Maven.
One of the first things we can note about Gradle is that itβs not using XML files, unlike Ant or Maven.
Over time, developers became more and more interested in having and working with a domain-specific language β which, simply put, would allow them to solve problems in a specific domain using a language tailored for that particular domain.
This was adopted by Gradle, which is using a DSL based either on Groovy or Kotlin. This led to smaller configuration files with less clutter since the language was specifically designed to solve specific domain problems. Gradleβs configuration file is by convention called build.gradle in Groovy, or build.gradle.kts in Kotlin.
Notice that Kotlin offers better IDE support than Groovy for auto-completion and error detection.
Here is an example of a build.gradle file for the same simple Java project with the HelloWorld main class from before:
apply plugin: 'java'
repositories {
mavenCentral()
}
jar {
baseName = 'gradleExample'
version = '0.0.1-SNAPSHOT'
}
dependencies {
testImplementation 'junit:junit:4.12'
}
We can compile the code by running:
gradle classes
At its core, Gradle intentionally provides very little functionality. Plugins add all useful features. In our example, we were using java plugin which allows us to compile Java code and other valuable features.
Gradle gave its build steps name βtasksβ, as opposed to Antβs βtargetsβ or Mavenβs βphasesβ. With Maven, we used Apache Maven Dependency Plugin, and itβs a specific goal to copy dependencies to a specified directory. With Gradle, we can do the same by using tasks:
task copyDependencies(type: Copy) {
from configurations.compile
into 'dependencies'
}
We can run this task by executing:
gradle copyDependencies
5. Conclusion
In this article, we presented Ant, Maven, and Gradle β three Java build automation tools.
Not surprisingly, Maven holds the majority of the build tool market today.
Gradle, however, has seen good adoption in more complex codebases, for the following reasons:
- Lots of open-source projects such as Spring are using it now
- It is faster than Maven for most scenarios, thanks to its incremental builds
- It offers advanced analysis and debugging services
However that Gradle seems to have a steeper learning curve, especially if youβre not familiar with Groovy or Kotlin.
