Introduction to Maven

Introduction to Maven

Introduction to Maven

Maven is a simple build automation tool which is basically used for enterprise Java projects, designed to take much of the hard work out of the build process. Maven uses a declarative approach, where the project structure and contents are described, rather than the task-based approach used in Ant or in traditional make files, for example. This helps enforce company-wide development standards and reduces the time needed to write and maintain build scripts.

The ease of source code compilation, distribution, documentation, collaboration with different teams and other vital tasks are seamless by using maven. Maven aims to describe 2 important things as how a software is built and the dependencies, plug-ins & profiles that the project is associated in a standalone or a distributed environment.

The maven can also be used in building & managing the projects written using C#, ruby and other programming languages as well.

Maven History

Maven was initially designed and developed by the Jakarta turbine project. At the later point of time, the Apache group developed the Maven to such an extent to support developing & building multiple projects together, publishing those projects, deploying them and generating the reports. The JARs/WARs of any maven project can be shared across any distributed environments.

Advantages of Using Maven over Ant

  • Managing dependencies.
  • Convention over configuration – configuration is very minimal
  • Multiple/Repeated builds can be achieved.
  • Focus on automation.
  • Plugin management.
  • Testing – ability to run JUnit and other integration test suites.
  • Making the development process transparent.
  • Provision to check the status of each build.
  • Avoiding inconsistent setups.
  • Standard and uniformed infrastructure among projects.
  • Few Glossaries around Maven

Maven Repository

Local Repository

This is the place where Maven stores all the project jars files or libraries or dependencies. By deault the folder name is ‘.m2‘ and by default the location in windows 7 is ‘Libraries\Documents\.m2‘.

Central Repository

Maven central repository is the default location ‘https://mvnrepository.com/‘ for Maven to download all the project dependency libraries. For any library required in the project, Maven first look in to the

.m2 folder of Local Repository, if it does not find the required library then it looks in Central Repository and download the library in to local repository.

Dependency Keyword

Dependencies are the libraries, which are required by the project. For example Log4j jars, Apache Poi jars, Selenium Jars. Dependencies are mentioned in the Maven pom.xml like this:

<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.43.1</version>
</dependency>        

Surefire Plugin

The Surefire Plugin is used during the test phase of the build lifecycle to execute the unit tests of an application. It generates reports in 2 different file formats like plain text file, xml files and html files as well. Even if you are using TestNG or Junits framework for reporting, this plugin is must to use, as it helps Maven to identify tests.

Maven POM

POM is Project Object Model XML file that contains information about the project and configuration details used by Maven to build the project. It contains default values for most projects. Some of the configuration that can be specified in the POM are the project dependencies, the plugins or goals that can be executed, the build profiles.

Install Maven on Eclipse IDE

The steps to Install Maven in Eclipse IDE are as below:

  1. Click on the Help from the top menu in Eclipse and select ‘Install New Software
  2. Click on the Add button on the newly opened window.
  3. In the Name box, type ‘Maven‘ and in the Location box, type ‘https://download.eclipse.org/technology/m2e/releases/‘
  4. Note: The URL is the location where you can download the Maven for Eclipse.
  5. A check-box will appear in the pop window, Check the check-box and click on Next button.
  6. Keep the default settings and click on Next button.
  7. Accept the ‘Terms and Conditions” and move forward by clicking on Finish button.
  8. Wait while it finish the installation.
  9. Once the installation is finished, it will ask to restart the Eclipse. Please click on Yes, so that changes can be reflected properly.

Install Maven on Windows

Maven is a build and dependency management tool for java based application development. Just like other java based development tools, it is not installed as windows service, rather it is configured using windows environment variables. These variables can be accessed from below location:

All Control Panel Items > System > Advanced system settings > Environment Variables

There are certain steps which are need to perform to set up Maven, such as installing Java on the system, setting up environment variable and downloading Eclipse IDE.

