0% found this document useful (0 votes)
27 views15 pages

UNIT2

Apache Ant, Maven, and Gradle are popular Java-based build automation tools, each with distinct features and use cases. Ant is customizable and flexible but verbose, while Maven offers structured project management and automatic dependency resolution, and Gradle provides modern performance and flexibility with incremental builds. Maven repositories manage dependencies efficiently through local, central, and remote repositories, with a specific resolution order to locate required artifacts.

Uploaded by

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

UNIT2

Apache Ant, Maven, and Gradle are popular Java-based build automation tools, each with distinct features and use cases. Ant is customizable and flexible but verbose, while Maven offers structured project management and automatic dependency resolution, and Gradle provides modern performance and flexibility with incremental builds. Maven repositories manage dependencies efficiently through local, central, and remote repositories, with a specific resolution order to locate required artifacts.

Uploaded by

jaatantil0886
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Apache Ant - Build Tool

Apache Ant (Another Neat Tool) is a Java-based build automation tool used primarily for compiling,
assembling, testing, and deploying Java applications. It is similar to Make but is platform-independent
and uses XML to define build scripts.
Key Features of Ant
1. XML-Based Configuration: Uses XML files (build.xml) to define tasks and dependencies.
2. Platform-Independent: Written in Java, making it cross-platform.
3. Task-Oriented: Provides built-in tasks for compiling code, packaging files, running tests, etc.
4. Extensible: Allows the creation of custom tasks using Java.
5. Integration with IDEs: Supported by Eclipse, NetBeans, and IntelliJ IDEA.
6. Dependency Management: Supports project dependencies but is not as advanced as Maven
or Gradle.
7. Procedural Execution: Executes tasks in a step-by-step manner as defined in the XML file.
8. Flexible: Unlike Maven, it does not enforce a specific project structure.

Pros of Ant
✔ Customizable: Highly flexible build process without predefined constraints.
✔ Cross-Platform: Works on any OS with Java installed.
✔ Extensible: Can create custom tasks using Java or external libraries.
✔ IDE Support: Works well with major Java IDEs.
✔ Integration Friendly: Can integrate with tools like Jenkins, Docker, and Git.

Cons of Ant
❌ XML Verbosity: Requires writing long and complex XML scripts.
❌ No Convention-over-Configuration: Unlike Maven, it does not enforce project structure or
dependency management.
❌ Manual Dependency Management: Does not have built-in dependency resolution like Maven or
Gradle.
❌ Procedural Approach: No declarative build system; users must specify each step explicitly.
❌ Less Modern: Gradle has replaced Ant in many projects due to its more efficient scripting and
dependency management.
Conclusion
Apache Ant is a powerful and flexible build tool but is gradually being replaced by Maven and Gradle
due to its verbosity and lack of built-in dependency management. However, it is still useful for legacy
projects and custom build processes where full control is required.

Apache Maven - Build Tool


Apache Maven is a powerful build automation tool primarily used for Java projects. It simplifies
project management by providing a structured way to build, test, and deploy applications with built-in
dependency management.

Key Features of Maven


1. Project Object Model (POM): Uses pom.xml for configuration and dependencies.
2. Convention over Configuration: Follows a standard project structure, reducing manual
setup.
3. Dependency Management: Automatically downloads and manages project dependencies.
4. Lifecycle Management: Provides predefined build lifecycle phases like compile, test,
package, and deploy.
5. Extensibility: Supports plugins to extend functionality.
6. Multi-Module Project Support: Can manage multiple projects under a single build system.
7. Repository-Based Approach: Uses local and remote repositories (like Maven Central) for
dependency resolution.
8. Integration with IDEs & CI/CD: Supported by Eclipse, IntelliJ, Jenkins, and GitHub
Actions.

Pros of Maven
✔ Standardized Build Process: Follows a uniform structure, making projects easier to manage.
✔ Automatic Dependency Management: No need to manually download and configure JAR files.
✔ Predefined Build Lifecycle: Reduces the need for writing custom build scripts.
✔ Plugin Support: Extensible with a wide range of plugins.
✔ Better Integration: Works seamlessly with CI/CD pipelines and cloud-based builds.
✔ Multi-Module Support: Ideal for large-scale enterprise applications.

