Apache Maven is so Famous? But Why?

by Jul 14, 2015

Apache Maven

This video introduces you to Apache Maven. It was originally part of a larger unpublished video tutorial on creating JAXRS applications using Java.

Transcript – Apache Maven is so Famous? But Why?

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

Tools Used

  • Maven

Media Credits

All media created and owned by DJ Spiess unless listed below.

  • Background image from Pixabay.com

Get the code

The source code for this video can be found on Github. If you have Git installed on your system, you can clone the repository by issuing the following command:

git clone https://github.com/deege/maven.git

Go to the Support > Getting the Code page for more help.

If you find any errors in the code, feel free to let me know or issue a pull request in Git.

Don’t miss another video!

New videos come out every week. Make sure you subscribe!



DJ Spiess

DJ Spiess

Your personal instructor

My name is DJ Spiess and I’m a developer with a Masters degree in Computer Science working in Colorado, USA. I primarily work with Java server applications. I started programming as a kid in the 1980s, and I’ve programmed professionally since 1996. My main focus are REST APIs, large-scale data, and mobile development. The last six years I’ve worked on large National Science Foundation projects. You can read more about my development experience on my LinkedIn account.

Pin It on Pinterest

Share This