Skip to main content

Experiment No. 8 Title: Demonstrate Maven Build Life Cycle

 Experiment No. 8

Title: Demonstrate Maven Build Life Cycle


Objective:

The objective of this experiment is to gain hands-on experience with the Maven build lifecycle by creating a simple Java project and executing various Maven build phases.


Introduction:

Maven is a widely-used build automation and project management tool in the Java ecosystem. It provides a clear and standardised build lifecycle for Java projects, allowing developers to perform various tasks such as compiling code, running tests, packaging applications, and deploying artefacts. This experiment aims to demonstrate the Maven build lifecycle and its different phases.


Key Maven Concepts:


  • Project Object Model (POM): The POM is an XML file named pom.xml that defines a project's configuration, dependencies, plugins, and goals. It serves as the project's blueprint and is at the core of Maven's functionality.

  • Build Lifecycle: Maven follows a predefined sequence of phases and goals organized into build lifecycles. These lifecycles include clean, validate, compile, test, package, install, and deploy, among others.

  • Plugin: Plugins are extensions that provide specific functionality to Maven. They enable tasks like compiling code, running tests, packaging artifacts, and deploying applications.

  • Dependency Management: Maven simplifies dependency management by allowing developers to declare project dependencies in the POM file. Maven downloads these dependencies from repositories like Maven Central.

  • Repository: A repository is a collection of artifacts (compiled libraries, JARs, etc.) that Maven uses to manage dependencies. Maven Central is a popular public repository, and organisations often maintain private repositories.


Maven Build Life Cycle:

The Maven build process is organised into a set of build lifecycles, each comprising a sequence of phases. Here are the key build lifecycles and their associated phases:


Clean Lifecycle:

  • clean: Deletes the target directory, removing all build artifacts.


Default Lifecycle:

  • validate: Validates the project's structure.

  • compile: Compiles the project's source code.

  • test: Runs tests using a suitable testing framework.

  • package: Packages the compiled code into a distributable format (e.g., JAR, WAR).

  • verify: Runs checks on the package to verify its correctness.

  • install: Installs the package to the local repository.

  • deploy: Copies the final package to a remote repository for sharing.


Site Lifecycle:

  • site: Generates project documentation.


Each phase within a lifecycle is executed in sequence, and the build progresses from one phase to the next. Developers can customise build behaviour by configuring plugins and goals in the POM file.




