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

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