Maven is a tool for building software. Doesn’t there already exist a number of tools for doing this exact task? Two different tools that do this are the classic make utility or the much newer Apache Ant.
The make utility requires a script that defines all of the objects and their dependencies. Make forces you to define every object and their dependencies. That is a bit of a harsh analysis of the make tool as it is possible to create more generic rules for compiling objects from source but in general this is accurate description of make.
Apache Ant is a few more steps along the automation path from make. You define some of the targets and a few rules in your build.xml file for compiling code, making jar files or other general commands. There is no default defined directory structure for the source files or external libraries. You need to define the structure each time you create a new program.
Maven has a number of differences from other build tools. There is a very specific directory structure for each of the types of programs or objects that Maven can build. In addition to the actual directory structure Maven has the concept of a the software lifecycle for the objects it builds.
Maven will perform all the steps in the lifecycle from the start up until the phase that you select.
- validate – perform a check to ensure that all the necessary files are available.
- compile – compile the source code of the project.
- test – runs the unit tests that have been defined.
- package – create the final package for the code. This might be a JAR, WAR or EAR file.
- install – install the package into the local repository.
- deploy – install the package into the remote repository.
Thus the program is compiled before it is tested and tested before it is packaged.
It is possible to setup a similar flow using Apache Ant but then you are responsible to create lifecycle from scratch. This includes adding additional logic for doing testing and implementing logic for installing the compiled objects.
In addition to all the other built-in logic that Maven contains it also is well aware of the different types of output objects such as jar or war files.
Another major difference between these other tools is that Maven has its own repository of jar files and other artifacts that might be needed when building objects. Maven can store multiple versions of the various artifacts in its repository.
The central repository is full of all sorts of open source jar files so it is quite possible that if your project uses one of these jars (ie log4j) then it will already be available. You simply add your required dependency and it will be downloaded to your computer and used on the next build.
Each object in the repository is defined by the unique combination of three different keys.
The combination of these three keys creates a unique value that refers to the object specifically. This built in repository concept actually really simplifies dealing with libraries if you only need open source libs.
Of course it is also possible to add other third party libraries. Using libraries that are not already in the repository is a bit different and will be described later.
Projects and project structures
One of the advantages to Maven is the structure that it provides. Maven contains a lot of templates and standard directory structures. There is a structure available if you simply need to create a java jar file but there is also a template if you want to create restful web applications.
Of course, it is possible to create these directory structures manually but another feature of Maven is that you simply pick which type of project you wish and it will create the entire directory structure for you.
Testing your work
It goes without saying that providing unit tests would be both a best practice and is easy enough to do if you start at the beginning. Performing the unit tests is part of the lifecycle and because they are run before creating any packages it should help to guarantee a higher quality of program.
Maven is a enterprise quality build tool that supports more than just the generation of programs or libraries.
Maven is big but I will attempt to describe some of the basics for anyone who wishes to expand their tool set to include it.