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

Maven Create and Build Artifacts

In Maven, you can create and build artifacts using the package phase of the build lifecycle. The package phase is responsible for taking the compiled code and other project resources and packaging them into a distributable format, such as a JAR (Java Archive), WAR (Web Application Archive), or other custom formats. Here are the steps to create and build artifacts using Maven: Configure the Build Output: In your project's pom.xml file, you need to configure the output of the build. This includes specifying the type of artifact you want to create (e.g., JAR, WAR) and any additional resources to include. You do this in the <build> section of your pom.xml: <build>     <finalName>my-artifact</finalName> <!-- Name of the artifact without the extension -->     <plugins>         <!-- Plugin configurations for creating the artifact -->         <!-- For example, maven-jar-plugin or maven-war-plugin -->     </plugins> </build> Depend

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

Maven Repositories (local, central, global)

Maven relies on repositories to manage dependencies, plugins, and other artifacts required for a project. There are typically three types of repositories in Maven: local, central, and remote/global repositories. Local Repository: Location: The local repository is located on your local development machine. By default, it's in the .m2 directory within your user home directory (e.g., C:\Users\<username>\.m2\repository on Windows or /Users/<username>/.m2/repository on macOS and Linux). Purpose: The local repository is used to store artifacts (JARs, POMs, and other files) that your machine has downloaded or built during previous Maven builds. These artifacts are specific to your local development environment. Benefits: Using a local repository improves build performance since it caches dependencies locally, reducing the need to download them repeatedly. It also ensures reproducibility by maintaining a local copy of dependencies. Central Repository: Location: The central repo