S01L04 – Maven jar plugin example

Using Maven Jar Plugin for Java Projects

Introduction

Maven is an essential tool for Java developers as it simplifies project building and dependency management. In this article, we will discuss how to use the Maven jar plugin to package a Java project into a JAR file. Understanding Maven and its plugins is crucial for developers working on complex projects, as it automates many repetitive tasks and ensures consistent builds.

Why Use Maven Jar Plugin?

  • Pros:
    • Automates the process of building and packaging Java applications.
    • Manages dependencies, making it easier to maintain and upgrade libraries.
    • Ensures that the project structure and build lifecycle are consistent across different environments.

When to Use?

  • For any medium to large-sized Java project where dependency management and automation are required.
  • When you need to package the Java application into a deployable JAR file.

Setting Up the Maven Project

1. Creating the Maven Project Structure

Maven projects have a predefined structure. Here’s a basic setup:

2. Writing the HelloWorld.java File

The HelloWorld.java file contains the following code:

Explanation:

  • The package statement defines the package name org.studyeasy.
  • The HelloWorld class contains the main method which prints “Hello, World from Maven!” to the console.

3. Creating the pom.xml File

The pom.xml file is the core of a Maven project. It manages the project’s dependencies and build configuration.

Key Sections:

  • <groupId> and <artifactId>: Define the unique identity of the project.
  • <build>: Configures the build process.
  • <plugins>: Specifies the Maven jar plugin and its configuration, including the main class for the manifest file.

Building the Project Using Maven

1. Compiling the Java Source Code

To compile the project, navigate to the project directory and run:

Explanation:

  • mvn compile command compiles the Java source files in the src/main/java directory.

2. Creating the JAR File

To create a JAR file, execute the following command:

Explanation:

  • The mvn package command packages the compiled code into a JAR file as specified in the pom.xml file.
  • The resulting JAR file, HelloWorldMaven-1.0-SNAPSHOT.jar, is located in the target directory.

Running the Maven-Built JAR File

1. Executing the JAR File

Run the JAR file using the following command:

The output will be:

2. Understanding the Manifest File

The JAR file contains a META-INF/MANIFEST.MF file, which includes:

  • Main-Class: Specifies the entry point of the application.

3. Adding External Dependencies

If your project requires external libraries, you can add them in the <dependencies> section of the pom.xml file:

After adding the dependencies, run mvn clean install to download and include them in your project.

Conclusion

In this article, we’ve explored how to use the Maven jar plugin to package a simple Java project. Understanding Maven is essential for Java developers, as it simplifies project management and build processes. By using Maven, developers can easily manage dependencies and automate complex build configurations, making the development process smoother and more efficient.