Materials:

  • A computer with Maven installed (https://maven.apache.org/download.cgi)

  • A code editor (e.g., Visual Studio Code, IntelliJ IDEA)

  • Java Development Kit (JDK) installed (https://www.oracle.com/java/technologies/javase-downloads.html)


Experiment Steps:


Step 1: Setup Maven and Java


  • Ensure that you have Maven and JDK installed on your system. You can verify their installations by running the following commands:


mvn -v

java -version


Step 2: Create a Maven Java Project

  • Create a new directory for your project, e.g., MavenDemo.

  • Inside the project directory, create a simple Java class, e.g., HelloWorld.java, with a main method that prints "Hello, Maven!".


public class HelloWorld {

    public static void main(String[] args) {

        System.out.println("Hello, Maven!");

    }

}


Create a pom.xml file (Maven Project Object Model) in the project directory. This file defines project metadata, dependencies, and build configurations. Here's a minimal example:


<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>com.example</groupId>

    <artifactId>MavenDemo</artifactId>

    <version>1.0-SNAPSHOT</version>

</project>


Step 3: Explore the Maven Build Phases

  • Maven has several build phases, and each phase is responsible for specific tasks. In this step, we'll explore some of the most commonly used build phases.


  • Clean Phase: To clean the project (remove generated files), run:

mvn clean


  • Compile Phase: To compile the Java source code, run:

mvn compile


  • Test Phase: To execute unit tests, run:

mvn test


  • Package Phase: To package the application into a JAR file, run:

mvn package


  • Install Phase: To install the project artifacts (e.g., JAR) into your local Maven repository, run:

mvn install


  • Deploy Phase: To deploy artifacts to a remote Maven repository, configure your pom.xml and run:

mvn deploy


Step 4: Run the Application

  • After running the mvn package command, you can find the generated JAR file (e.g., MavenDemo-1.0-SNAPSHOT.jar) in the target directory. Run the application using:


java -cp target/MavenDemo-1.0-SNAPSHOT.jar HelloWorld


Conclusion:

This experiment demonstrates the Maven build lifecycle by creating a simple Java project and executing various Maven build phases. Maven simplifies the build process by providing a standardized way to manage dependencies, compile code, run tests, and package applications. Understanding these build phases is essential for Java developers using Maven in their projects.


Exercise/Questions:

  1. What is Maven, and why is it commonly used in software development?

  2. Explain the purpose of the pom.xml file in a Maven project.

  3. How does Maven simplify dependency management in software projects?

  4. What are Maven plugins, and how do they enhance the functionality of Maven?

  5. List the key phases in the Maven build lifecycle, and briefly describe what each phase does.

  6. What is the primary function of the clean phase in the Maven build lifecycle?

  7. In Maven, what does the compile phase do, and when is it typically executed?

  8. How does Maven differentiate between the test and verify phases in the build lifecycle?

  9. What is the role of the install phase in the Maven build lifecycle, and why is it useful?

  10. Explain the difference between a local repository and a remote repository in the context of Maven.

Comments

Popular posts from this blog

Experiment No. 5 Title: Applying CI/CD Principles to Web Development Using Jenkins, Git, and Local HTTP Server

  Experiment No. 5 Title: Applying CI/CD Principles to Web Development Using Jenkins, Git, and Local HTTP Server  Objective: The objective of this experiment is to set up a CI/CD pipeline for a web development project using Jenkins, Git, and webhooks, without the need for a Jenkinsfile. You will learn how to automatically build and deploy a web application to a local HTTP server whenever changes are pushed to the Git repository, using Jenkins' "Execute Shell" build step. Introduction: Continuous Integration and Continuous Deployment (CI/CD) is a critical practice in modern software development, allowing teams to automate the building, testing, and deployment of applications. This process ensures that software updates are consistently and reliably delivered to end-users, leading to improved development efficiency and product quality. In this context, this introduction sets the stage for an exploration of how to apply CI/CD principles specifically to web development using J

Experiment No. 10 Title: Create the GitHub Account to demonstrate CI/CD pipeline using Cloud Platform.

  Experiment No. 10 Title: Create the GitHub Account to demonstrate CI/CD pipeline using Cloud Platform. Objective: The objective of this experiment is to help you create a GitHub account and set up a basic CI/CD pipeline on GCP. You will learn how to connect your GitHub repository to GCP, configure CI/CD using Cloud Build, and automatically deploy web pages to an Apache web server when code is pushed to your repository. Introduction: Continuous Integration and Continuous Deployment (CI/CD) pipelines are essential for automating the deployment of web applications. In this experiment, we will guide you through creating a GitHub account and setting up a basic CI/CD pipeline using Google Cloud Platform (GCP) to copy web pages for an Apache HTTP web application. Continuous Integration and Continuous Deployment (CI/CD) is a crucial practice in modern software development. It involves automating the processes of code integration, testing, and deployment to ensure that software changes are co

Experiment No. 6 Title: Exploring Containerization and Application Deployment with Docker

  Experiment No. 6 Title: Exploring Containerization and Application Deployment with Docker  Objective: The objective of this experiment is to provide hands-on experience with Docker containerization and application deployment by deploying an Apache web server in a Docker container. By the end of this experiment, you will understand the basics of Docker, how to create Docker containers, and how to deploy a simple web server application. Introduction Containerization is a technology that has revolutionised the way applications are developed, deployed, and managed in the modern IT landscape. It provides a standardised and efficient way to package, distribute, and run software applications and their dependencies in isolated environments called containers. Containerization technology has gained immense popularity, with Docker being one of the most well-known containerization platforms. This introduction explores the fundamental concepts of containerization, its benefits, and how it differs