1. What is Maven?
Maven is a tool that helps to build and manage Java projects. It handles things like downloading the libraries that project needs, compiling the code, running the tests, and packaging everything into a final file called .jar. Instead of doing all of this manually, Maven does it for you based on a set of rules written in a file called pom.xml.
2. Advantages of Maven
Maven downloads the libraries (dependencies), project needs straight from the internet, so there is no need to add them by hand.
Maven has standard project structure so that every Maven project follows the same folder layout, so any developer can jump into a new project and immediately know where things are.
Maven has an easy build process so that a single command like
mvn installcan compile the code, run the tests, and package the whole project.Maven has a huge ecosystem of plugins for testing, reporting, deployment, and more.
3. How do you install Maven?
First, I made sure Java (JDK) was installed, since Maven needs it to run:java -version
Then I updated my package list and installed Maven directly using
apt:sudo apt update
sudo apt install mavenOnce it finished installing, I confirmed everything worked by checking the version: mvn -version
4. Display output of maven version
Apache Maven 3.9.6
Maven home: /usr/share/maven
Java version: 17.0.10, vendor: Ubuntu
Java home: /usr/lib/jvm/java-17-openjdk-amd64
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "6.5.0", arch: "amd64"
The above output says the Maven version, the Java version it's linked to, and some basic OS details.
5. The 3 Types of Maven Repositories
(i) Local Repository — A folder on the own computer, where Maven stores every library it downloads. Once something's downloaded once, Maven just reuses it from here instead of re-downloading it every time you build.
(ii) Central Repository — The default, public repository on the internet (Maven Central) containing thousands of common libraries. If something isn't already in your local repository, Maven looks here next.
(iii) Remote Repository — A private or custom repository set up by a company or team, often using tools like Artifactory. This is useful for internal libraries that aren't meant to be public, or for controlling exactly which versions of dependencies are approved for use across a team.
6. Running Unit Tests with Maven
The command is as follows:
mvn test
This compiles the code and runs every test class found under src/test/java. Under that, Maven uses a plugin called "Surefire" to actually execute the tests, and gives an output as follows:
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
BUILD SUCCESS
If a test fails, the output is as BUILD FAILURE, along with details on exactly which test failed and why, which makes the debugging pretty straightforward once I understood how to read the output.