Maven

Maven is a widely-used build automation tool for Java projects. It simplifies the process of building and managing Java applications by providing a standard project structure, dependency management, and build lifecycle.

Maven uses a project object model (POM) file, typically named pom.xml, to define project configuration and dependencies. It downloads dependencies from remote repositories, compiles source code, and packages the project into a distributable format.

Maven Installation

To install Maven, follow these steps:

  1. Download Maven: Visit the Apache Maven download page (https://maven.apache.org/download.cgi) and download the latest binary distribution for your operating system.

  2. Extract Maven:

    • On Windows: Extract the downloaded archive to a directory (e.g., C:\Program Files\apache-maven-x.y.z).

    • On Linux or macOS: Use tar to extract the archive to a directory (e.g., /opt/apache-maven-x.y.z).

  3. Set Environment Variables:

    • On Windows: Add the M2_HOME and MAVEN_HOME environment variables, and add %M2_HOME%\bin to your PATH.

    • On Linux or macOS: Add the following lines to your shell profile file (~/.bashrc, ~/.bash_profile, or ~/.zshrc):

    export M2_HOME=/path/to/apache-maven-x.y.z
    export PATH=$M2_HOME/bin:$PATH
  4. Verify Installation: Open a new terminal window and run mvn -version to verify that Maven is installed correctly.

Maven Project Structure

A typical Maven project has a well-defined directory structure, which makes it easy to manage and build. Here's a simplified project structure:

my-maven-project/
├── src/
│   ├── main/
│   │   ├── java/          # Java source code
│   │   └── resources/     # Resources like configuration files
│   └── test/
│       ├── java/          # Test source code
│       └── resources/     # Test resources
├── target/                # Compiled classes and built artifacts
└── pom.xml                # Project configuration file

Creating a Simple Maven Project

Let's create a simple Maven project:

  1. Open a Terminal/Command Prompt.

  2. Navigate to the Directory Where You Want to Create the Project.

  3. Create a New Maven Project: Run the following command to create a new Maven project:

    mvn archetype:generate -DgroupId=com.example -DartifactId=my-maven-project -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

    Replace com.example and my-maven-project with your desired group ID and project name.

  4. Navigate to the Project Directory:

    cd my-maven-project
  5. View the Project Structure: You'll see the project structure similar to what was described earlier.

Adding Dependencies

Maven simplifies dependency management. To add dependencies to your project, edit the pom.xml file and add the following dependency block inside the <dependencies> element:

<dependency>
    <groupId>group-id</groupId>
    <artifactId>artifact-id</artifactId>
    <version>version</version>
</dependency>

For example, to add the Apache Commons Lang library, your pom.xml would include:

<dependencies>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.12.0</version>
    </dependency>
</dependencies>

After editing the pom.xml, run mvn clean install to download the dependencies.

Building and Packaging a Maven Project

To build and package your Maven project, use the following Maven goals:

  • Clean: Removes the target directory.

    mvn clean
  • Compile: Compiles the source code.

    mvn compile
  • Test: Runs tests.

    mvn test
  • Package: Packages the project (e.g., creates a JAR or WAR file).

    mvn package
  • Install: Installs the project artifact to your local repository.

    mvn install

These are some of the most common Maven goals. You can execute these commands in your project's root directory.

Maven's capabilities extend far beyond what's covered here, but this guide should give you a solid foundation for managing and building your Java projects with Maven.

Last updated