Maven & GitHub & Jenkins & TestNG Integration – CI Automation
Maven & GitHub & Jenkins & TestNG Integration – CI Automation
Skip to end of metadataGo to start of metadata
Last updated: Sep 14, 2017 14:06
Build a maven project in eclipse, including a pom file that contains the build process and project dependencies. Configure a new job in jenkins. Setup github and create a git repository, enabling Jenkins to retrieve the repository files to run your test.
Maven Overview
In short, Maven helps developers manage all project dependencies and ensure an easy build process.In addition, Gradle is an extension of maven and works extremely well. Newer developers tend to prefer Gradle over Maven. Check out the perfecto Gradle article listed below:
1) Gradle Complete Setup Guide
2) Gradle Jenkins Setup
Maven vs Gradle
https://gradle.org/maven_vs_gradle/
Maven objectives include:
Providing a uniform build system
Providing quality project information
Providing guidelines for best practices development
Allowing transparent migration to new features
This post includes:
Building a Maven project
POM file
– build process
– dependencies
Installing Jars
Testing POM file
Settings.xml file
Github setup
Git repository
Jenkins configuration
Setting up Jenkins as a windows service
Creating a job in Jenkins
Building a Maven Project in Eclipse
https://maven.apache.org/run-maven/index.html
Step 1: Create a new Maven project in eclipse.
Figure 1: The src/main/java and src/test/java folders are created and are used for the Maven project
New Maven Eclipse project – main and test directories
Step 2: Configure the pom.xml file, that was automatically created, to include the Perfecto Mobile plugin jars to the build process. It can be found in the root of the Maven project.
Add the below maven dependency to allow maven to download the necessary jars for your project.
Step 3: Add a suiteXmlFile for each specific test case in the properties section of the pom.xml file.
TestNG & JUnit Dependency
Adding TestNG/JUnit jars to the dependency will enable TestNG/JUnit in your project. These are open source automation tools which will help aid in automating scripts.
Maven TestNG Build Process
The below build process will build your Maven project and run the base testng.xml during a Maven test.
Once you have setup Maven and downloaded all the corresponding jars, right click on the project and go to: run > Maven Clean and then try install & build.
If you’re able to build/clean/install your Maven projects then you have successfully installed/configured Maven.
Maven Install Jars that are not included in the Central Repo
Maven creates an .m2 folder under your system username/.m2/repository. This folder contains all the downloaded jar files needed to compile and run your project.
Note: maven also will need a settings.xml file that will be placed in the same directory. Refer to maven settings.xml section to configure this settings.xml.
maven .m2 repo
To install jars that are not included in your project, you will need to navigate to where your apache maven is installed, navigate to the bin folder, and run the below cmd line with administrative privileges. This will install the needed jar files in the .m2 folder.
** Click here for a Guide to installing 3rd party JARs. Now you are ready to run a Maven project in Eclipse.
mvn install:install-file -Dfile=C:\Users\rajp\git\PerfectoMobileJars\PerfectoMobileJars\stagingRepo\selenium-remote-driver-2.46.0.jar -DgroupId=com.perfectomobile -DartifactId=pm-webdriver -Dversion=3.14.0.9 -Dpackaging=jar
Maven install external jar files
Setting up windows environment variables
The following variables must be set prior to setting up Jenkins. The user will need to provide the below path locations. The default locations for the files are shown below:
Git System/User Variables:
Variable name: GIT_HOME
Variable value: c:\Program Files\Git\bin\git.exe
Java System/User Variables:
Variable name: JAVA_HOME
Variable value: c:\Program Files\Java\jdk1.8.0_45
JRE System/User Variables
Variable name: JRE_HOME
Variable value: c:\Program Files\Java\jre1.8.0_45
Maven Repo System/User Variables
Variable name: M2_REPO
Variable value: c:\Users\{your_username} \.m2
Maven Home System/User Variables
Variable name: MAVEN_HOME
Variable value: c:\eclipse\apache-maven-3.3.3\
Setting up Maven Settings.xml file
This file must be added to the external third party jars if they are not available in your maven central repository. Maven will first search for these jars in the Maven central repository, if they are not found, it will search in the below URL and download them. You can also use the cloud bees URL that will download the corresponding jars in the following URL: http://repository-perfectomobile.forge.cloudbees.com/public/com/. In addition, in the below settings.xml, a user can access the profile by -PST as the ST is the “id”.
Note that you will need to increase your memory size depending on the jar file you are downloading.
\.m2\repository
Maven Running your POM & Command Line
To run your pom.xml, right click on it and select Run As > Run Configurations. Specify the test case name and provide “test” as your goal so Maven will your test case as a test.
Running a maven project
Setup a custom Maven run configuration profile.
Setup a custom Maven run configuration profile
** Click here for examples of how to run Maven via command line.
Github Setup
** Refer to the GitHub Setup for detailed description on setting up a new Git project.
Step 1:Right Click on the project > team > share Project > github, see below screen shot.
Setting up GitHub – sharing a project
Step 2:Select Git hub project and provide a new “gitHubProjectName” name > Finish.
Step 3: This will create a project and local Git repository.
Step 4:Add the Git Repositories & the Git Staging
This will create a local Git Repository, where you can check-in your first code change and create a local master copy of your project.
** Remember! This will only check-in your code into your local workstation.
Step 5: Create a Remote push/pull GitHub setup.
Before we create a GitHub repository, we need to create a GitHub account and new GitHub repository. Once that’s created we can use the .git url to create a remote origin as shown below.
Right click on your remote and click create new remote. Leave origin as show below and click OK.
Type your git url and user/pass and click done. This will create a remote git repo where you will be able to push your code to the git repo.
Configuring Jenkins for a Maven Project
Important Notice!
There are no public Jenkins servers, you will need to install your own Jenkins server that requires a dedicated machine that Jenkins will run on.
** Refer to the Jenkins setup as a service post to setup Jenkins as a windows service. Setting up Jenkins as a windows service provides a dedicated machine that will always be available to use by your team.
To configure Jenkins for a Maven project, load Jenkins and go to Manage Jenkins > Configure System and setup all the properties as shown below.
Maven/JDK installation and Git can be installed automatically by checking Install automatically. This will allow Jenkins to download the appropriate files needed.
Creating new jobs in Jenkins
Once we have setup Jenkins configuration, let’s create a new job in Jenkins by selecting new item and create a new maven project.
GitHub project: https://github.com/{username}/MavenJenkins.git/
Source Code Management: https://github.com/{username}/MavenJenkins.git/
Prerequisite: Install the GitHub Jenkins plugin before continuing.
Set the Root POM to the projectName/pom.xml, where the pom.xml is located in your project as shown below. Set the Goals and Options to test –DskipTests=false as shown below. This will run your pom file which will reference the base directory testng.xml file test case. This can be configured on the fly depending on what test case you would like to run.
source: https://developers.perfectomobile.com/pages/viewpage.action?pageId=21435209
0 comments