Hi!
In this video we will introduce you to the build tool Maven, and describe how it's used.
Maven is the tool we will use to compile our applications.
In the handout that accompanies this video, we will install Maven and make sure it is
functioning.
When you start writing an application, you need somewhere to start.
Most people start writing programs in an IDE, without much thought about what is happening
behind the scenes.
Applications are just files in a directory.
You might store all your Java libraries in a folder on your hard drive called “C:\My
Java Libraries”.
Your IDE will happily load the libraries from your application directory.
It will store a reference inside a project file, and everything will be happy.
The problem with this approach is when you try to move the program to another computer.
The directories you created might not match the directories on your new computer.
The problem is worse when you need to make sure everyone is using the same library versions
to compile.
Luckily there is a better solution.
Maven is a tool that helps you structure your project, knows how to compile your code, run
your unit tests, assemble any artifacts, and maintains a database of dependencies your
project might have.
If you’re from a C/C++ world, Maven is like a make file on steroids.
Instead of a make file, Maven uses something called a Project Object Model file, or POM
file.
This is the file that describes your project, what libraries should be included, including
the specific library version, and what files should be compiled.
We will look at this file in depth in the next few videos.
A dependency is a library your application requires to compile.
Not only does Maven examine what libraries are required, it also examines what libraries
those libraries need.
It builds a whole tree of dependencies.
Maven looks in your local Maven repository first for these required libraries.
The local Maven repository is the collection of libraries installed on your computer.
It doesn’t matter if you have your repository in one location, and another developer has
hers in a different location.
The maven installation will know the correct repository location.
When you compile an application, Maven will look to find required files in the local repository.
If Maven cannot find a dependency with a specific version in your repository, it will look for
the dependency in other publicly shared repositories on the internet.
A build is the action of creating compiling and packaging your application.
Every project goes through several steps or phases in it’s build process.
You compile the code, package the code, test the code and eventually deploy the code.
A maven project also goes through these same steps in the Maven build lifecycle.
With Maven you can choose to start at any phase of the lifecycle.
You can use maven to compile the code, then when you are ready you can move on to testing
and deploying.
Maven has the following default build phases you can use: validate, compile, test, package,
integration-test, verify, install, deploy.
If you execute the package phase, you'll run the validate, compile, test, and package build
phases.
If you execute the compile phase, you will run the validate and compile build phases.
You can also insert other build phases into the life cycle, but for the most part these
are the only build phases we will use.
Lets take a quick look at using these build phases with Maven.
There’s a simple Maven project included in the course materials for this lecture.
Unzip the project into a directory on your machine.
If you want to compile your code, you simply type mvm compile.
This will compile your classes, and that’s it.
It will not produce a jar or war.
You can verify this by looking in the target directory.
That’s where Maven puts all your compiled artifacts.
Sometimes you might need to clear out this directory.
There’s a command that isn’t part of the build phase lifecycle - that’s clean.
If you type mvn clean, you can see this.
Now the target directory is deleted.
This is also a good state to deliver your project to other team members, or to a source
code repository.
Your project should only contain code.
If you want to run tests, we can run that maven phase.
Typing mvn test will compile and test your code.
Lets check out at how that looks.
Ultimately you’ll want to compile, test and package your code into a jar or war file.
Most of the time we’ll want to do it all in one shot.
You’ll want to clean the project, then compile, test, and package your code.
We can chain the cleaning to this process and run mvn clean install.
Here’s how that looks.
We are going to do this often in this class.
You can compile in your IDE as well, but if you want to make sure your code compiles 100%,
this is the way to do it.
IDEs do weird things, cache previously compiled code, and you can spend a long time tracking
down errors due to IDE craziness.
Compiling from the command line can tell you immediately if the problem is your IDE, or
your code.
We've taken a quick look at how Maven works, and the Maven build lifecycle.
You're thinking, Sounds great!
You’re sold!
So where do you start?
First download and install the latest Maven version from the Maven website.
Depending on what computer you're using, your install steps will be different.
For this reason, I've created a PDF describing the installation for each.
Use the correct instructions for your computer.
When you’re done, you should be able to type mvn -version from any directory on your
machine and it will reply with the current installed version of Maven.
If you get an error, double check your steps.
Next you're ready to learn how to create Java projects using Maven, archetypes and more