Pages

Getting started with Maven

Overview

I still remember my initial college projects where we use to develop various components using various sets of API’s. We used to face compile problem transferring the project from one members machine to another’s machine mostly because the libraries were stored in a location in the main developer’s machine. And when it was transferred to another’s machine, the APIs were not in the same location. The jars were directly included in the build path or sometimes jars were included in a library and the libraries were included in the build path of the project. We also found a general method where, we made a Lib folder inside the project itself and put all our jars into that folder. Now all we have to do while transferring our project from one machine to another machine was to transfer the root folder completely, and include the /Lib folder in the build path of the project. There were other problems however. We have heard about version control systems, and we were using it to store our project safe in the remote location. We have to upload all the library files (jars) to the Git. Certainly not a good idea. The source files were few MB where as the jars could be hundreds of MBs. There is however an easy solution to the problem of which we were unaware  then. The name of the solution is called maven. I have tried it only for Java so I don’t know whether it is or some similar tools is available for other language.  

Introduction

Maven is a command line tool mainly. You go to a location here. Extract it in some location in your computer. Add the location of the bin file in the path of the operating system. Now open a command-prompt and type mvn and click Enter. If it displays some information about maven, congratulations, Maven is installed.  

Creating Maven Project with eclipse

As per the concept of learning by example, let's create a maven project from eclipse and see our first experience with maven. Lets create a java application that requires “Selenium” API. A little thing about this API is that it is a web Driver which can simulate users action of browsing the web browser(hovering, clicking, scrolling, downloading etc.). Lets create a normal Java project with eclipse. clip_image002
 Now , right click on the project folder in the left Project-Explorer panel and then click configure->”Convert to maven Project”. clip_image004
 A configuration window will appear where you have to provide a couple of information. Some of them are group Id, Artifact Id, Version, Packaging, Name, Description etc. These are the minimum things you have to do as shown in the picture. clip_image006
 The group Id, artifactId are rather kind of a signature for the project. You can guess what others are for. Clicking on the finish button will create a pom.xml( Project Object Model) file. clip_image008 This is a general pom for the whole project and not related to just selenium only. Selenium will be just a dependency for the project. To add the dependency , Right click on the pom file and then click Maven->”Add Dependency”. clip_image010 A window will appear. Type “selenium” on the search box. clip_image012
 Select the seleniumhq as selected and click ok. It will add a dependencies tag in the pom. Now create a class as the example below.
Note: For the eclipse to find the search term(eg. selenium here), the maven repository index should have been created. If its not created, Goto Window->ShowView->Maven Repositories and click it. When it appears, expand the central Repository, right click and then finally Create Index. You should have active internet connection, and it could take some time.

import java.util.concurrent.TimeUnit; 

import org.openqa.selenium.By; 

import org.openqa.selenium.WebDriver; 

import org.openqa.selenium.WebElement; 

import org.openqa.selenium.chrome.ChromeDriver; 

import org.openqa.selenium.firefox.FirefoxDriver; 

import org.openqa.selenium.ie.InternetExplorerDriver; 

import org.openqa.selenium.remote.DesiredCapabilities; 

public class GoogleTest { 

public WebDriver driver; 

public String testUrl = "http://www.google.com"; 

public GoogleTest() { 

initiateFirefoxDriver(); 

//driver = initiateIEDriver(); 

//driver = new ChromeDriver(); 

} 

public void initiateFirefoxDriver() { 

driver = new FirefoxDriver(); 

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 

} 

public WebDriver initiateIEDriver() {// 

DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer(); 

capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true); 

WebDriver driver = new InternetExplorerDriver(capabilities); 

return (InternetExplorerDriver)driver; 

} 

public void test1() { 

driver.get(testUrl); 

WebElement searchBox = driver.findElement(By.name("q")); 

searchBox.sendKeys("selenium"); 

driver.findElement(By.id("gbqfba")).click(); 

} 

public static void main(String[] args) { 

GoogleTest gt = new GoogleTest(); 

gt.test1(); 

} 

}
Now you are ready to run the class file. Run it and you will see the result.  

Summary

Notice that you have not downloaded any of your library files (jars) and added them in the build path of the project. Rather you added the dependencies in the pom, and the maven, which is the most popular build tool for java applications, will automatically add the dependent libraries into your projects. This method is very useful in the industrial environment as it is totally tiresome to build the dependency manually, and then compile it etc. I will talk about the deployment automation in my next document. Till then happy coding and happy mavenization.

No comments:

Post a Comment

If you like to say anything (good/bad), Please do not hesitate...