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

DevOps : Lab Manual (Complete)

  Lab Manual  Subject: DevOps Prepared By : Antosh Mahadappa Dyade  INDEX List of Practical Sr. No. Experiment Title Page No. 1 Exploring Git Commands through Collaborative Coding. 2 Implement GitHub Operations 3 Implement GitLab Operations 4 Implement BitBucket Operations 5 Applying CI/CD Principles to Web Development Using Jenkins, Git, and Local HTTP Server 6 Exploring Containerization and Application Deployment with Docker 7 Applying CI/CD Principles to Web Development Using Jenkins, Git, using Docker Containers 8 Demonstrate Maven Build Life Cycle 9 Demonstrate Container Orchestration using Kubernets. 10 Create the GitHub Account to demonstrate CI/CD pipeline using Cloud Platform. 11 (Content Beyond Syllabus) Title: Demonstrating Infrastructure as Code (IaC) with Terraform                                   ...

Example of Maven project that interacts with a MySQL database and includes testing

Example Maven project that interacts with a MySQL database and includes testing To install Java, MySQL, Maven, and write a Java program to fetch table data, execute, and create a JAR file using Maven on Ubuntu, you can follow these steps: Step 1: Install Java You can install Java using the following commands: sudo apt update sudo apt install default-jre sudo apt install default-jdk Verify the installation by running: java -version Step 2: Install MySQL You can install MySQL using the following commands: sudo apt update sudo apt install mysql-server During the installation, you'll be prompted to set a root password for MySQL or you can set password at latter stage using following steps.  sudo mysql ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password'; exit Step 3: Install Maven You can install Maven using the following commands: sudo apt update sudo apt install maven Verify the installation by running: mvn -version Step 4: Create ...

Setting Up a Custom HTTPD Server on Kubernetes with Minikube on Ubuntu

In this article, we’ll walk through the steps to set up a Kubernetes cluster on Ubuntu using Minikube and deploy a custom Apache HTTP server (`httpd`) with your own `index.html` file. Whether you’re a beginner or just looking to refresh your skills, this guide will help you get started with Kubernetes and Docker. What You’ll Need - An Ubuntu system - Basic knowledge of the command line - Installed tools: `curl`, `apt-transport-https`, `virtualbox` (or another virtualization tool) Step 1: Prepare Your Environment First, let’s ensure your system is up-to-date and has the necessary packages. sudo apt update sudo apt upgrade -y sudo apt install -y curl apt-transport-https virtualbox Step 2: Install Minikube Minikube is a tool that makes it easy to run Kubernetes locally. Here’s how to install it: 1. Download the Minikube binary:    curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 2. Install Minikube:    sudo install minikube-linux-amd...