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:
Maven needs Java, its presence can be verified by the follow command:
In case you couldn't find Java on your system, you can install it with this operation:
Then check version again. We expect you see picture like this:
Open the download page in your browser and get the latest version (3.9.2 on the June 2023).
Installation process
Unpack the downloaded archive:
Next, move the result to the /opt directory:
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
Then you should change script's permissions:
Then rebooting the machine or run this command:
Check the installation result
To check the correctness, run this command:
We expect that you see picture like this, its indicates that everything is OK:
First app building
Lets illustrate our first app build process. First, create your project app and "fall" inside:
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>
You also can download this file here. Then start building process itself:
Result should be in target subdirectory:
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.