Nobody is going to start a conversation about Maven in the pub - it’s not hip, it’s not cool, it’s not down with it. That said, if you don’t know a bit about it then you’re probably not going to make it to the pub, because you’ll be stuck in the office tearing your hair out when nothing seems to work.
That was me, before I took a training day to learn about it. Time is valuable though, so I’m going to condense down the good bits so that you regain your social life and save your hair in five minutes flat.
Nobody is going to start a conversation about Maven in the pub - it’s not hip, it’s not cool, it’s not down with it. That said, if you don’t know a bit about it then you’re probably not going to make it to the pub, because you’ll be stuck in the office tearing your hair out when nothing seems to work. That was me, before I took a training day to learn about it. Time is valuable though, so I’m going to condense down the good bits so that you regain your social life and save your hair in five minutes flat.
Maven is a software build tool. It takes in a configuration for your project in XML and spits your fully tested and (hopefully) working software out the other end.
Maven doesn’t actually do very much by itself - when you use Maven it is actually a number of plugins which do the work, and Maven just acts to coordinate them. There are plugins for running tests, packaging your code and deploying to containers, among many many others.
Yes, you can specify the plugin you want to run, and the task you want it to perform. Say you wanted to redeploy your app in a Tomcat 7 container, you could run the command mvn tomcat7:redeploy
. Running a single plugin is not where the power of Maven really lies though. The main way to use is through its lifecycle phases.
The Maven lifecycle is the set of stages that a build goes through from compilation, to testing and on to deployment. It provides an abstraction away from the individual plugins which make up the process. The most commonly used steps in the lifecycle are test
, package
, install
and deploy
.
Each plugin has a lifecycle phase which it is configured to run against by default. So, for example, if your project creates a WAR file then running the package
lifecycle phase will invoke the war plugin and create the WAR file. It will also run all of the lifecycle phases which precede it, so your code will be compiled and tested before being put into the WAR file.
Probably not what you think it does. With Maven install
has a special meaning, it does not mean it’s going to deploy your application. Maven works with a central repository which stores packaged up copies of over a million dependencies which you can use in your projects. To save pulling these dependencies over the network every time you do a build it maintains a cache which it calls the local repository. Running the install
lifecycle phase will package your project and place it in your local repository so that it is available to be used a dependency in other projects on your machine.
Often when you are using Maven to build or run your project you will be specifying properties like the hostname for a database server. It can get tedious putting these in every time, so wouldn’t it be great if there was a file you could specify this in so it would be applied to every build? Good news - there is! In your Maven home directory there is a file called settings.xml and you can add it in there.
If you want to live life on the edge and deploy your application without running any tests, you can do that. Just append -DskipTests
to the command, but do use sparingly...
If running no tests makes you feel slightly uncomfortable then you can choose just to run one single test class at a time. If you append -Dtest=MyTestClass
(where MyTestClass
is the test class you want to run) then you will run only those tests and not others.
I’m afraid not - Maven is a big and complex beast, and you will undoubtedly run into some problems that are going to need more than five minutes to learn how to solve. If someone has already set up the project though, this should be enough to get you on your way for now though. Enjoy your time in the pub.
Alistair Black
Tags: maven
June 22nd 2016