A Comprehensive Guide to Setting Up Apache Maven

In this guide, you’ll learn everything you need to know about Apache Maven—from what it is to how to configure it across different operating systems.


What is Apache Maven?

In simple terms, Maven is a powerful build and project management tool primarily used by Java developers. Compared to older build systems like Apache Ant, Maven provides a standardized way to manage builds, dependencies, and project lifecycles.

Key Features of Maven:

  • Simplifies the build process.
  • Ensures consistency with a uniform build system.
  • Manages project dependencies through a central or local repository.
  • Provides quality project information.
  • Follows development best practices.
  • Allows for smooth migration to new features.

Now, let’s dive into how to install and configure Maven on your system.


Step 1: Downloading Apache Maven

  1. Visit the official Apache Maven Download page.
  2. Download the binary archive matching your operating system:
    • .zip for Windows
    • .tar.gz for Linux/macOS
  3. Extract the archived files to a directory of your choice:
    • On Windows, you might extract to: C:\Apache\apache-maven-<version>
    • On Linux/macOS, extract it to: /opt/apache-maven-<version>

Step 2: Configuring Environment Variables

After downloading Maven, the next step is to configure your environment so that Maven can be accessed from your terminal or command prompt.

On Windows:

  1. Open System Properties:
    • Right-click on “This PC” → select Properties → click Advanced System Settings → select Environment Variables.
  2. Add the following system variables:
    • M2_HOME: Set this to the Maven installation directory (e.g., C:\Apache\apache-maven-<version>).
    • JAVA_HOME: Set this to your JDK installation directory (e.g., C:\Program Files\Java\jdk-<version>).
  3. Add M2_HOME\bin to your PATH variable:
    • Locate and edit the PATH variable in the Environment Variables list.
    • Append: ;%M2_HOME%\bin

On Linux/macOS:

  1. Open your shell profile configuration file. This depends on the shell you’re using:
    • For bash: ~/.bashrc or ~/.bash_profile
    • For zsh (default on macOS): ~/.zshrc
  2. Add the following lines to configure Maven and Java:
    export M2_HOME=/opt/apache-maven-<version>
    export PATH=$M2_HOME/bin:$PATH
    export JAVA_HOME=/path/to/your/jdk
    

    Example:

    export M2_HOME=/opt/apache-maven-3.9.5
    export PATH=$M2_HOME/bin:$PATH
    export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
    
  3. Reload the configuration:
    source ~/.bashrc    # For bash users
    source ~/.zshrc     # For zsh users
    

Step 3: Verifying the Installation

Now that Maven is installed and configured, it’s time to check if it works correctly.

  1. Open a terminal or command prompt.
  2. Run the following command:
    mvn -version
    
  3. You should see output similar to this:
    Apache Maven 3.x.x (...)
    Maven home: <Maven installation path>
    Java version: <your JDK version>, vendor: Oracle Corporation
    Java home: <JDK path>
    

Additional Installation Options

Using a Package Manager (Optional):

If you’re on Linux or macOS, you may install Maven using a package manager. Note that these methods may not always install the latest version.

  1. Linux (Ubuntu/Debian):
    sudo apt update
    sudo apt install maven
    
  2. Linux (CentOS/RHEL):
    sudo yum install maven
    
  3. macOS:
    Install Maven via Homebrew:

    brew install maven
    

Verifying Java:

Maven requires a JDK (Java Development Kit) to work. Verify that Java is installed by running:

java -version

If it’s not installed:

  • On Linux, install OpenJDK:
    sudo apt install openjdk-17-jdk   # For Ubuntu/Debian
    sudo yum install java-17-openjdk  # For CentOS/RHEL
    
  • On macOS, install OpenJDK via Homebrew:
    brew install openjdk
    

Step 4: Running Maven

You’re now ready to use Maven! Here are a few common commands:

  1. Check Maven Version:
    mvn -version
    
  2. Create a New Maven Project:
    mvn archetype:generate
    
  3. Build a Maven Project:
    mvn clean install
    

Final Thoughts

Congratulations! You’ve successfully installed and configured Apache Maven. With Maven set up on your system, you can now manage your Java projects with ease. Maven simplifies build processes, ensures consistent project management, and makes dependency management seamless. Feel free to explore its many features and extend your knowledge by diving into Maven’s powerful tools, plugins, and architecture.

Have fun coding! 🚀

How to Install and Set Up Java 17 on Your System

