Maven. You must have heard or read about this tool in your automation journey. Moving from a beginner to an automation expert requires knowledge of Selenium Tools Galaxy – IDE, Webdriver, Log4j, Extent Reports, Apache POI, TestNG, Maven, Jenkins and Cucumber. These tools serve different purpose in the automation galaxy and are a must-know if you really want to excel in your career as a Test Architect. This article is to introduce the ‘Maven’ tool, which is a Yiddish word meaning “accumulator of knowledge”. But what is being accumulated here? The Selenium Automation project’s build components. Let’s kick-off with our Maven understanding…
What is a Build Tool?
Before anything else, Maven is a build tool. But what exactly is a build tool at the first place? In laymen terms – Build tool is used to set up everything which is required to run your java code independently. This can be applied to your entire java project. It generates source code, compiling code, packaging code to a jar etc. A build tool takes care of everything for building a process. It does following:
- Generates source code (if auto-generated code is used)
- Generates documentation from source code
- Compiles source code
- Packages compiled code into JAR of ZIP file
- Installs the packaged code in local repository, server repository, or central repository
What is Apache Maven?
Maven is a powerful project management and build automation tool that is used primarily for Java projects. Based on the concept of a project object model (POM), Maven can manage a project’s build, reporting and documentation from a central piece of information. It is used for projects build, dependency and documentation. It simplifies the build process like ANT. But it is too much advanced than ANT.
Maven addresses two aspects of building software:
- It describes how software is built.
- It describes its dependencies.
Maven is for defining how your .java files get compiled to .class, packaged into .jar (or .war or .ear) files, (pre/post) processed with tools, managing your CLASSPATH, and all others sorts of tasks that are required to build your project. An XML file describes the software project being built, its dependencies on other external modules and components, the build order, directories, and required plug-ins. It comes with pre-defined targets for performing certain well-defined tasks such as compilation of code and its packaging. Maven dynamically downloads Java libraries and Maven plug-ins from one or more repositories such as the Maven 2 Central Repository, and stores them in a local cache. This local cache of downloaded artifacts can also be updated with artifacts created by local projects. Public repositories can also be updated.
Maven was originally started as an attempt to simplify the build processes in the Jakarta Turbine project. There were several projects each with their own Ant build files that were all slightly different and JARs were checked into CVS. They wanted a standard way to build the projects, a clear definition of what the project consisted of, an easy way to publish project information and a way to share JARs across several projects. The result is a tool hosted by the Apache Software Foundation that can now be used for building and managing any Java-based project.
Why use Maven?
Maven’s primary goal is to allow a developer to comprehend the complete state of a development effort in the shortest period of time.
- Making the build process easy: While using Maven doesn’t eliminate the need to know about the underlying mechanisms, Maven does provide a lot of shielding from the details. Maven allows a project to build using its project object model (POM) and a set of plugins that are shared by all projects using Maven, providing a uniform build system.
- It helps to manage the Selenium project’s build easily. It will help to create right project structure, add and manage jar files in project’s build path etc.
- Providing quality project information: Maven provides plenty of useful project information that is in part taken from your POM and in part generated from your project’s sources. For example – Change log document created directly from source control, Cross referenced sources, Mailing lists, Dependency list, Unit test reports including coverage.
- Providing guidelines for best practices: Gather current principles for best practices development, and make it easy to guide a project in that direction. For example, specification, execution, and reporting of unit tests are part of the normal build cycle using Maven.
- Maven also suggests some guidelines on how to layout your project’s directory structure so that once you learn the layout you can easily navigate any other project that uses Maven and the same defaults.
- Have a lot of dependencies? No problem. Maven connects to remote repositories (or you can set up your own local repos) and automatically downloads all of the dependencies needed to build your project. This lets you avoid “dependency hell”. It’s NOT hard to download a single Java package and place it in your project’s folder, but think about having to do this for multiple dependencies every time one is updated. It’s a small amount of time, but adds up quickly.
- Maven also has lots of nice plug-ins that you can install that will handle many routine tasks from generating Java classes from an XSD schema using JAXB to measuring test coverage with Cobertura. Just add them to your pom.xml and they will integrate with everything else you want to do.
- Helps in Continuous integration (CI) for a Selenium Project. The process of building and testing the development work integrated at regular intervals is known as Continuous Integration (CI). To be able to use any CI tool (Hudson, Jenkins etc.), we need to have Source Code Repository (SVN/Git/CVS etc.) and Build Script (Ant/Maven etc.). Maven build helps here to create the JAR file for performing the CI operation.
Maven project structure and contents are declared in an xml file, pom.xml, referred as Project Object Model (POM), which is the fundamental unit of the entire Maven system. It is a single configuration file that contains the majority of information required to build a project in just the way you want. In order to build the project, Maven provides developers with options to mention life-cycle goals and project dependencies (that rely on Maven plugin capabilities and on its default conventions). Much of the project management and build related tasks are maintained by plugins. Developers can build any given project without the need to understand how the individual plugins work.
- Maven is a Java tool, so you must have Java installed in order to proceed.
- The src/main/java directory contains the project source code, the src/test/java directory contains the test source, and the pom.xml file is the project’s Project Object Model, or POM.
Although hardly a comprehensive list, these are the most common default life-cycle phases executed.
- Validate: validate the project is correct and all necessary information is available
- Compile: compile the source code of the project
- Test: test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
- Package: take the compiled code and package it in its distributive format, such as a JAR.
- Integration-test: process and deploy the package if necessary into an environment where integration tests can be run
- Verify: run any checks to verify the package is valid and meets quality criteria
- Install: install the package into the local repository, for use as a dependency in other projects locally
- Deploy: done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.
There are two other Maven life-cycles of note beyond the default list above. They are
- clean: cleans up artifacts created by prior builds
- site: generates site documentation for this project
Phases are actually mapped to underlying goals. The specific goals executed per phase is dependent upon the packaging type of the project. For example, package executes jar:jar if the project type is a JAR, and war:war if the project type is – you guessed it – a WAR. An interesting thing to note is that phases and goals may be executed in sequence.
mvn clean dependency:copy-dependencies package
This command will clean the project, copy dependencies, and package the project (executing all phases up to package, of course).
Given its strong backing and immense popularity, Maven is very stable and feature-rich, providing numerous plugins that can do anything from generate PDF versions of your project’s documentation to generating a list of recent changes from your SCM. And all it takes to add this functionality is a small amount of extra XML or an extra command line parameter.
The initial learning curve is steep, but (nearly) every professional uses Maven or wishes they did. You should use Maven on every project although don’t be surprised if it takes you a while to get used to it and that sometimes you wish you could just do things manually, since learning something new sometimes hurts. However, once you truly get used to Maven you will find that build management takes almost no time at all.
To summarize, Maven simplifies and standardizes the project build process. It handles compilation, distribution, documentation, team collaboration and other tasks seamlessly. It increases re-usability and takes care of most of the build related tasks. We hope this quick overview has piqued your interest in the versatility of Maven. Stay tuned for further articles on Maven’s usage in Selenium Webdriver Framework.