Cons of Maven
❌ Learning Curve: Requires understanding of pom.xml, dependencies, and lifecycle.
❌ Slower Builds: Fetching dependencies and plugins can slow down the build process.
❌ Less Flexibility: Strict project structure can be limiting for custom build processes.
❌ XML Complexity: pom.xml can become complex and difficult to manage in large projects.
❌ Overhead for Small Projects: May be overkill for simple applications.

Conclusion
Apache Maven is widely used for Java-based projects due to its structured approach, dependency
management, and ease of integration. However, for more flexibility and faster performance, modern
alternatives like Gradle are becoming increasingly popular.
Gradle - Build Tool
Gradle is a powerful build automation tool designed for Java, Kotlin, Groovy, and other languages. It
is known for its performance, flexibility, and incremental build capabilities, making it a preferred
choice over Ant and Maven.

Key Features of Gradle


1. Groovy/Kotlin DSL: Uses Groovy or Kotlin for concise build scripts instead of XML.
2. Incremental Builds: Optimizes build times by executing only necessary tasks.
3. Dependency Management: Similar to Maven but more flexible.
4. Task-Based Execution: Defines tasks that can be executed independently.
5. Convention Over Configuration: Follows standard project structures but allows
customization.
6. Multi-Project Builds: Supports modular and multi-module project structures.
7. Plugin System: Extensible with various plugins for Java, Android, Spring Boot, etc.
8. Parallel Execution & Caching: Increases performance by running tasks in parallel.
9. IDE & CI/CD Integration: Works well with IntelliJ, Eclipse, Jenkins, and GitHub Actions.

Pros of Gradle
✔ Faster Builds: Uses incremental builds, caching, and parallel execution to optimize performance.
✔ Flexible Scripting: Supports Groovy and Kotlin DSL, making build scripts more readable and
powerful.
✔ Better Dependency Management: More flexible than Maven’s fixed lifecycle.
✔ Extensibility: Rich plugin ecosystem for various project types.
✔ Multi-Module Support: Ideal for enterprise projects with multiple subprojects.
✔ Improved Performance: Significantly faster than Maven for large projects.

Cons of Gradle
❌ Steep Learning Curve: Requires familiarity with Groovy/Kotlin for scripting.
❌ Less Standardized than Maven: Offers flexibility but lacks Maven’s structured approach.
❌ Heavy for Small Projects: More complex than necessary for simple applications.
❌ IDE Support Issues: Some IDEs may have inconsistent Gradle support compared to Maven.
❌ Plugin Compatibility: Not all plugins are as mature as Maven’s plugin ecosystem.

Conclusion
Gradle is a modern and efficient build tool, offering better performance and flexibility than Maven. It
is widely used for Android development and large Java projects, but beginners may find it harder to
learn compared to Maven.
Maven Repository
A Maven repository is a storage location for project dependencies, plugins, and artifacts. It helps
manage libraries required for building and running Java applications.

Types of Maven Repositories


1. Local Repository
 Stored on the developer’s machine (~/.m2/repository by default).
 Automatically created when Maven is installed.
 Caches downloaded dependencies to avoid re-downloading.
 If a dependency is not found, Maven checks the central or remote repository.
🔹 Pros:
✔ Faster access since dependencies are stored locally.
✔ Reduces network usage by avoiding repeated downloads.
🔹 Cons:
❌ Needs to be updated manually if dependencies change in remote repositories.
❌ Can cause version conflicts if multiple projects use different dependency versions.

2. Central Repository
 Maintained by Maven at https://repo.maven.apache.org/maven2/.
 Contains a vast collection of open-source libraries.
 Maven downloads dependencies from here if they are not in the local repository.
🔹 Pros:
✔ Official and trusted source of dependencies.
✔ Always available and updated with the latest releases.
🔹 Cons:
❌ Requires an internet connection to download dependencies.
❌ Slower than local repositories due to network dependency.

