Skip to content
On this page

Introduction to Maven

Before understanding Maven, let's first look at what a Java project requires. Firstly, we need to determine which dependencies to include. For example, if we need to use commons logging, we must add the commons logging jar to the classpath. If we also need log4j, we must add all related log4j jars to the classpath. This is dependency management.

Secondly, we need to establish the project's directory structure. For example, the src directory stores Java source code, the resources directory stores configuration files, and the bin directory stores the compiled .class files.

Additionally, we need to configure the environment, such as the JDK version, the compilation and packaging process, and the current code version number.

Finally, besides using an IDE like Eclipse for compilation, we must also be able to compile through command-line tools to allow the project to be built, tested, and deployed on an independent server.

These tasks are not difficult but are very tedious and time-consuming. If each project manages its own set of configurations, it would inevitably become chaotic. What we need is a standardized Java project management and build tool.

Maven is a management and build tool specifically designed for Java projects. Its main functionalities include:

  • Providing a standardized project structure;
  • Offering a standardized build lifecycle (compilation, testing, packaging, deployment, etc.);
  • Providing a dependency management mechanism.

Maven Project Structure

A typical Java project managed by Maven has the following default directory structure:

a-maven-project
├── pom.xml
├── src
│   ├── main
│   │   ├── java
│   │   └── resources
│   └── test
│       ├── java
│       └── resources
└── target
  • Project Root Directory (a-maven-project): The project name.
  • pom.xml: The project descriptor file.
  • src/main/java: Directory for Java source code.
  • src/main/resources: Directory for resource files.
  • src/test/java: Directory for test source code.
  • src/test/resources: Directory for test resource files.
  • target: Directory for all compiled and packaged files.

These are the standard directory structures of a Maven project. All directory structures are predefined standards; do not modify the directory structure arbitrarily. Using the standard structure requires no additional configuration, and Maven can be used normally.

The pom.xml File

Let's take a closer look at the most crucial project descriptor file, pom.xml. Its content looks like the following:

xml
<project ...>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.itranswarp.learnjava</groupId>
    <artifactId>hello</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.release>17</maven.compiler.release>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>2.0.16</version>
        </dependency>
    </dependencies>
</project>
  • groupId: Similar to a Java package name, usually representing the company or organization name.
  • artifactId: Similar to a Java class name, usually representing the project name.
  • version: Combined with groupId and artifactId, these three elements uniquely identify a Maven project.

When referencing other third-party libraries, we also use these three elements to identify them. For example, to depend on org.slf4j:slf4j-simple:2.0.16:

xml
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>2.0.16</version>
</dependency>

By declaring a dependency using <dependency>, Maven will automatically download this dependency and add it to the classpath.

Additionally, notice that <properties> defines some properties. Commonly used properties include:

  • project.build.sourceEncoding: Indicates the character encoding of the project source code, usually set to UTF-8.
  • maven.compiler.release: Indicates the JDK version to use, for example, 21.
  • maven.compiler.source: Indicates the source code version read by the Java compiler.
  • maven.compiler.target: Indicates the class version compiled by the Java compiler.

Starting from Java 9, it is recommended to use the maven.compiler.release property to ensure that the input source code and the output version are consistent during compilation. If the source and output versions differ, you should set maven.compiler.source and maven.compiler.target separately.

By defining properties using <properties>, you can fix the JDK version, preventing different developers of the same project from using different JDK versions.

Installing Maven

To install Maven, download the latest Maven 3.9.x from the Maven official website, then extract it locally and set up a few environment variables:

  • M2_HOME: Set to the path of the extracted Maven directory, e.g., /path/to/maven-3.9.x.
  • PATH: Add $M2_HOME/bin to the existing PATH.

For Windows, you can add %M2_HOME%\bin to the system Path variable.

Then, open a command prompt and enter mvn -version to check Maven's version information:

┌────────────────────────────────────────────────────────┐
│Command Prompt                                    - □ x │
├────────────────────────────────────────────────────────┤
│Microsoft Windows [Version 10.0.0]                      │
│(c) 2015 Microsoft Corporation. All rights reserved.    │
│                                                        │
│C:\> mvn -version                                       │
│Apache Maven 3.9.x ...                                  │
│Maven home: C:\Users\liaoxuefeng\maven                  │
│Java version: ...                                       │
│...                                                     │
│                                                        │
│C:\> _                                                  │
│                                                        │
└────────────────────────────────────────────────────────┘

If the command is not found, it indicates that the system PATH is incorrect and needs to be fixed before running the command.

Summary

Maven is a management and build tool for Java projects:

  • Maven uses pom.xml to define project contents and follows a preset directory structure.
  • Declaring a dependency in Maven automatically downloads and adds it to the classpath.
  • Maven uniquely identifies a dependency using groupId, artifactId, and version.
Introduction to Maven has loaded