News
Seven days of Black Friday madness — great deal from Serverspace!
Serverspace Black Friday
VB
July 4, 2023
Updated July 25, 2023

How to install Maven on Ubuntu

Ubuntu

Introduction

Apache Maven is toolset disigned for automate software building. Generally it is used for Java projects but Maven could be easily extended to build different project types and development tasks. Maven will "close" two critical "pains" of software builders: first, it describes how the software is constructed, and second, it describes its dependencies. It employs standard practices for the building process and encompasses an extensive array of plugins for its fundamental capabilities.



Prerequisites

Don't hurry to start the installation procedure, renew system packages before:

apt-get update; apt-get upgrade -y

Maven needs Java, its presence can be verified by the follow command:

java -version

In case you couldn't find Java on your system, you can install it with this operation:

apt install -y default-jdk

Then check version again. We expect you see picture like this:

pic1

Open the download page in your browser and get the latest version (3.9.2 on the June 2023).

cd /tmp; wget https://dlcdn.apache.org/maven/maven-3/3.9.2/binaries/apache-maven-3.9.2-bin.tar.gz

pic2

Installation process

Unpack the downloaded archive:

tar xf apache-maven-3.9.2-bin.tar.gz

pic3
Next, move the result to the /opt directory:

mv apache-maven-3.9.2 /opt/maven

pic4Set correct ownership:

chown -R root:root /opt/maven

pic5

Environment Variables editing

Being that Maven is now installed in the /opt/maven directory, we should add some environment variables to enable Maven execution from the command line:


cat <<EOF >> /etc/profile.d/mymavenvars.sh
export JAVA_HOME=/usr/lib/jvm/default-java
export M2_HOME=/opt/maven
export MAVEN_HOME=/opt/maven
export PATH=${M2_HOME}/bin:${PATH}
EOF
ln -s /opt/maven/bin/mvn /usr/bin/mvn

pic6

Then you should change script's permissions:

chmod 755 /etc/profile.d/mymavenvars.sh

pic7

Then rebooting the machine or run this command:

source /etc/profile.d/mymavenvars.sh

pic8

Check the installation result

To check the correctness, run this command:

mvn --version

We expect that you see picture like this, its indicates that everything is OK:

pic9

First app building

Lets illustrate our first app build process.  First, create your project app and "fall" inside:

mkdir myproject; cd myproject

pic10

Remember, Maven uses special file called pom.xml to build your artifact (special termin meant Maven's work result). So, lets create this:


cat <<eof>> pom.xml
<project>
<modelversion>4.0.0</modelversion>
<groupid>instr.serverspace.io</groupid>
<artifactid>you_are_the_best</artifactid>
<version>1.0.0</version>
<build>
<plugins>
<plugin>
<groupid>org.apache.maven.plugins</groupid>
<artifactid>maven-compiler-plugin</artifactid>
<version>3.8.0</version>
<configuration>
<release>1</release>
</configuration>
</plugin>
</plugins>
</build>
</project>
EOF
</eof>

pic12

You also can download this file here. Then start building process itself:

mvn package

pic13

Result should be in target subdirectory:

pic14

Conclusion

After this guide reading you know how to install Apache Maven on Ubuntu 20.04 by getting the binary archive, configuring the variables, and check the result. Don't forget to consult Apache Maven documents for more details about features and plugins designed for different project types and tasks.

Vote:
5 out of 5
Аverage rating : 5
Rated by: 3
1101 CT Amsterdam The Netherlands, Herikerbergweg 292
+31 20 262-58-98
700 300
ITGLOBAL.COM NL
700 300

You might also like...

We use cookies to make your experience on the Serverspace better. By continuing to browse our website, you agree to our
Use of Cookies and Privacy Policy.