3. Remote Repository
 Hosted on external servers (private or public) by organizations or teams.
 Used to store custom or private artifacts not available in the Central Repository.
 Configured in settings.xml or pom.xml.
🔹 Pros:
✔ Allows sharing custom dependencies within a team or organization.
✔ Provides controlled access to specific dependencies.
🔹 Cons:
❌ Requires additional setup and maintenance.
❌ Might need authentication for restricted access.

Conclusion
Maven repositories work together to manage dependencies efficiently. The local repository provides
quick access, the central repository serves as the main public storage, and remote repositories
enable custom dependency sharing in teams.

Maven Dependency Search Sequence


When Maven resolves dependencies for a project, it follows a specific search order to locate and
download required artifacts.

1. Local Repository (First Priority)


 Maven first checks the local repository (~/.m2/repository by default).
 If the dependency is found, it is used immediately.
 If not found, Maven moves to the next step.
🔹 Reason: Avoids unnecessary downloads and speeds up builds.

2. Remote Repositories (If Not Found Locally)


 Maven checks remote repositories configured in pom.xml or settings.xml.
 These can be private or organizational repositories (e.g., Nexus, Artifactory).
 If the dependency is found, it is downloaded and stored in the local repository.
🔹 Reason: Used for internal dependencies or custom artifacts not available in the central repository.

3. Central Repository (Last Resort)


 If the dependency is not found in local or remote repositories, Maven queries the Maven
