The build process is typically one of compiling the source files of a project, running tests, and producing a finished product or products.
In some companies there are whole teams whose sole job is maintaining and updating the build process. In any project of any size, it helps to have a good automated build tool.
Ant
Ant is the first really popular project builder for Java that existed. It is XML-based and requires you to create tasks in XML that can be executed by Ant. An Ant build file is typically named build.xml and has a <project> root element.
A task is a division of work. Tasks depend on other tasks. For example, the jar task usually depends on the compile task. Although Maven threw away the task concept, it was used again in Gradle.
Critics of Ant complain that it uses XML (a declarative and highly verbose format) and requires a lot of work to do simple things. However, it was a very popular tool for many years and is still used in many places.
Maven
Maven is an XML-based declarative project manager. Maven is used for building Java projects but is capable of much more. Maven is also a set of standards that allows Java/JVM developers to easily define and integrate dependencies into large projects. Maven somewhat replaces Ant but can also integrate with it and other build tools.
Maven was mostly a reaction to the huge number of open source libraries Java projects tend to rely on. It has a built-in standard for dependency management (managing the inter-dependencies of open source libraries).
Although Maven is an Apache open source project, it could be said that the core of Maven is Maven Central, a repository of open source libraries run by Sonatype, the company behind Maven. There are many other repositories that follow the Maven standard, such as JFrog’s jCenter,4 so you are not restricted to Maven Central.
Ivy5 is a similar build tool, but is more closely related to Ant.
Many build tools, such as Ivy and Gradle, build on top of Maven’s concept.
Using Maven
Basic properties (artifactId, groupId, name, version, packaging)
Dependencies
Plug-ins
There is a Maven plug-in for every major Java-based IDE out there (Eclipse, NetBeans, and IntelliJ IDEA), and they are very helpful. You can use the Maven plug-in to create your project, add dependencies, and edit your POM files.
Starting a New Project
If you want to use any additional third-party libraries, you will have to edit the POM to include each dependency. Fortunately, most IDEs make it easy to add dependencies to the POM.
Maven: The Complete Reference6 is available online if you want to learn more.
Life Cycle
- 1.
validate: Validates that the project is correct and all necessary information is available
- 2.
compile: Compiles the source code of the project
- 3.
test: Tests the compiled source code, using a suitable unit-testing framework
- 4.
package: Takes the compiled code and packages it in its distributable format, such as a JAR
- 5.
integration-test: Processes and deploys the package, if necessary, into an environment in which integration tests can be run
- 6.
verify: Runs any checks to verify that the package is valid and meets quality criteria
- 7.
install: Installs the package into the local repository, for use as a dependency in other projects locally
- 8.
deploy: Copies, in an integration or release environment, the final package to the remote repository, for sharing with other developers and projects
There are more phases,7 but you don’t need to know all of them until you are doing more complex builds.
Executing Code
Sometimes, however, you just need more control over your build. In Maven, you can execute Groovy code, Ant build files, and Scala code, and you can even write your own plug-ins in Groovy.
The preceding code would write out every dependency of the project into the file deps.txt. Then it would copy all of the source files into the project.build.outputDirectory (usually target/classes).
Gradle
Gradle is an automated build tool with a Groovy native DSL (domain-specific language) for defining project builds. It also has a Kotlin native DSL and is the official build tool for Android projects.
From mobile apps to microservices, from small startups to big enterprises, Gradle helps teams build, automate and deliver better software, faster.
Getting Started with Gradle
When prompted you can choose a Java application with Junit 4 tests and a Groovy build script (type 2, enter, 3, enter, 1, enter, 1, enter). This will create a build.gradle file, a settings.gradle file, gradlew, and gradlew.bat (which allow you to run Gradle from any system without even installing it) and some basic code and a test.
Projects and Tasks
Each Gradle build is composed of one or more projects and each project is composed of one or more tasks.
Tasks can contain any Groovy code (or Kotlin if using the Kotlin DSL), but you can take advantage of some of the many existing Gradle plugins to quickly produce a dependable and fast build.
Plugins
Add tasks to the project (e.g., compile, test).
Pre-configure added tasks with useful defaults.
Add dependency configurations to the project.
Add new properties and methods to existing type, via extensions.
We’re going to concentrate on building Java-based projects, so we’ll be using the java plugin; however, Gradle is not limited to Java projects.
This enables the java plugin and the application plugin.
Gradle uses standard project organization conventions. For example, it expects to find your production Java source code under src/main/java and your test Java source code under src/test/java. This is the same style expected by Maven.
Dependencies
This build script uses sourceCompatibility to define the Java source code version of Java 11 (which is used during compilation). Next, it tells Maven to use the local repository first (mavenLocal), then Maven Central.
In the dependencies block, this build script defines two dependencies for the implementation scope and two for testImplementation scope. Jars in the testImplementation scope are only used in tests and won’t be included in any final products.
The line for JUnit shows the more verbose style for defining dependencies. The “+” here means that version or greater.
Do First and Last
Your jar file will now be located in the build/libs/ directory of your project.
Gradle has a huge online user guide available online at gradle.org.10