Building Lifecycles and Plugins

The commands that can be used with Maven to trigger compiling, building, and testing, among other tasks, are based on the idea of a build lifecycle. In this video, we explore how Maven is composed of lifecycles, phases, and goals.

Maven Phases

See the Maven docs for a list of all phases included in each of the three built-in lifecycles:

clean

default

site

Creating an Executable Java App with Maven

To create an executable JAR with Maven, you can use the shade plugin. Configuring this plugin involves altering pom.xml to include configuration of the plugin, which consists of indicating the main class that should be run when this JAR is executed. After adding this configuration to your POM, you can run the following in your terminal:

mvn shade

Then, you can run the app just like any other executable JAR:

java -jar your-archive-1.0.0-SNAPSHOT.jar

0:00

Maven is based on the idea of a build
lifecycle which refers to the process of

0:04

assembling and
distributing an artifact like a JAR file.

0:07

Maven ships with three lifecycles and

0:09

you can think of them in terms
of distributing an application.

0:12

The first is the clean lifecycle
during which all compiled and

0:16

packaged resources and
artifacts are removed from the project,

0:19

in other words the target
directory is deleted.

0:22

If you remember, this is the directory
that will hold all the .class files and

0:26

the generated JAR file.

0:27

Next is the default lifecycle wherein your
application code is compiled, tested,

0:32

packaged, iand deployed,

0:33

much of the heavy lifting is done
here in the default lifecycle.

0:38

Finally, there is the site lifecycle.

0:40

This allows you to generate some
HTML documentation of the project.

0:44

Now within lifecycle, there are phases.

0:46

For examples, some of the phases that
make up the default build lifecycle

0:49

are the compile, test,
package, and install phases.

0:54

So, switching back to the terminal,
if we want to compile our application,

0:59

we can invoke the compile phase from
the command line like this, mvn compile.

1:07

Now, this will download all the listed
dependencies as well as the dependencies

1:11

listed by the ones we included.

1:12

The dependencies of our dependencies
are called transitive dependencies and