Installing Apache Maven on Linux is essential for developers working on Java projects. Maven streamlines project management and automates the build process, making it easier to manage dependencies and project structures. To get started quickly, you’ll need a Java Development Kit (JDK) installed, and then you can move on to installing Maven itself.

The good news is that setting up Maven on Linux, particularly Ubuntu, is straightforward. We simply need to update our package index and use the apt package manager to install Maven. This way, we get the latest stable version directly from the repositories. We can verify the installation by running the mvn -version command, ensuring everything is set up correctly.
Once Maven is installed, it becomes a powerful tool in our development arsenal. Whether we’re compiling code, running tests, or managing dependencies, Maven’s seamless integration with the JDK turns these tasks into a breeze. Ready to transform our Java project workflow? Let’s dive right in!
Contents
Preparing Your Environment for Java and Maven
To ensure a smooth installation and operation of Maven on Linux, it is essential to have Java set up correctly first. Proper configuration of environment variables is crucial for seamless interaction between Java and Maven.
Setting Up Java
First, we need to install Java Development Kit (JDK). OpenJDK is a popular choice for many. On Ubuntu 20.04, you can install it using the package manager.
sudo apt update
sudo apt install default-jdk
Verify the installation:
java -version
If you see the Java version and vendor, you’re good to go. The path to your JDK is often critical. Identify it and set the JAVA_HOME environment variable.
export JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64
To make this permanent, add it to your shell startup script (e.g., ~/.bashrc or /etc/profile.d/java.sh).
echo "export JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64" >> ~/.bashrc
source ~/.bashrc
Installing Maven on Ubuntu
With Java set up, let’s move on to installing Maven.
sudo apt update
sudo apt install maven
Check your Maven installation:
mvn -version
You should see output showing the Maven version along with Java details. This indicates Maven is ready for use.
Our Maven installation directory by default is /usr/share/maven, and its binaries are linked to /usr/bin/mvn.
Configuring Environment Variables
Finally, setting environment variables like M2_HOME can streamline development.
export M2_HOME=/usr/share/maven
export MAVEN_HOME=/usr/share/maven
Add these lines to your shell startup script to make them persistent.
echo "export M2_HOME=/usr/share/maven" >> ~/.bashrc
echo "export MAVEN_HOME=/usr/share/maven" >> ~/.bashrc
source ~/.bashrc
For good measure, include Maven in your PATH as well to ensure the mvn command is accessible.
export PATH=$PATH:$M2_HOME/bin
Add it to your startup script too:
echo "export PATH=\$PATH:\$M2_HOME/bin" >> ~/.bashrc
source ~/.bashrc
By configuring JAVA_HOME, M2_HOME, and modifying the PATH, we ensure our tools are accessible and correctly linked, setting a solid foundation for Maven projects.
Understanding Maven Project Structure
The Maven Project Structure is quite organized, facilitating efficient project management and build processes. It features essential configurations and repository details crucial for developing and managing software projects effectively.
Exploring the Project Object Model
The Project Object Model (POM) is integral to any Maven project. Found in the pom.xml file, it contains all configuration details needed for building the project. Think of the POM as the blueprint of our project.
Within the pom.xml file, we define dependencies, plugin configurations, and project metadata. Dependencies include external libraries, specified using unique coordinates like groupId and artifactId. By defining these, Maven can fetch the correct versions from repositories.
Here’s a simple example of a dependency definition:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
This XML configuration ensures our project has access to the Apache Commons Lang library.
Maven repositories are classified into two types: local and remote.
- Local repositories reside on our computer. By default, Maven downloads dependencies to a
.m2folder in our home directory. - Remote repositories, like Maven Central, host a vast array of libraries and are accessed over the internet.
By understanding these repositories, we manage our project’s dependencies efficiently. Whenever Maven encounters a new dependency, it first looks in the local repository. If not found, it fetches it from a configured remote repository and stores it locally.
In our pom.xml, we can specify additional remote repositories using:
<repositories>
<repository>
<id>central</id>
<url>https://repo.maven.apache.org/maven2</url>
</repository>
</repositories>
This setup ensures our project can always find and use the necessary libraries, streamlining our development process.
Executing Maven Commands
Maven commands are essential for managing and building projects. We will explore some basic commands to get you started, then dive into advanced options for more control.
Basic Maven Commands
Before we start, it’s important to verify your Maven installation. Run the following command to check the version:
mvn -v
This command will display the Maven version, platform encoding, and default locale. Ensuring these are correct is a crucial setup step.
To compile your project, use:
mvn compile
This command processes the source code and generates compiled bytecode.
For packaging your project into a JAR file, use:
mvn package
This command bundles your project into a distributable JAR, WAR, or other specified package type.
If you need to execute tests within your project:
mvn test
This will run all the tests defined in your project.
Finally, to clean the target directory, use:
mvn clean
This command deletes the target directory, essentially resetting your environment for a fresh build.
Advanced Maven Options
For more complex operations, Maven offers advanced options. For example, if you need to skip tests during a build, use:
mvn install -DskipTests
This command installs your package without running the tests.
Sometimes, you might want to execute shell commands from within Maven. An example of this is running an npm command:
mvn exec:exec -Pstart-node
This runs the specified command without binding it to any Maven phase.
To set custom options or configurations:
mvn -s path/to/settings.xml
This specifies a custom settings.xml file, allowing you to override default configurations.
For debugging purposes, you might need to enable verbose output:
mvn -X
This displays a detailed debug log, helping troubleshoot issues during the build process.
By mastering these commands and options, we can effectively manage and build our projects with precision and control.