0% found this document useful (0 votes)
5 views16 pages

Maven and Gradle Build Tools Guide

unit 2
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views16 pages

Maven and Gradle Build Tools Guide

unit 2
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

COMPILE AND BUILD

USING MAVEN & GRADLE

01/06/2026 1
Introduction
Build tools automate the process of compiling source
code, running tests, packaging, and deploying
applications.

Two major tools:


Maven (XML-based)
Gradle (Groovy/Kotlin DSL-based)
Installation of Maven
Apache Maven is a build automation and dependency management tool for Java projects.
Uses a Project Object Model (POM) file (`[Link]`) to manage the project’s configuration.
Convention over configuration.
Download Maven from [[Link]
Set environment variables:
Add Maven to `PATH`
Verify installation on CMD or shell:
>mvn -version
Apache Maven 3.9.11 (3e54c93a704957b63ee3494413a2b544fd3d825b)
Maven home: C:\apache-maven-3.9.11
Java version: 24.0.2, vendor: Oracle Corporation, runtime: C:\Program Files\Java\jdk-24
Default locale: en_GB, platform encoding: UTF-8
OS name: "windows 11", version: "10.0", arch: "amd64", family: "windows"
POM files
POM stands for Project Object Model. It is an XML file ([Link]) that contains all the configuration
details and metadata required for Maven to build a project.
Purpose of POM:
• Defines the project and its dependencies
• Specifies build plugins and goals
• Manages versioning and repositories
• Configures the build lifecycle and other Maven settings

Important Tags Explained:


• <project>: Root element of the POM file
• <modelVersion>: Specifies the model version of POM (always 4.0.0)
• <groupId>: Organization or group the project belongs to
• <artifactId>: Name of the project or module
• <version>: Version of the project
• <dependencies>: External libraries needed by the project
• <build>: Configuration for build settings like plugins
• <plugins>: Tools to extend Maven functionality, e.g., compiler
POM Sample Code
Maven Build lifecycle
Cl • Cleans up the previous builds
ea
n
D • Compiles,tests,packages, and
ef installs.
au
lt
• Generates documentation
Sit
e
Build phases(compile build, test, package)

•validate​- Checks project is correct​


•compile​- Compiles source code​
•test​- Runs unit tests​
•package​- Bundles into JAR/WAR​
•verify​- Verifies package meets criteria​
•install​- Installs package in local repo​
•deploy​- Deploys to remote repo​
Maven Profiles
Maven Profiles allow you to customize the build process for different
environments (like development, testing, production) by changing
configuration settings such as dependencies, plugins, properties, and
build goals.

Why Use Maven Profiles?


You might want different configurations for:

• Building with/without debug info

• Deploying to different servers

• Using different databases for dev and prod

• Skipping tests in some builds


Maven repositories(local, central, global),
Type Description

Local On your system (~/.m2)

Central Default remote repository

Remote Any external repo you specify


Maven plugins
Maven plugins are tools that extend Maven's capabilities during the build process.
They allow you to perform tasks such as compiling code, running tests, creating
JAR/WAR files, generating reports, packaging, and deploying your application.
Each plugin consists of one or more goals, which are specific tasks that can be
executed during different phases of the Maven build lifecycle.
examples of commonly used Maven plugins

1. Compiler Plugin​
Used to compile Java source code.​
2. Surefire Plugin​
Used to run unit tests.​

3. JAR Plugin​
Used to package the project as a JAR.​

4. Install Plugin​
Installs the project artifact into the local Maven repository.​

5. Deploy Plugin​
Used to deploy your artifact to a remote repository.​

6. Clean Plugin​
Cleans the target directory before building.​

7. Site Plugin​
Generates project documentation.​

8. Resources Plugin​
Copies and filters resources to the output directory.
Maven create and build Artificats
Maven Create and Build Artifacts refers to the mvn package – Builds the artifact using the
process where Maven compiles source code, runs [Link] and source code
tests, and packages the final output (like a JAR,
WAR, or EAR file), which is called an artifact.

mvn install – Install the artifact into your


local Maven repository
Dependency management in Maven is the mechanism that
allows you to manage external libraries (JAR files) your project
depends on. Instead of manually downloading and configuring
them, Maven automatically downloads the required libraries
and their transitive dependencies from a central repository.

Dependency
managemen How Dependency Management Works

Declare Dependencies in pom, You define dependencies


inside the <dependencies> section.
Installation of Gradle
Gradle is a powerful build automation tool used primarily for Java, Kotlin, Groovy, and Android projects. It
combines the best features of Apache Maven and Ant.
Installing Gradle
There are multiple ways to install Gradle:
# Method 1: Manual Installation
Steps:
1. Download Gradle from the official site
2. Extract the ZIP to a directory, e.g., `C:\Gradle` or `/opt/gradle`
3. Set Environment Variables:
4. Verify Installation using : gradle -v
Understand build using Gradle
Gradle Project Structure

Common Gradle Commands


Phase Description
Determines which projects are
Initialization
involved in the build
Creates a task graph by evaluating
Configuration
the build scripts
Executes the tasks in the correct
Execution
order
Understand build using Gradle
Command Description
gradle init Initializes a new Gradle project
gradle build Compiles, tests, and packages
gradle clean Deletes previous build output
gradle test Runs test cases
gradle run Runs the application (if set up)
gradle tasks Lists available tasks

Advantages of Gradle
• Faster builds with incremental compilation
• Flexible DSL (Groovy/Kotlin)
• Great support for Android development
• Integration with CI/CD tools like Jenkins, GitLab CI

You might also like