Follow the below steps one by one to set Maven in Windows:

  1. Download and Install Java
  2. Install Java, if it is not already installed. Java latest version can be installed from https://www.oracle.com/technetwork/java/javase/downloads/index.html.

  • To check the Java version installed on the machine, please go to Run and type ‘cmd‘ to launch the command prompt.
  • Now type ‘Java -version‘ and press Enter.

  1. Set Up Java Environment Variable
  2. Once the Java installation is done, set up the Java Environment Variable. To set the Java environment variable, open System Settings.

  • Go to My Computer and select Properties by Right Clicking on empty space in My Computer.
  • Click on ‘Change settings‘.
  • A pop up window will display, click on ‘Advanced‘ tab and then click on ‘Environment Variables..‘.
  • Click on New button under ‘System Variables..‘
  • Write ‘JAVA_HOME‘ in the Variable name box and enter ‘C:\Program Files\Java\jdk1.8.0_20‘ JDK path in the Variable value box and click OK.
  • Entry for newly created Java variable will be displayed under ‘System Variables..‘

  1. Download Maven and Set up Maven Environment Variable

  • Next step is to download the Maven and it can be downloaded from below link : https://maven.apache.org/download.cgi.
  • Extract it to some location. Here it is extracted to ‘C:/apache-maven-3.2.3′.
  • Set up the Maven Environment Variable the same way we set up the Java Environment Variable above.
  • Write ‘MAVEN_HOME‘ in the Variable name box then enter ‘C:\apache-maven- 3.2.3‘ Maven path in the Variable value box and click OK.
  • Entry for newly created Maven variable will be displayed under ‘System Variables..‘

  1. Update the Path Variable
  2. To run Maven from command prompt, this is necessary to update the Path Variable with Maven’s installation ‘bin’ directory.

  • Go to My Computer and select Properties by Right Clicking on empty space in My Computer.
  • Click on ‘Change Settings‘.
  • A pop up window will display, click on ‘Advanced‘ tab and then click on ‘Environment Variables..‘.
  • Click on Edit button under ‘User Variables for UserName‘, where UserName is your machine name.
  • Write ‘PATH‘ in the Variable name box then enter ‘C:\apache-maven-3.2.3\bin’ Maven path in the Variable value box and click OK.

  1. Test the Maven Installation
  2. Maven installation is complete. Now lets test it from windows command prompt. Go to Run and
  3. type ‘cmd’ in application location search box. A new command prompt will be opened. Type mvn – version in command prompt.

Install Maven on Mac

Maven is a tool that is also available on Mac machine. Maven basics remain same across Windows and Mac machines.

Prerequisite: Java should be installed on the Mac machine.

  1. Download Maven binaries
  2. First download to Maven binaries. To do so click on the following link:?https://maven.apache.org/download.cgi?On the downloads page above click on the link apache- maven-3.3.3-bin.tar.gz link. Here is a small screenshot:
  3. On click on the link a .tar.gz file will be downloaded in the downloads folder. Download folder icon can be found on the upper right corner of the Safari browser window. As shown in the image below.
  4. Unzip the files
  5. Now go to the downloads folder and unzip the files there by double clicking on the downloaded file. Here the .tag.gz file that we have got is apache-maven-3.3.3-bin-2.tar. Here is what it will get when you will unzip/double click the folder
  6. Set the Path variable
  7. Like on a Windows system here also it has to set the path variable on a Mac machine too. Path variable will have to be updated to include the path on which you will copy all your Maven libraries. After unzipping the downloaded file just open the unzipped folder. You will find a few folders inside it. There will be a folder called bin, this folder will have a file called mvn. This is the file which is used to run Maven commands. File path should be mention in the Path variable of Mac system. Open the terminal window on the Mac machine. Once the terminal window up type in following command to see all the environment variable
  8. $ printenv
  9. To see only the path variable values type in command

$echo $PATH        

  1. Update the path variable to point to Maven’s bin folder.

$ export PATH=$PATH:/Users/viren/Documents/AllBins/apache-maven- 3.3.3/bin/        

  1. Here the PATH variable has updated with existing value of path + the new value of of Maven’s bin folder.
  2. Check the Installation

