Tuesday, 8 April 2014

MAVEN

Maven is a tool which is used for building and managing the project.

Why to mavenize a selenium project ??


Suppose in your current selenium project, you are using selenium jars let us say 2.3 version and latest selenium say 2.4 released. If your project is not mavenize , then you need to manually delete all the selenium 2.3 version jars and download all the 2.4 version and need to add it to your project. This is very difficult to maintain. If your project is mavenize, and a new selenium jar files released then apache deploys the latest jar files on to the Maven Centralized repository and you can to connect to the server and get the latest jar files.
This is the basic concept of Maven.
       Maven contains the POM(Project Object Model) files. POM is an XML file. It contains all project sources such as selenium code , dependencies (external jar files). It should exists in the root directory of the project.

Download and Configure Maven

  1.  Download Maven binary zip from the link http://maven.apache.org/download.cgi?Preferred=ftp://mirror.reverse.net/pub/apache/ 
  2. Unzip the Maven and copy to C:\apache-Maven folder
  3. Create a new Environment variable called MVN_HOME that points to the MAVEN  folder (C:\apache-Maven folder\apache-maven-3.1.1
  4. Now select the path and click on 'Edit' button.
  5. Now give the semicolon (;) at the end and add Maven path up to bin folder (C:\apache-Maven folder\apache-maven-3.1.1\bin)
  6. Click on OK
  7. Let us cross check if MAVEN is installed or not
  8. Open the command prompt
  9. Enter 'mvn -version' and hit enter
  10. Message should be displayed like "Apache Maven 3.1.1 <07286....... etc>"  

 Maven Project Structure 

While making a maven project and based on artifact you select(i selected default artifact), the structure will be as follows. Normally developers will write source code in src/main/java and unit cases will written in src/test/java.



Maven build Process(Build Life Cycle)

  1. Validate - Will validate the structure of project
  2. Compile - Will compile the source code and generate the class files in target folder
  3. test - Will execute the test cases in the test folder and generate the class files in target/test-classes folder.It will also generate the reports in target/surefire-reports
  4. Package - Will make source code as jar/war file based on what we specified in POM.XML
  5. Install - Install the jar/war(artifact) into local maven repository(.m2)
  6. Deploy - Will publish the artifact into remote machine


Important note:
  1. From command line when you fire the command mvn compile, then it will validate and then compile
  2. From the command line when you fire the command mvn package , then it will validate , compile , test and then package it. Similar way happens for install and deploy.


 Run testNg.xml from Maven

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>testing</groupId>
    <artifactId>mavenProject</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>Maven Quick Start Archetype</name>
    <url>http://maven.apache.org</url>

    <dependencies>      
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-server</artifactId>
            <version>2.41.0</version>
        </dependency>

        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.8.8</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.10</version>
                <configuration>
                    <suiteXmlFiles>
                        <suiteXmlFile>testng1.xml</suiteXmlFile>
                    </suiteXmlFiles>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Surefire plug-in is used to execute the test cases in src/test folder. To run above pom.xml. Open the command prompt and navigate to the directory where pom.xml exists and fire the command mvn test.


Executing ANT Targets from Maven

ANT "build.xml" :

<project name="test" basedir=".">

    <property name="jars" location="${user.home}/.m2/repository"></property>

    <path id="jars">
        <fileset dir="${jars}" includes="**/*.jar">
        </fileset>
    </path>
   
    <target name="makeDir">
        <delete dir="zetendra"></delete>
        <mkdir dir="zetendra"/>
        <javac srcdir="sourceDirecotry" destdir="zetendra" classpathref="jars"></javac>
    </target>
   
</project>

Now create a "anyName.bat" file with the ant target inside it and place it inside the project folder parallel to 'build.xml'.

My "start.bat" file is as follows :



Maven "POM.xml"

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>testing</groupId>
    <artifactId>mavenProject</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>Maven Quick Start Archetype</name>
    <url>http://maven.apache.org</url>

    <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-server</artifactId>
            <version>2.41.0</version>
        </dependency>

        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.8.8</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.7</version>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <configuration>
                            <tasks>
                                <echo message="${basedir}"></echo>
                                <ant antfile="build.xml"></ant>
                                <exec dir="${basedir}" executable="${basedir}/start.bat"
                                    failonerror="true">
                                </exec>
                            </tasks>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
   
</project>

No comments:

Post a Comment