Apache Maven is an open-source automation tool that allows you to automate and simplify software development processes. It is also known as a build management system and is used to create and manage projects written in C#, Ruby, Scala, and other programming languages. It is used by many developers to manage all stages of the software development lifecycle.
In this post, we will show you how to install Apache Maven on Debian 11.[jumpbox]
Step 1 – Install Java
Apache Maven is a Java-based application, so Java must be installed on your server. If not installed, you can install it by running the following command:
apt-get install default-jdk -y
After the successful installation, verify the Java installation using the command below:
java -version
You should see the Java version in the following output:
openjdk version "11.0.13" 2021-10-19 OpenJDK Runtime Environment (build 11.0.13+8-post-Debian-1deb11u1) OpenJDK 64-Bit Server VM (build 11.0.13+8-post-Debian-1deb11u1, mixed mode, sharing)
Step 2 – Install Apache Maven
First, run the wget command to download the latest version of Apache Maven:
wget https://dlcdn.apache.org/maven/maven-3/3.8.4/binaries/apache-maven-3.8.4-bin.tar.gz
You will get the following output:
HTTP request sent, awaiting response... 200 OK Length: 9046177 (8.6M) [application/x-gzip] Saving to: ‘apache-maven-3.8.4-bin.tar.gz’ apache-maven-3.8.4-bin.tar.gz 100%[=================================================================>] 8.63M --.-KB/s in 0.1s 2021-12-05 10:40:12 (61.4 MB/s) - ‘apache-maven-3.8.4-bin.tar.gz’ saved [9046177/9046177]
Once the download is completed, extract the downloaded file using the command below:
tar -xvzf apache-maven-3.8.4-bin.tar.gz
Next, move the extracted directory to /opt with the following command:
mv apache-maven-3.8.4 /opt/maven
Step 3 – Setup Environment Variables for Maven
Next, you will need to set Java environment variables for Maven. You can set them by creating a file in the /etc/profile.d/ directory:
nano /etc/profile.d/maven.sh
Add the following lines based on your Java installation path:
export JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64 export M2_HOME=/opt/maven export PATH=${M2_HOME}/bin:${PATH}
Save and close the file, then load the environment variable using the following command:
source /etc/profile.d/maven.sh
Step 4 – Verify Apache Maven Installation
At this point, Apache Maven is installed on your system. You can now verify the Apache Maven installation using the command below:
mvn -version
You should see the following output:
Apache Maven 3.8.4 (9b656c72d54e5bacbed989b64718c159fe39b537) Maven home: /opt/maven Java version: 11.0.13, vendor: Debian, runtime: /usr/lib/jvm/java-11-openjdk-amd64 Default locale: en_US, platform encoding: UTF-8 OS name: "linux", version: "5.10.0-8-amd64", arch: "amd64", family: "unix"
Step 5 – Create a Project with Maven
Now, open your command-line terminal interface and run the following command to create your first Maven project:
mvn archetype:generate -DgroupId=com.example.app -DartifactId=testapp -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false
You will get the following output:
[INFO] ---------------------------------------------------------------------------- [INFO] Using following parameters for creating project from Archetype: maven-archetype-quickstart:1.4 [INFO] ---------------------------------------------------------------------------- [INFO] Parameter: groupId, Value: com.example.app [INFO] Parameter: artifactId, Value: testapp [INFO] Parameter: version, Value: 1.0-SNAPSHOT [INFO] Parameter: package, Value: com.example.app [INFO] Parameter: packageInPathFormat, Value: com/example/app [INFO] Parameter: package, Value: com.example.app [INFO] Parameter: groupId, Value: com.example.app [INFO] Parameter: artifactId, Value: testapp [INFO] Parameter: version, Value: 1.0-SNAPSHOT [INFO] Project created from Archetype in dir: /root/testapp [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 21.503 s [INFO] Finished at: 2021-12-06T04:01:38Z [INFO] ------------------------------------------------------------------------
Next, navigate to your project directory and compile the application using the command below:
cd testapp mvn package
You will get the following output:
[INFO] Building jar: /root/testapp/target/testapp-1.0-SNAPSHOT.jar [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 19.623 s [INFO] Finished at: 2021-12-06T04:02:22Z [INFO] ------------------------------------------------------------------------
Now, run the following command to test your Jar file:
java -cp target/testapp-1.0-SNAPSHOT.jar com.example.app.App
You should see the following output:
Hello World!
Conclusion
Congratulations! You have successfully installed Apache Maven on Debian 11. You can now use Apache Maven in your development environment to speed up your application development process. Try it on dedicated hosting from Atlantic.Net!