What is maven actually doing?

I've never been a maven power-user, mostly I get by with my IDE doing most of my tasks, like running tests and whatnot. But I do use maven regularly, either just for running the tests before I commit or to figure out why the tests work in my IDE and not in Maven.

I've noticed during my years now as a developer that a lot of people don't think much more about maven than running:

mvn install

in the terminal.

And most of the time this is all you need. But did you know that when you run this command maven downloads dependencies, compiles, run tests and packages your application? Yeah you probably did. I mean it says so in the output. What you probably didn't know is that maven stores that jar in your local repository. Let's assume that you have a project that has this configuration:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>se.sideeffect</groupId>
    <artifactId>blogg</artifactId>
    <!-- other stuff omitted -->
</project>

Then you'd end up with the built jar in you .m2 directory. So why is this happening? Well when you think about it, it makes sense. You call install, and this is mavens concept of installing something in your system. It puts the generated jar in your local repository.

The Maven Lifecycle

In maven there's a concept of a lifecycle and a lifecycle has phases. In the documentation for maven we can read that the default lifecycle consists of theese well known phases (actually there are many more):

  • validate
  • compile
  • test
  • package
  • verify
  • install
  • deploy

When you call mvn install all the phases up to and including the install lifecycle are run.

As you can see the default lifecycle doesn't include clean, which is something I do (way to often probably) when I want to make sure that everything is run from a clean slate. This is because the clean phase belongs to the clean lifecycle. So when we call maven clean we're using the clean lifecycle and running it all the way to the clean phase. The clean lifecycle concist of the following phases:

  • pre-clean
  • clean
  • post-clean

As you probably know from experience we can call multiple targets at once, this for example is my standard command:

mvn clean package

This will first run the clean lifecycle up to and including the clean phase and then run the default lifecycle up to and including package. I am not 100% sure about this but my understanding is that the name of a phase must be unique, so that maven can figure out which lifecycle to run.