Skip to main content

Experiment to Setup Maven and Compiling & Building Using Maven

Title: Setting up Maven and Compiling & Building Using Maven

Objective:

The objective of this experiment is to demonstrate how to set up Maven and use it to compile and build a simple Java project.

Prerequisites:

  • Java Development Kit (JDK) installed on your system
  • Apache Maven installed on your system
  • A text editor or an Integrated Development Environment (IDE)

Experiment Steps:

Step 1: Install Java Development Kit (JDK)

  1. Download and install the latest JDK version compatible with your operating system.
  2. Set the JAVA_HOME environment variable to the JDK installation directory.

Step 2: Install Apache Maven

  1. Download the latest Apache Maven binary distribution from the official website (https://maven.apache.org/download.cgi).
  2. Extract the downloaded archive to a directory on your system.
  3. Set the MAVEN_HOME environment variable to the Maven installation directory.
  4. Add the Maven bin directory to the system's PATH variable.

Step 3: Verify Maven Installation

  1. Open a terminal or command prompt.
  2. Type mvn -version and press Enter.
  3. Verify that Maven version information is displayed, confirming the successful installation.

Step 4: Create a Simple Java Project

  1. Open a text editor or an IDE.
  2. Create a new directory for your Java project, and navigate to that directory using the command-line.
  3. Inside the project directory, create a new Java source file with a simple Java class (e.g., HelloWorld.java).

Step 5: Write Java Code

  1. Open the HelloWorld.java file in your text editor or IDE.
  2. Write a simple Java program, for example:


public class HelloWorld {

    public static void main(String[] args) {

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

    }

}


Step 6: Create a Maven Project Structure

  1. In the project directory, create a new directory named src.
  2. Inside the src directory, create two subdirectories: main and test.
  3. Inside the main directory, create a subdirectory java.

Step 7: Move Java Source File

  1. Move the HelloWorld.java file to the src/main/java directory.

Step 8: Create a Maven Project Object Model (POM)

  1. In the project directory, create a new file named pom.xml.
  2. Add the following minimal configuration to the pom.xml file:


<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>my-app</artifactId>

    <version>1.0-SNAPSHOT</version>

</project>



Step 9: Compile and Build Using Maven

  1. Open a terminal or command prompt.
  2. Navigate to the project directory.
  3. Run the following Maven command to compile the Java code:


mvn compile

  • Maven will compile the source code and create the compiled classes in the target/classes directory.
  • Run the following Maven command to package the compiled classes into a JAR file:


mvn package

  • Maven will create a JAR file in the target directory with the name <artifactId>-<version>.jar (e.g., my-app-1.0-SNAPSHOT.jar).

Step 10: Run the Java Application

  1. Navigate to the target directory.
  2. Run the Java application using the following command:

java -cp my-app-1.0-SNAPSHOT.jar com.example.HelloWorld

  • (Replace my-app-1.0-SNAPSHOT.jar with the actual name of the generated JAR file).

Conclusion:

In this experiment, we successfully set up Maven, created a simple Java project, and used Maven to compile and build the project. Maven simplifies the build process and provides a standardized way to manage Java projects, making it easier to manage dependencies, compile, and package applications. With Maven, you can efficiently manage large-scale Java projects and streamline the development process.

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. 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

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