Central Repository (https://repo.maven.apache.org/maven2/).
 Once downloaded, the dependency is stored in the local repository for future use.
🔹 Reason: Acts as the global default repository for open-source libraries.

4. Fails with an Error (If Dependency is Not Found)


 If the dependency is not found in any repository, Maven throws an error:
 Could not resolve dependencies for project
 You need to manually check the repository URLs or add missing dependencies.
🔹 Solution: Verify pom.xml, check repository settings, or manually add missing dependencies.

Summary of Maven Dependency Resolution Order:


1️Local Repository (~/.m2/repository) →
2️Remote Repositories (if configured) →
3️Maven Central Repository (repo.maven.apache.org) →
4️Build Failure if Not Found
🔹 Optimization Tip: Use mvn dependency:resolve to check dependencies before building.

Maven Dependency Management


Maven dependency management is the process of handling external libraries required by a project in
an automated and structured way. Dependencies are declared in the pom.xml file, and Maven manages
their downloading, versioning, and resolution.

Key Features of Maven Dependency Management


1. Centralized Dependency Declaration
 Dependencies are listed in pom.xml under the <dependencies> tag.
 Example:
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-core</artifactId>
 <version>5.3.9</version>
 </dependency>
 This eliminates manual downloading and adding JAR files to the class path.

2. Transitive Dependency Resolution


 Maven automatically resolves dependencies of dependencies.
 Example: If Project A depends on Library B, and Library B depends on Library C, Maven
fetches both B and C automatically.
 This reduces the need to manually track dependency chains.
🔹 Issue: Sometimes conflicting versions arise (dependency hell).
3. Dependency Scope Control
 Defines when a dependency is available (compile-time, runtime, test, etc.).
 Common scopes:
o compile (default) – Available throughout (compile, runtime, testing).

o provided – Available at compile-time but not included in the final package.

o runtime – Needed at runtime but not during compilation.

o test – Only available for testing.

o system – Provided explicitly using a local path.

o import – Used for BOM (Bill of Materials).

Example:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>

4. Dependency Exclusions
 Prevents unwanted transitive dependencies.
 Example: Excluding commons-logging from spring-core:
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-core</artifactId>
 <version>5.3.9</version>
 <exclusions>
 <exclusion>
 <groupId>commons-logging</groupId>
 <artifactId>commons-logging</artifactId>
 </exclusion>
 </exclusions>
 </dependency>
5. Dependency Version Management using BOM (Bill of Materials)
 Used to manage multiple dependencies with consistent versions.
 Provided via <dependencyManagement>, typically used in parent POMs.
Example:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>5.3.9</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>

6. Dependency Resolution Order


1️⃣ Local Repository (~/.m2/repository)
2️⃣ Remote Repositories (if configured in pom.xml or settings.xml)
3️⃣ Maven Central Repository (https://repo.maven.apache.org/maven2/)
4️⃣ Fails if dependency is not found

Pros of Maven Dependency Management


✔ Automated downloading & versioning – No need to manually manage JARs.
✔ Transitive dependencies reduce configuration effort – Fetches required sub-dependencies.
✔ Centralized version control with BOM – Avoids version mismatches in multi-module projects.
✔ Scopes optimize build size – Reduces unnecessary dependencies in final builds.

Cons of Maven Dependency Management


❌ Version conflicts (Dependency Hell) – Different dependencies may require different versions of
the same library.
❌ Performance overhead – Fetching dependencies from remote repositories can slow down builds.
❌ Complex pom.xml – Large projects may have lengthy and difficult-to-maintain configurations.
Conclusion
Maven’s dependency management system simplifies handling libraries, reduces manual work, and
ensures consistency across projects. However, proper version control and exclusions are necessary to
avoid conflicts and unnecessary dependencies.

Maven Dependency Identification


In Maven, dependencies are uniquely identified using four key attributes in the pom.xml file:

1. Components of Dependency Identification


Each dependency is identified by the following attributes:

Attribute Description Example

Unique identifier for the organization or project that


groupId org.springframework.boot
provides the dependency.

artifactId Name of the specific library or module. spring-boot-starter-web

version Defines the version of the library to be used. 3.1.2

scope Defines the lifecycle phase where the dependency is


compile, test, provided, etc.
(optional) required.

2. Example of Dependency Identification in pom.xml


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>3.1.2</version>
<scope>compile</scope>
</dependency>
🔹 Explanation:
 org.springframework.boot → Organization that provides the dependency.
 spring-boot-starter-web → The specific library/module name.
 3.1.2 → The required version of the library.
 compile → The dependency is needed at both compile-time and runtime.

3. How Maven Identifies and Resolves Dependencies


Maven searches for the dependency in the following order: 1️Local Repository (~/.m2/repository) –
First check in the local machine.
2️Remote Repositories (if configured) – Custom repositories like Nexus/Artifactory.
3️Maven Central Repository (repo.maven.apache.org) – Official repository for open-source
libraries.
4️Dependency Not Found → Build Failure
🔹 Use mvn dependency:tree to view resolved dependencies.

4. Handling Conflicts in Dependency Identification


When multiple versions of a dependency are found (dependency conflict), Maven follows:
✔ Nearest Dependency Rule – The dependency closest to the project takes priority.
✔ Explicit Version Override – Specify the required version in pom.xml.
Example of forcing a specific version:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.0.1-jre</version>
</dependency>
</dependencies>
</dependencyManagement>

Conclusion
✔ Dependencies in Maven are uniquely identified using groupId, artifactId, and version.
✔ Maven follows a strict resolution order to locate dependencies.
✔ Conflicts can be resolved using explicit versioning or dependency exclusions.

Maven Dependency Scope


In Maven, the scope of a dependency determines when and how it is available in the project. The
scope is defined inside the <dependency> tag in pom.xml.

1. Types of Dependency Scopes

Availability in
Scope Description Example Use Case
Lifecycle

compile Available at compile, test, and ✅ Compile, ✅ Libraries used in both


(default) runtime. Runtime, ✅ Test compilation and runtime (e.g.,
Availability in
Scope Description Example Use Case
Lifecycle

Spring Boot, Hibernate).

Servlet APIs in web apps,


Similar to compile, but the JDK or ✅ Compile, ❌
provided where the server provides
container provides it. Runtime, ✅ Test
them.

Not required for compilation but ❌ Compile, ✅ JDBC drivers, logging


runtime
needed at runtime. Runtime, ✅ Test frameworks.

❌ Compile, ❌
test Available only in the test phase. JUnit, Mockito, TestNG.
Runtime, ✅ Test

Like provided, but requires a local ✅ Compile, ❌ Custom JARs not in


system
JAR file. Runtime, ✅ Test repositories.

Used in <dependencyManagement> Managing versions from an


import Depends on BOM
to import BOM files. external BOM file.

Transitive Dependency in Maven


1. What is a Transitive Dependency?
A transitive dependency is an indirect dependency that a project inherits because of the
dependencies declared in its pom.xml. If a dependency (A) depends on another dependency (B), then
(B) is a transitive dependency for your project.
🔹 Example:
If your project depends on Spring Boot Starter Web, which in turn depends on Spring Core, then
Spring Core becomes a transitive dependency of your project.

2. How Maven Resolves Transitive Dependencies?


 Maven automatically includes all transitive dependencies, reducing the need for manual
declarations.
 Maven follows the dependency tree to fetch the required dependencies.
 The dependency resolution process follows depth-first search.

3. Example of Transitive Dependency


Direct Dependency (Declared in pom.xml)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>3.1.2</version>
</dependency>
📌 spring-boot-starter-web internally depends on:
 spring-web
 spring-core
 spring-beans
 spring-context
These dependencies are automatically included in the project.

4. Viewing Transitive Dependencies


To check transitive dependencies, use:
mvn dependency:tree
📌 This command will display all dependencies and their hierarchy.

5. Excluding a Transitive Dependency


Sometimes, unwanted transitive dependencies may cause conflicts. You can exclude them like this:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>3.1.2</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
</exclusions>
</dependency>
📌 This removes spring-core from transitive dependencies.

6. Managing Conflicting Transitive Dependencies


If two dependencies bring different versions of the same library, Maven uses the nearest dependency
rule:
 The version closest to the project in the dependency tree is selected.
 If necessary, you can force a specific version using dependencyManagement.
✅ Example: Forcing a Specific Version
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.9</version>
</dependency>
</dependencies>
</dependencyManagement>
📌 This ensures all dependencies use Spring Core 5.3.9, avoiding version mismatches.

7. Key Takeaways
✔ Maven automatically resolves transitive dependencies.
✔ Use mvn dependency:tree to analyze dependencies.
✔ Use exclusions to remove unwanted transitive dependencies.
✔ Use dependencyManagement to enforce specific versions.

Features of Transitive Dependency in Maven


1. Automatic Dependency Resolution
o Maven automatically resolves and includes transitive dependencies, reducing manual
effort in managing dependencies.
2. Dependency Tree Structure
o Maven follows a hierarchical tree structure where dependencies bring in their own
dependencies (transitive dependencies).
o Use mvn dependency:tree to visualize the dependency tree.

3. Depth-First Search (DFS) Resolution


o Maven resolves dependencies using a depth-first search (DFS) approach, ensuring
all necessary dependencies are fetched.
4. Nearest Dependency Rule
o If multiple dependencies declare different versions of the same library, Maven selects
the one closest to the project in the dependency tree.
5. Conflict Resolution
o When different versions of the same dependency are included, Maven applies the
nearest dependency rule but also allows explicit version enforcement via
dependencyManagement.
6. Exclusion Mechanism
o Unwanted transitive dependencies can be removed using the <exclusions> tag in
pom.xml.
7. Version Control with Dependency Management
o The <dependencyManagement> section allows you to specify the versions of
transitive dependencies, ensuring uniformity across the project.
8. Scope Inheritance
o Transitive dependencies inherit the scope of their parent dependency unless
explicitly overridden.
9. Performance Optimization
o By managing dependencies efficiently, Maven avoids redundant downloads and
reuses cached dependencies from the local repository.
10. Remote Repository Resolution
 If a transitive dependency is missing in the local repository, Maven automatically downloads
it from the central or configured remote repositories.

Example: Transitive Dependency in Action


Direct Dependency in pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>3.1.2</version>
</dependency>
📌 Spring Boot Starter Web internally depends on:
 spring-web
 spring-core
 spring-context
These dependencies are automatically included in the project.
Key Takeaways
✔ Transitive dependencies reduce manual dependency management.
✔ Maven follows a dependency tree structure using DFS.
✔ Conflict resolution is handled using the nearest dependency rule.
✔ Unwanted dependencies can be excluded using <exclusions>.
✔ Use mvn dependency:tree to check dependency resolution.

You might also like