To install and set up Java 17 on your system, follow the steps below. The process may vary slightly depending on your operating system.


On Windows

  1. Download Java 17
  2. Install Java 17
    • Run the .msi installer file and follow the setup instructions.
    • Install Java in the default directory or specify a custom directory (e.g., C:\Program Files\Java\jdk-17).
  3. Set Environment Variables
    • Open the Start menu, search for “Environment Variables,” and click on “Edit the system environment variables.”
    • In the System Properties window, click on the “Environment Variables” button.
    • Under “System Variables,” find the Path variable and click Edit.
    • Add the path to the bin directory of your Java installation (e.g., C:\Program Files\Java\jdk-17\bin).
    • Click OK on all windows to save your changes.
    • Optionally, set a JAVA_HOME variable:
      • Click New under “System Variables.”
      • Name the variable JAVA_HOME and set its value to the path of your Java installation (e.g., C:\Program Files\Java\jdk-17).
  4. Verify Installation
    • Open a Command Prompt and run:
    java -version
    
    • If installed properly, it will display the Java 17 version.

On macOS

  1. Download Java 17
    • Visit the Oracle JDK or OpenJDK website, and download the .dmg installer for macOS.
  2. Install Java 17
    • Open the .dmg file and follow the installation instructions.
    • Java will be installed, usually in /Library/Java/JavaVirtualMachines/.
  3. Set Environment Variables (Optional)
    • Open a terminal and edit the ~/.zshrc (for zsh users) or ~/.bash_profile (for bash users) file using a text editor.
    • Add the following lines to set the JAVA_HOME variable:
    export JAVA_HOME=$(/usr/libexec/java_home -v 17)
    export PATH=$JAVA_HOME/bin:$PATH
    
    • Save and close the file, then reload the shell configuration:
    source ~/.zshrc
    
    • Note: The /usr/libexec/java_home command automatically detects installed Java versions.
  4. Verify Installation
    • Run the following in Terminal:
    java -version
    
    • It should show the Java 17 version.

On Linux

  1. Install OpenJDK 17
    • Use your package manager to install OpenJDK 17:
      • For Debian/Ubuntu-based systems:
      sudo apt update
      sudo apt install openjdk-17-jdk
      
      • For Red Hat/CentOS/Fedora-based systems:
      sudo dnf install java-17-openjdk-devel
      
  2. Set Default Java Version
    • If multiple Java versions are installed, you can set Java 17 as the default:
    sudo update-alternatives --config java
    
    • Select the path for Java 17 from the list.
  3. Set Environment Variables
    • Edit the ~/.bashrc or ~/.zshrc file and add:
    export JAVA_HOME=/usr/lib/jvm/java-17-openjdk
    export PATH=$JAVA_HOME/bin:$PATH
    
    • Save the file and reload it:
    source ~/.bashrc
    
  4. Verify Installation
    • Run the following command:
    java -version
    
    • It should display details about Java 17.

Optional: Verify Java Compiler

To ensure the javac compiler is working:

javac -version

That’s it! Now Java 17 is installed and ready to use.

How to copy files ending with a specific string to another file in Linux/macOS?

In Java 17, the Locale for Indonesia changed from in_ID to id_ID. In my project, the resource bundle files are named with the suffix _in.properties which is unrecognized by Java 17. To resolve this issue, I need to create copies of these resource bundle files that ends with _id.properties.

Here are solutions for both Linux/macOS (using Bash).

#!/bin/bash

# Set the root directory for the search
ROOT_DIR="/path/to/root_directory"

# Find all files ending with '_in.properties' and process each one
find "$ROOT_DIR" -type f -name "*_in.properties" | while read -r FILE; do
    # Construct file name by replacing '_in.properties' with '_id.properties'
    NEW_FILE="${FILE%_in.properties}_id.properties"
    # Copy the original file to the new file
    cp "$FILE" "$NEW_FILE"
done

Save this script as copy_properties.sh, make it executable with chmod +x copy_properties.sh, and run it with ./copy_properties.sh.

Explanation

  • find "$ROOT_DIR" -type f -name "*_in.properties": Finds all files ending with _in.properties.
  • while read -r FILE; do ... done: Loops through each found file.
  • ${FILE%_in.properties}_id.properties: Constructs the new file name by replacing _in.properties with _id.properties.
  • cp "$FILE" "$NEW_FILE": Copies the original file to the new file.

These scripts will recursively search the specified directory for files ending with _in.properties, then create a copy of each file with _id.properties in the same directory.