Everything You Want to Know About Maven – An Automation Tool for Java By Guest Contributor on March 30, 2020 Java is at the heart of digital transformation! From cell phones to data centers, game consoles to scientific supercomputers, it has been the platform for unlocking digital innovation everywhere. With more than 14 years of experience as a Java Development Company, our prime focus has always been to ensure that our developers have the best productivity tools. That’s why we made it a point that our programmers give a try to Apache Maven. Stay tuned to find out what our developers first reaction was! Why do you need Maven? “Why do we need Maven when we have Eclipse for Java Projects?” Yes, this is exactly what their first reaction was when we asked them to give Apache Maven a try. You must be aware that Eclipse is an Integrated Development Environment (IDE) that provides an environment to develop your projects. However, it doesn’t build your code, whereas, Maven does! In most of the Java Projects, you need to work with third-party libraries. For example, if you are working on a Spring MVC project, there are numerous dependencies that you need for your project. For those of you new to dependency, dependencies are nothing but the libraries or the jar files. So, for Spring, there are 10 to 12 libraries that you require and in order to use them you would need to download them first and then add them to your project. Don’t you think it is a tedious task to do all of these manually? In addition to that you also face another big issue. Suppose you’re working on the Java 8 version and for some reason you need to upgrade your project from Java 8 to Java 9. In this case, you would again need to download and add these dependencies for the latest software version. Here comes in the picture Apache Maven to make your life easier! Apache Maven in a Nutshell Maven is a powerful build automation tool that is mainly used for Java-based projects. The term “Maven” is derived from Yiddish language which means “accumulator of knowledge”. When you build a software, you need to take care of two critical aspects, that is, how to build it and its dependencies. And Maven effectively addresses these two aspects; no wonder it managed to become one of the choicest tools of Java developers. In the software build procedure, Maven uses conventions and you only need to write down the exceptions. In Maven, a fundamental unit of work is known as Project Object Model (POM). This POM.xml file, as already mentioned, stores all the necessary information regarding how a software is built and its dependencies on external components & modules. The POM.xml file also includes the built order, the directories and the required plugins. It also performs defined tasks like compilation of code and its packaging, thanks to Maven’s built-in system which holds predefined targets. Maven is capable of downloading Java libraries and Maven plugins from repositories such as the Maven central repository and then it stores these software artifacts in the local cache. If needed it can update the software artifacts downloaded in the local cache with the ones created by local projects. The same holds true for public repositories. In addition to Java Projects, you can also use Maven to build and manage projects written in Scala, C#, Ruby, and so on. How does Maven help? Maven makes your life easier by eliminating practices of having to do things manually. Maven can solve all your problems related to dependencies. You just need to specify the dependencies that you want in a POM.xml file and it will take care of the rest. Therefore, even in the later stages of your project if you need to upgrade the version of the software that you are using, Maven handles everything elegantly. You just need to make the change in the POM.xml file and it’ll automatically download the latest version for you. Unlike other build tools like Ant, you need not write long scripts to execute your tasks. Now, let’s take a look at the Maven architecture. Maven Architecture There are various components of Maven architecture – a local repository or the local machine that you work on (please refer to the diagram above). There is a central repository and then there is a remote repository or the remote web server. So, whenever you specify any dependency in the POM.xml file, Maven will look for it in the central repository first. If the dependency is present in the central repository it will copy the same onto your local machine. However, if that dependency is not present there, Maven will fetch it from the remote repository or remote web server using the internet. So, the internet is very much mandatory for using Maven. So, this was a glimpse of how Maven architecture works! Now, let’s move on to Maven Lifecycle, phases and goals. Maven Lifecycles, Phases and Goals Lifecycles. The Maven build comprises three built-in lifecycles which enables it to deploy and distribute your target project: Default – This is the main lifecycle for it takes care of project deployment. Clean – This not only cleans your current project but also removes files created during the previous build. Site – This is used to create the project’s site documentation. Each lifecycle comprises a sequence of phases. As shown in the image below, the default lifecycle consists of 23 phases, whereas, the clean and site lifecycles consist of three and four phases respectively. Phases. A Maven phase is nothing but a stage responsible for a specific task in the build lifecycle. Let’s deep dive into some of the important phases in the default build lifecycle: The validate phase checks if all the required information is available. The compile phase compiles the source code of the project whereas the test-compile compiles the tests or scope. The test phase is responsible for running the unit tests of the application. The package phase, as its name suggests, packages the compiled source code into a distributable format, ie., the war format or the jar format. Integration-test is the phase in which process and deployment of the package is needed to run the integration test. While in the install phase you install the package to a local repository. And in the deploy phase you copy the package to the remote repository. Now, an important point to note here is that all the phases are executed in a specific order. No matter whichever phase you run, it won’t run only that particular phase but also run the preceding phases. For example, if you run the install phase, which is the penultimate phase in the default build lifecycle, all the phases before the install phase will be executed and not just the install phase. Now, let’s move on to Maven goals. Goals. Each phase in a lifecycle consists of an array of task-specific goals. And when you run a phase, all goals associated with this phase are executed in order. The image below depicts some of the phases, the default goals bound to them and also the format of specifying the goals. Now, that you know all about Maven lifecycles, phases and goals, let’s talk about Maven Plugins. A Quick Glance at Maven plugins Maven plugins help you to execute all the tasks that are associated with goals which in turn are bound to phases. You can directly carry out your specific goal during Maven execution. You can modify a plug-in configuration by just using the plug-in declaration. Now, let’s discuss a few advantages of Maven that made it rise to fame! Advantages of Maven It allows you to understand the entire state of necessary development efforts within minimum possible time. With Maven you might need to delve deep in the underlying mechanisms, however, too much detailing could be avoided. It maintains a uniformity within the build system as you can use its POM for Maven project builds. Another major benefit is that you can share the very same set of plugins among different projects using Maven. You just need some time to get familiar with how Maven projects build; navigating among different projects is a breeze thereafter. It helps you follow best development practices and keeps your project guided in the right direction. It suggests some guidelines on how to layout your projects directory structure while assisting your project workflow. Maven enables you to update your installation easily and also allows you to take advantage of any changes that have been made to the tool itself. So, these were some of the advantages of Maven and now it’s time to have a talk with you! What’s your say… So, if you have been offering Java web development services to your clients, you must be well aware of this amazing tool – Apache Maven. What are your personal experiences with this automation tool for Java? Why not share them with our readers. In case your favourite build tool is something else, nevertheless, feel free to write down how Maven stacks against your one. We look forward to your comments! Author bio:- Poli Dey Bhavsar is a Content Writer at Helios Solutions. She puts her passion for content to work by writing stories on the latest tech trends and advancements in IT. When she is not hitting the keyboard, she is cooking delicacies, traveling and trying to unearth the meaning of life.