Apache Maven is one important tool that we will be using in creating every single Selenium project from here. In short its a software project management and comprehension tool. It allows the developer to automate the process of creating the initial folder structure for Java application, performing the compilation, testing, packaging and deploying the final product. It is implemented in Java which makes it platform-independent. Java is also the best work environment for Maven. Some of the key feature of Maven are:
- Convention over Configuration: Maven uses Convention over Configuration which means developers are not required to create build process themselves. Maven provides sensible default behavior for projects.
- Dependency management: Maven encourages the use of a central repository of JARs and other dependencies. Maven comes with a mechanism to download any JARs required for building your project from a central JAR repository.
- Repository: Project dependencies can be downloaded from local file system, from Internet or public repositories like Maven Central.
- Extensible via plug-ins: The Maven build system is extensible via plug-ins, which allows to keep the Maven core small. The Maven core does for example not know how to compile Java source code, this is handled by the compiler plug-in.
We have to go through following topics like Maven Lifecycle, POM and Super POM, Standard Directory Layout, Repositories, Dependency mechanism, Plugins etc. to gain more knowledge on Maven. Complete Maven documentation is placed in Apache Maven Documentation .
Download and Install Maven
The first step is to download and install Maven in your machine. We need to follow below steps for installing Maven.
- Before installing Maven, we have to ensure that JDK is already installed in our machine and path is also set. All the steps for installing JDK is mentioned in Installing Java Development Kit… and setting path is mentioned in Setting up Environment Variables….
- Open Browser and navigate to Google. Type in “Maven download” and go to downloads page in maven.apache website. Direct link to Maven download page Maven Download.
- Click on the Binary zip archive link to start the download in the local machine. This is same for Windows and Mac machines.
- Once download is complete in the local Downloads folder of our machine, we have to move the ‘apache-maven-3.5.0’ folder to a location which is consistent with other applications. For Mac machine, we can copy and paste the folder in your home folder, e.g. ‘/Users/sejijohn’ location. We can also use the Terminal in Mac to move the folder from Downloads to home folder. We can use the below command in Terminal to move the ‘apache-maven-3.5.0’ folder to Home. This command will require password authentication.
--sudo mv source_folder destination_folder-- sudo mv /Users/sejijohn/Downloads/apache-maven-3.5.0/ /Users/sejijohn
For Windows machine, we can paste the ‘apache-maven-3.5.0’ folder inside C Drive ‘Program Files/Apache’ folder.
- Next step is to set the environment variables for Maven. For Mac OS, Open Terminal and type in the following command “vim .bash_profile” and then type ‘i’ to insert into the VIM Editor. We need to set M2_Home and MAVEN_HOME variables and then update existing PATH variable. Type in below commands in the VIM editor.
--Set up M2_HOME-- export M2_HOME=/Users/sejijohn/apache-maven-3.3.9 --Set up MAVEN_HOME-- export MAVEN_HOME=/Users/sejijohn/apache-maven-3.3.9 --Append existing PATH-- export PATH=$PATH:/Users/sejijohn/apache-maven-3.3.9/bin
To Save the entires in VIM Editor, hit Escape, type in: “:wq + Enter”.
To Quit without saving the editor, hit Escape, type in “:q!”. To save the entries in .bash_profile, use below command.--Save the entries in .bash_profile-- source .bash_profile
- For Windows OS, type “advanced system settings” in the search box (beside the Windows start button), click View advanced system settings. Select Advance tab and click Environment Variables button. To run maven from command line in Windows we have to set MAVEN_HOME, M2_HOME and PATH variables. In System variables, add a new ‘MAVEN_HOME’ variable and point it to the Maven folder inside C Drive and that is “C:\Program Files\Apache\apache-maven-3.5.0”. Add another variable ‘M2_HOME’ and point it to same Maven location in C Drive.
- We need to update the PATH variable.For that we have to point it to bin folder inside Apache Maven folder.
- Once the environment variables settings are completed, next step is to verify if the maven installation is successful. For that go to Mac Terminal or Windows Command Prompt and type in ‘mvn -version‘. If Maven installation is successful, we will see the below message.
With this we have completed the steps to install and configure Maven in Windows and Mac machines. In the next post we will create a sample Maven Project using Eclipse. Till then, Happy Learning!