• Home
  • LLMs
  • Python
  • Docker
  • Kubernetes
  • Java
  • Maven
  • All
  • About
Maven | Installing and Configuring Maven
  1. References
  2. Install Maven
  3. Configure Maven
  4. Configure the ".m2" Directory

  1. References
    To use Maven, make sure that Java is properly installed and configured.
    See the following page to install and configure Java: Install Java (Ubuntu)
  2. Install Maven
    You can download Maven from the Apache Maven Project website: http://maven.apache.org/download.html

    Download the file: apache-maven-3.9.9-bin.tar.gz

    Extract the contents of this file to a location of your choice: /opt/apache-maven-3.9.9
    $ tar -xf ~/Downloads/apache-maven-3.9.9-bin.tar.gz -C /opt/
    $ chmod -R 755 /opt/apache-maven-3.9.9/
    $ sudo chown -R mtitek:mtitek /opt/apache-maven-3.9.9/
    
    Create a symbolic link:
    $ sudo ln -s /opt/apache-maven-3.9.9/ /opt/apache-maven
    $ sudo chown -R mtitek:mtitek /opt/apache-maven
    
    You should replace mtitek:mtitek with your actual user/group names.
  3. Configure Maven
    This section refers to the MAVEN_HOME environment variable.
    This variable contains the path to the Apache Maven binary distribution (where Maven is installed).
    The environment variable is optional but it's recommended, to simplify integration with applications that use it to locate the default Maven installation.

    You can add the "${MAVEN_HOME}/bin" directory to your PATH environment variable.

    You can also configure the M2_REPO environment variable to specify an alternative path to the repository folder (default is "${user.home}/.m2/repository").
    This path can also be configured in the user or global "settings.xml" file by setting the value of the "localRepository" element.

    The MAVEN_OPTS environment variable allows you to set options (e.g., initial heap size:-Xms, maximum heap size:-Xmx) used to start the JVM running Maven and can be used to provide additional parameters.
    See http://docs.oracle.com/en/java/javase/23/docs/specs/man/java.html

    Environment variables (Linux):
    $ vi .bash_profile
    
    export MAVEN_HOME="/opt/apache-maven"
    export M2_REPO="$HOME/.m2/repository"
    export MAVEN_OPTS="-Xms512m -Xmx1024m"
    
    export PATH="${MAVEN_HOME}/bin:$PATH"
    
    Note: Depending on your system, you may need to edit .bashrc or .profile instead of .bash_profile.

    Run "mvn -v" to verify that Maven is correctly installed:
    $ mvn -v
    
    Apache Maven 3.9.9
    Maven home: /opt/apache-maven
    Java version: 23.0.2, vendor: Oracle Corporation, runtime: /opt/jdk-23.0.2
    ...
    
    Note: To add an environment variable on Windows:
    • Right-click on "This PC".
    • Click on "Properties".
    • Click the "Advanced" tab.
    • Click the "Environment Variables" button.
    • Add the MAVEN_HOME environment variable with the path where Maven is installed.
    • Select the "Path" system variable and click "Edit".
    • Add the path: ${MAVEN_HOME}/bin
  4. Configure the ".m2" Directory
    The ".m2" directory is, by default, located in the ${user.home} directory (${user.home}).

    Note: The ${user.home} variable refers to the user's home directory path.
    For example, on my VM this variable corresponds to: "/home/mtitek".

    By default, the ".m2" directory may contain:
    • The "settings.xml" file: this file contains user-specific configurations.
      It allows you to customize the global "${MAVEN_HOME}/conf/settings.xml" file.
      See more information about this file: settings.xml

      You can copy the global "${MAVEN_HOME}/conf/settings.xml" file to the "${user.home}/.m2/" directory and adjust it as needed to configure your user-specific settings.

    • The "repository" folder: this folder contains the components installed locally by Maven, as well as all dependencies (and plugins) downloaded from remote Maven repositories.
© 2025  mtitek