$ mvn -version        

There are two ways of creating Maven project. One by using command prompt and other from within the eclipse IDE.

The steps to create a New Maven Project from Command Prompt.

  1. Go to Run and type ‘cmd‘ to open Command Prompt.
  2. Browse to the folder where the project is set up and then type the below command:
  3. mvn archetype:generate -DgroupId=ToolsQA -DartifactId=DemoMavenProject – DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
  4. Note: ‘cd..’ is used for go back to previous folder and ‘cd foldername’ used for go in to the folder.
  5. Here ‘DartifactId‘ is the project name and ‘DarchetypeArtifactId‘ is the type of Maven project. There are diffirent types of maven projects like web project, java project etc.
  6. Once the Enter is press after typing the above command, it will start creating the Maven project.
  7. Note: In case of build failure, please check the Maven version number in the pom.xml file. It should match the version of Maven installed on the machine. Look for this in the POM
  8. ‘https://maven.apache.org/POM/3.2.3′. Correct the version from all the places it is mentioned in the POM.
  9. Go to the project location to see the newly created maven project. Now open the pom.xml file, which reside in project folder. By default the POM is generated like this:

<project xmlns="https://maven.apache.org/POM/3.2.3" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://maven.apache.org/POM/3.2.3 https://maven.apache.org/maven-v3_2_3.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ToolsQA</groupId>
<artifactId>DemoMavenProject</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>DemoMavenProject</name>
<url>https://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>        

  1. The default folder structure of the Maven project.
  2. Note: Test cases resides under the src > test > java > ToolsQA will only be considered as a test by Maven, rest will be ignored if test cases are put in some other folder.

The steps to create a New Maven Project from Eclipse

  1. Open command prompt and browse to the Maven project and type this ‘mvn eclipse:eclipse‘

The project is now compatible for Eclipse IDE.

Import Maven Project in to Eclipse

  • Open Eclipse and go to File > Import.
  • Click on Existing Projects in to Workspace.
  • Browse to Maven project folder and click on Finish.
  • Project Explorer will now look like this:
  • Modify the POM. The default pom.xml s too simple, often times it need to add the compiler plugin to tell Maven which JDK version to compile the project.

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>         

  • Update the jUnit from 3.8.1 to latest 4.11

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>        

  • Now the POM will look like this:

<project xmlns="https://maven.apache.org/POM/3.2.3" 
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://maven.apache.org/POM/3.2.3 
https://maven.apache.org/maven-v3_2_3.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ToolsQA</groupId>
<artifactId>DemoMavenProject</artifactId>
<packaging>jar</packaging>
 
<version>1.0-SNAPSHOT</version>
<name>DemoMavenProject</name>
<url>https://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>        

  • Once the POM has modified, do the Maven Project to work with Eclipse step
  • again. Open command prompt and browse to the Maven project and type this ‘mvn eclipse:eclipse‘

Run the first Maven Test

  1. Right click on the pom.xml and go to Run As > Maven test.
  2. In the console window of Eclipse, see the information like this:
  3. Go to ‘SureFire Reports‘ folder and open the xml file.
  4. It will display the result of the JUnit test.

How to Create a New Maven Project in Eclipse

This is the second method to create a Maven project. But in this rather than creating a project outside eclipse and then import in to eclipse, it will directly create a maven project in to eclipse.

  1. Open your eclipse and Go to File > New > Others.
  2. Select Maven Project and click on Next.
  3. Un-check the ‘Use default Workspace location‘ and with the help of Browse button choose the workspace where it would like to set up the Maven project.
  4. Select the archetype, for now just select the ‘maven-aechetype-quickstart‘ and click on Next.
  5. Specify the Group Id & Artifact Id and click on Finish.
  6. Note: Here the ‘artifactId‘ is the project name.
  7. Go to the project location to see the newly created maven project. Now open the pom.xml file, which reside in project folder. By default the POM is generated like this:

