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)
- Download and install the latest JDK version compatible with your operating system.
- Set the JAVA_HOME environment variable to the JDK installation directory.
Step 2: Install Apache Maven
- Download the latest Apache Maven binary distribution from the official website (https://maven.apache.org/download.cgi).
- Extract the downloaded archive to a directory on your system.
- Set the MAVEN_HOME environment variable to the Maven installation directory.
- Add the Maven bin directory to the system's PATH variable.
Step 3: Verify Maven Installation
- Open a terminal or command prompt.
- Type mvn -version and press Enter.
- Verify that Maven version information is displayed, confirming the successful installation.
Step 4: Create a Simple Java Project
- Open a text editor or an IDE.
- Create a new directory for your Java project, and navigate to that directory using the command-line.
- Inside the project directory, create a new Java source file with a simple Java class (e.g., HelloWorld.java).
Step 5: Write Java Code
- Open the HelloWorld.java file in your text editor or IDE.
- 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
- In the project directory, create a new directory named src.
- Inside the src directory, create two subdirectories: main and test.
- Inside the main directory, create a subdirectory java.
Step 7: Move Java Source File
- Move the HelloWorld.java file to the src/main/java directory.
Step 8: Create a Maven Project Object Model (POM)
- In the project directory, create a new file named pom.xml.
- 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
- Open a terminal or command prompt.
- Navigate to the project directory.
- 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
- Navigate to the target directory.
- 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
Post a Comment