Apache Maven is a widely-used build automation and project management tool primarily utilized in Java development. It simplifies the process of building, packaging, and managing dependencies for Java projects by using a standardized project structure and a central repository for libraries and plugins. Apache Maven’s ability to streamline project workflows makes it a vital tool for developers working on large-scale Java applications.
On Ubuntu 24.04, 22.04, or 20.04, you can install Apache Maven via two primary methods. The first method is using the Ubuntu default repository, which provides a stable and straightforward installation suitable for most users. Alternatively, for those who need the latest version or a specific version of Maven, you can download and install Maven manually from the source archive. This guide will walk you through both installation methods, allowing you to choose the best option for your development needs.
Method 1: Install Apache Maven via APT Terminal Commands
Update the Ubuntu System Before the Apache Maven Installation
Before we dive into the installation process, ensuring that your Ubuntu system is up-to-date is crucial. This step helps avoid potential conflicts and ensures you have the latest security patches and system improvements. To update your system, you’ll need to execute the following command in your terminal:
sudo apt update && sudo apt upgrade
This command does two things. sudo apt update updates the package lists for upgrades for packages that need upgrading, as well as new packages that have just come to the repositories.
Install Apache Maven via APT Command
With your system updated, you’re now ready to install Apache Maven. As mentioned, Ubuntu includes Apache Maven in its default repository, simplifying the installation process. To install Apache Maven, execute the following command:
sudo apt install maven
This command tells APT to install the maven package. The sudo prefix ensures the command is run with superuser privileges necessary for system-wide installations.
Verify Apache Maven Installation via APT
After installing, verify that you installed Apache Maven correctly and check its version. This step ensures a smooth installation process and confirms you have the appropriate Maven version. To verify the installation and determine the version, use the following command:
mvn -version
This command asks Maven to display its version number, which should output information about the installed version of Maven.
Method 2: Install Apache Maven via source
Step 1: Install Default Java Development Kit (JDK)
First, to install Apache Maven on Ubuntu via the source archive, ensure you have Java installed on your Ubuntu system. Apache Maven needs Java to function. Install the default JDK on Ubuntu with this command:
sudo apt install default-jdk -y
This command instructs APT to install the default-jdk package. The -y flag automatically confirms the installation, saving you from having to manually approve it.
Download Apache Maven Archive
With Java installed, we can now download the Apache Maven archive. We’ll use the wget command for this, a utility for non-interactive download of files from the web.
To find the latest version of Apache Maven, you can visit the Apache Maven download page. For the purpose of this guide, let’s assume that the latest version is 3.9.2. To download this version, use the following command:
wget https://dlcdn.apache.org/maven/maven-3/3.9.2/binaries/apache-maven-3.9.2-bin.tar.gz
Note: The above command is an example, ensure you get the latest version link, as copying the above will get you an outdated version.
Extract Apache Maven Archive
After downloading the Apache Maven archive, we need to extract it. We’ll extract the archive to the /opt/ directory, which is a standard directory for storing optional or add-on software. To extract the archive, use the following command:
sudo tar xzf apache-maven-3.9.2-bin.tar.gz -C /opt/
This command uses the tar utility to extract the archive. The -xzf flags tell tar to extract (-x) the gzipped (-z) file, and the -C flag specifies the target directory for the extraction.
cd /opt/
Create Symbolic Link to the Apache Maven Directory
After extracting the archive, we’ll create a symbolic link to the Apache Maven directory. This step is optional but recommended, allowing you to refer to the directory more conveniently. To create a symbolic link, use the following command:
sudo ln -s apache-maven-3.9.2 maven
This command creates a symbolic link named maven
that points to the apache-maven-3.9.2 directory.
Setting Up the Apache Maven Environment
Next, we must set up the Apache Maven environment by defining some environment variables. We’ll create a new script file in the /etc/profile.d/ directory, a standard directory for adding scripts that set environment variables. To create the script file, use the following command:
sudo nano /etc/profile.d/maven.sh
This command opens a new file named maven.sh in the nano text editor. In this file, add the following lines:
export M2_HOME=/opt/maven
export PATH=${M2_HOME}/bin:${PATH}
These lines define two environment variables: M2_HOME, which points to the Apache Maven directory, and PATH, which includes the path to the Apache Maven binaries. To save the file and exit the editor, press CTRL+O and then CTRL+X.
Alternatively, we can use the echo command and the sudo tee command to achieve this. The echo command will generate the necessary lines and sudo tee will write these lines to the file. Here’s how you can do it:
echo -e "export M2_HOME=/opt/maven\nexport PATH=\${M2_HOME}/bin:\${PATH}" | sudo tee /etc/profile.d/maven.sh
This command does the following:
- The echo -e command enables interpretation of backslash escapes and generates the lines to be added to the file.
- The | (pipe) takes the output of the command on its left and uses it as the input for the command on its right.
- The sudo tee /etc/profile.d/maven.sh command writes the input it receives from the echo command to the file /etc/profile.d/maven.sh. The tee command is used in conjunction with sudo to write to files owned by the root user.
This command effectively creates the maven.sh file with the necessary environment variables without needing to open and edit the file manually.
Enable the Apache Maven Environment
With the environment variables defined, we need to enable them. We’ll do this by sourcing the script file we just created. Sourcing a script file means executing the file in the current shell, which allows the environment variables to be available in your current session. To source the script file, use the following command:
source /etc/profile.d/maven.sh
This command reads and executes the maven.sh file in the current shell. The “source command” is a shell built-in command that reads and executes commands from the file specified as its argument in the current shell environment.
Verifying Apache Maven Installation
After setting up the Apache Maven environment, verifying the correct setup is good practice. As before, check the installed version of Apache Maven to confirm the installation. Use the following command:
mvn -version
The command will display information about the installed Apache Maven version. If you configured everything properly, the version number should correspond with the version you downloaded.
Create and Test a Maven Project (Optional)
Create a New Maven Project
The first step in creating a Maven project is generating a new structure. Maven provides a command for this purpose, which creates a new directory structure with a default pom.xml file (Project Object Model file), the fundamental unit of work in Maven.
To create a new Maven project, navigate to the directory where you want to create the project and run the following command:
mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
This command tells Maven to generate a new project from an archetype. An archetype is a template of a project, which in this case is maven-archetype-quickstart, a basic Java project. The groupId and artifactId parameters are used to uniquely identify your project across all projects.
Navigate to the Maven Project Directory
After creating the project, navigate to the project directory using the following command:
cd my-app
This command changes the current directory to the my-app directory, which is the root directory of the project you just created.
Building the Maven Project
With the project created and the project directory set, you can now build the project. Building the project compiles the source code and packages it into a distributable format, such as a JAR file. To build the project, use the following command:
mvn package
This command tells Maven to execute the package phase of the build lifecycle. The package phase compiles the source code, runs any tests, and packages the compiled code into its distributable format.
Run the Maven Project
Finally, after building the project, you can run it to verify everything is working as expected. To run the project, use the following command:
java -cp target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App
This command runs the Java application contained in the JAR file produced by the mvn package command. The -cp flag specifies the classpath, and the argument to the java command is the main class of the application.
With the correct setup, this command will output “Hello World!” This indicates that your Maven project is functioning as expected.
Closing Thoughts
By installing Apache Maven on your Ubuntu system, you can streamline your Java development workflow and efficiently manage your projects. Whether you opt for the ease and stability of the Ubuntu default repository or the flexibility of manually downloading and installing the latest or specific Maven version, both methods provide a reliable way to get Maven up and running. Regular updates and version management will ensure that your Maven setup remains current, allowing you to take full advantage of its powerful project management and build automation features on Ubuntu.