<project xmlns="https://maven.apache.org/POM/4.0.0" 
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>ToolsQA</groupId>
<artifactId>DemoMavenEclipseProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>DemoMavenEclipseProject</name>
<url>https://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF- 8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>        

  1. Look at the default folder structure of the Maven project.
  2. Note: Test cases resides under the src > test > java > PackageName will only be considered as a test by Maven, rest will be ignored if you put the test cases in some other folder.
  3. Modify xml with latest Junit and save the xml.

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>        

Run the first Maven Test

  1. Right click on the pom.xml and go to Run As > Maven test.
  2. In the console window of Eclipse, the information will be seen like this:
  3. Go to ‘SureFire Reports‘ folder and open the xml file.
  4. It will display the result of the JUnit test.

Configure Selenium Continuous Integration with Maven

Configure Selenium Continuous Integration with Maven project is not a big task. But making that project work with Maven is little tricky and that requires few configurations around the project. Once it done with that only then the test will be recognized by the Maven build system, else Maven will always ignore the test and perform the build. The complete list of steps are as followed:

  1. Convert Selenium project in to Maven Project
  2. Remove Errors from the Project
  3. Remove Associated Libraries from the Project Build Path
  4. Add Dependencies to the Maven Repository
  5. Add JDK to the Project Build Path
  6. Add Maven Dependencies to the Project Build Path
  7. Resolve Apache POI errors
  8. Create Maven Test folder structure
  9. Create a new TestNg Test
  10. Run the Test with Maven Build

Prerequisite for Converting a Selenium Project in to Maven Project

  • Java is Installed
  • Eclipse IDE is Installed
  • Maven is Installed
  • Dummy Selenium Project is Imported

  1. Convert a Selenium Project in to Maven Project
  2. Before moving on to this step, make sure that the dummy Selenium project has been imported. Importing a project in Eclipses simple, it just need to go on File menu > Import…. Then on the new window select General > Existing Projects in to Workspace and press Next. Browse the downloaded Selenium project.
  3. Once it is done with the import of the dummy project, the project explorer window will look like this.
  4. Right click on the Online Store folder and select Configure > Convert to Maven Project.
  5. It then ask to fill the Group ID and Artifact ID. Fill appropriate name and click on Finish button. Do not change the rest of the default settings.
  6. Eclipse will take few seconds to convert the project and once it is completed with the process, the project explorer window will look like this.
  7. Remove Errors from the Project
  8. It will be notice that the project now have quite a good number of errors, These errors are on throws statement because the try catch block is used in the methods but Exception is not declared in the method declaration. To resolve the errors add throws declaration in all the methods where ever the try catch statement is used.
  9. Remove Associated Libraries from the Project Build Path
  10. During the build, Maven only consider its own libs/repository and it will not consider the libraries available in the project build path. So the next step is to add all those libraries and dependencies which is required in the project. To figure out what all is required, just remove all the libraries from the build path and the project will end up with many more errors. Then it can be browse to every single error and one by one add
  11. Add Dependencies to the Maven Repository
  12. Dependencies are the libararies, which are required by the project. For example Log4j jars, Apache Poi jars, Selenium Jars.

  • Maven Local Repository
  • This is the place where Maven stores all the project jars files or libraries or dependencies. By default the folder name is ‘.m2‘ and by default the location in windows 7 is
  • ‘Libraries\Documents\.m2‘ or ‘C:\Users\yourusername\.m2‘.
  • Maven Central Repository
  • Maven central repository is the default location ‘https://mvnrepository.com/‘ for Maven to download all the project dependency libraries. For any library required in the project, Maven first look in to the .m2 folderof Local Repository, if it does not find the required libarary then it looks in Central Repository and download the libabry in to local repository.
  • Maven POM
  • Go to https://www.mvnrepository.com/ and search for Selenium Java.

Click on the Selenium Java from the search results.

Click on any version of the Selenium. it is allowed to choose any version here if in case it is specific to any project.

Take a look at the highlighted area on the below image. This highlighted details needs to be entered in the Maven pom.xml.

