Maven#

Maven is a build automation tool for Java projects. It handles the tedious parts of building software so developers don’t have to. Specifically, Maven manages three things that would otherwise be painful:

  • Dependencies β€” Your project needs external libraries (e.g., a library for reading JSON, or connecting to a database). Instead of hunting down .jar files and manually adding them to your project, you tell Maven what you need. It downloads the correct versions automatically from the internet and makes them available to your code.

  • Build lifecycle β€” Maven knows the standard steps to go from source code to a finished, runnable program: compile the code, run the tests, package everything into a .jar file. You run one command and Maven executes all of those steps in the right order.

  • Consistency β€” Every developer on the team builds the project the exact same way, on any machine. β€œIt works on my computer” becomes much less of a problem.

Maven is configured through a single file at the root of the project called pom.xml (Project Object Model). This file describes the project’s name, version, dependencies, and how it should be built. When a new developer joins a project, they clone the repo and Maven takes care of the rest.

The big idea

Maven answers the question: β€œHow do we turn source code into working software, reliably, every time?”

You can think of Maven like a recipe card for your project. The pom.xml is the recipe; it lists the ingredients (dependencies) and the steps (build lifecycle). Maven is the chef that follows it.

Pros:

  • Dependency Management

  • Industry Standard

  • Well Designed Structure

Cons:

  • Ramp-up

  • Structure can be overkill

  • Opaque mechanisms

    • If the build breaks, fixing it is tough!

POM.xml#

The pom.xml (Project Object Model) file is the core of a Maven project. It contains information about the project and configuration details used by Maven to build the project. Here’s a basic example of a pom.xml file and an explanation of its components:

POM == Project Object Model

<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.0</version>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
            <version>1.1.1</version>
        </dependency>
    </dependencies>
</project>

Project File Structure#

Maven follows a mandatory and specific directory structure to maintain consistency and convention across projects. Here’s an overview of the Maven project structure :

my-app
β”‚
β”œβ”€β”€ src
β”‚   β”œβ”€β”€ main
β”‚   β”‚   β”œβ”€β”€ java
β”‚   β”‚   β”‚   └── com
β”‚   β”‚   β”‚       └── example
β”‚   β”‚   β”‚           └── App.java
β”‚   β”‚   β”œβ”€β”€ resources
β”‚   β”‚   β”‚   └── application.properties
β”‚   β”œβ”€β”€ test
β”‚   β”‚   β”œβ”€β”€ java
β”‚   β”‚   β”‚   └── com
β”‚   β”‚   β”‚       └── example
β”‚   β”‚   β”‚           └── AppTest.java
β”‚   β”‚   β”œβ”€β”€ resources
β”‚   β”‚   β”‚   └── test-config.properties
β”‚
β”œβ”€β”€ pom.xml

Here is an explanation of the above directory structure:
src/main/java:
This directory contains your main application source code. The package structure should follow the standard Java package naming conventions (e.g., com.example).
src/main/resources:
This directory is for non-Java resources required by your application, such as configuration files, property files, XML files, etc. These resources are included in the classpath when the project is built. src/test/java:
This directory contains your test source code. It follows the same package structure as your main source code. Test classes are typically named with a Test suffix (e.g., AppTest.java).
src/test/resources:
This directory is for resources needed by your tests, such as test configuration files, mock data, etc. These resources are also included in the classpath during the test phase.
pom.xml:
The Project Object Model file that contains information about the project and configuration details used by Maven to build the project. It includes dependencies, plugins, build configurations, and more.

In the CSS 490: Multithreading in GUI Application class, we have the following directory structure.

β”œβ”€β”€β”€.mvn
β”‚   └───wrapper
β”œβ”€β”€β”€.vscode
β”œβ”€β”€β”€lib
β”œβ”€β”€β”€logs
β”œβ”€β”€β”€src
β”‚   β”œβ”€β”€β”€main
β”‚   β”‚   β”œβ”€β”€β”€java
β”‚   β”‚   β”‚   └───com
β”‚   β”‚   β”‚       └───mrstride
|   |   |           β”œβ”€β”€β”€entity
β”‚   β”‚   β”‚           β”œβ”€β”€β”€gui
β”‚   β”‚   β”‚           └───services
β”‚   β”‚   └───resources
β”‚   β”‚       β”œβ”€β”€β”€images
β”‚   β”‚       β”œβ”€β”€β”€levels
β”‚   β”‚       └───playbacks
β”‚   └───test
β”‚       └───java
β”‚           └───com
β”‚               └───mrstride
|                   β”œβ”€β”€β”€entity
β”‚                   β”œβ”€β”€β”€gui
β”‚                   └───services
└───target

What’s so important? Billy#

  • Maven automates the entire build process, handling compilation, testing, packaging, and dependency management so we don’t waste time manually managing jar files or build steps.

  • The pom.xml is the single source of truth for a project’s configuration, letting any developer clone the repo and build instantly with consistent results.

  • Dependency management becomes effortless. Maven automatically downloads and version‑controls libraries so projects stay organized and up‑to‑date.

  • Standard directory structure means fewer surprises and makes it easier to navigate, share, and understand Java projects across courses and teams.