Double click on the pom.xml file which is at the bottom of the project explorer window. Select the Dependencies tab and then click on Add button.

A pop window will display. Enter the highlighted detail here which we copied from maven repository website, as per the below screen shot. Make sure that Group ID, Artifact ID & Version are exactly the same.

Now the Dependencies tab will look like this with Selenium Java added on it.

Now once the changes has been saved, right click at the empty space of the project explorer and select Refresh.

If it is choose ‘Run in Background‘, it will download the Selenium Java jar files in to

the Maven dependencies and the downloading process will take place at the background. If you choose ‘Details>>‘, it will display the downloading status bar and the process.

The same way it is required to add all the dependencies which are required in the project. Now add the Log4j dependency in the POM file.

Add TestNGdependency to the POM file.

Go to ExcelUtils class under the utility package.

The SurefirePlugin is used during the test phase of the build life cycle to execute the unit tests of an application. It generates reports in 2 different file formats like plain text file, xml files and html files as well. Even if TestNGor Junits framework is using for reporting, this plugin is must to use, as it helps Maven to identify tests.

  1. Add JDK to the Project Build Path
  2. Right Click on the project explorer and select Build Path > Configure Build Path. Select the Libraries tab and click on Add Library
  3. Select JRE System Library and click on Next button.
  4. Select ‘Workspace default JRE(jdk XXXXX)‘ radio button and click on Finish button.
  5. Add Maven Dependencies to the Project Build Path
  6. When a new Maven project has created, dependencies are added by default, so Maven Dependencies can be seen under the project explorer window. But when the Selenium project is convert to the Maven Project, maven dependencies needs to be added on to the project build path. So far it has just added the dependencies in the maven repository, now it need to link that repository to the project.
  7. Right Click on the project explorer and select Build Path > Configure Build Path. Select the Libraries tab.
  8. Click on Add Library…
  9. Select Maven Managed Dependencies and click on Next.
  10. Take a look over the project explorer window, Maven Dependencies can be seen.
  11. Resolve Apache POI Errors
  12. There is a little change in the current version of Apache POI which is that they have changed the XSSF in to HSSF. IF the ExcelUtils class of utility package will be seen, then may be many errors will be there around it.
  13. Just change all the XSSF entries in to HSSF for the sheet, workbook, celland row.
  14. Now by bringing the cursor over the each of the keyword, import the relevant packages.
  15. Project explorer window will now look like this, as now the Seleniumproject is completely error free.
  16. Create the Maven Test folder structure
  17. Maven has its own project folder structure and it is advisable to follow its default folder structure. For example all the test of the project should be in src > Test > Java folder. Lets just create the same for this project now.
  18. Right click on the src folder and select New > Folder.
  19. Give the folder name as ‘test/java‘ and click on Finish button.
  20. Create a TestNG test for the Selenium Maven Project
  21. Either a new test can be created or cut paste the existing tests to this src/test/java folder. The prerequisite for this step is that the TestNG should be installed in the Eclipse.
  22. Right click on the test/java folder and Select New > Other. A new window will display and then select TestNG Class from it.
  23. Enter Class name of any choice, but for now take it as NewTest. Now select any of the Annotations as well. Click on Finish button.
  24. Project explorer and the Eclipse window will look like this now.
  25. Run the Selenium Test with Maven Build
  26. Right click on the pom.xml and Select Run As > Maven Test.
  27. Once the run is complete, the result will be displayed in the console window of the Eclipse IDE.

Summary

  • Maven is a simple build automation tool which is basically used for enterprise Java projects, designed to take much of the hard work out of the build process
  • The JARs/WARs of any maven project can be shared across any distributed environments
  • The Surefire Plugin is used during the test phase of the build lifecycle to execute the unit tests of an application
  • POM is Project Object Model XML file that contains information about the project and configuration details used by Maven to build the project
  • Maven is a build and dependency management tool for java based application development. Just like other java based development tools, it is not installed as windows service, rather it is configured using windows environment variables


要查看或添加评论,请登录

社区洞察

其他会员也浏览了