How to Run maven project from Jenkins (Continuous Integration Tool)
In this article i will talk about how to run maven project from jenkins (continuous Integration Tool). POM.xml needs to be configured in jenkins. Will talk in detail about this.
In this article i will talk about how to run maven project from jenkins (continuous Integration Tool). POM.xml needs to be configured in jenkins. Will talk in detail about this.
In this article i will talk about how to run testng.xml from maven’s POM.xml file. This is very useful feature of maven when we need to run multiple testng.xml files.
The very first thing is, install maven in your eclipse IDE. Refer below article to know about how to install maven in eclipse ide.
Now, create a maven project. Refer below article to understand how to create new project in eclipse IDE.
Now, write one sample test case in one of the folder created by maven, preferably test folder. I have created below sample code –
package kdf1; import java.util.concurrent.TimeUnit; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.Test; public class Sample { @Test public void test(){ System.out.println("Test Print"); System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.navigate().to("http://demowebshop.tricentis.com/"); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); } }
Now, create testng.xml file for executing above test case. My testng.xml files looks like –
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Default suite"> <test verbose="2" name="Default test"> <classes> <class name="kdf1.Sample"/> </classes> </test> <!-- Default test --> </suite> <!-- Default suite -->
Now, create a POM.xml file, add required dependencies. My POM.xml looks like –
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com</groupId> <artifactId>kdf1</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>2.53.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.testng/testng --> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.9.9</version> <scope>test</scope> </dependency> </dependencies> </project>
Now, My job is to execute above given testng.xml file from above created POM.xml file via maven commands.
To achieve this, we need to use Suite XML files. Add below lines of code in POM.xml under <build> tags.
Make sure that you give the full testng.xml path.
<plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.20.1</version> <configuration> <suiteXmlFiles> <suiteXmlFile>testng.xml</suiteXmlFile> </suiteXmlFiles> </configuration> </plugin> </plugins>
After adding above piece of code in to my POM.xml (under <build> tags), my POM.xml looks like –
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com</groupId> <artifactId>kdf1</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>2.53.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.testng/testng --> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.9.9</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.20.1</version> <configuration> <suiteXmlFiles> <suiteXmlFile>C:\Users\Prakash\workspace\kdf1\testng.xml</suiteXmlFile> </suiteXmlFiles> </configuration> </plugin> </plugins> </build> </project>
Thats it, you are done.
You are good to execute your testng.xml file from maven’s POM.xml. The main advantage of using this is we can execute multiple testng.xml files.
You can execute the project now by right clicking on project, Run As –> Maven Test (another way to execute test case is from command prompt).
This was all about how to run testng.xml from pom.xml.
To know more about how to execute maven commands from command prompt, refer below link –
Hope this helps !!!!! Do provide your valuable comments !!!
In this article i will talk about maven build life cycle, that is maven build process.
In my last tutorial. i talked about maven introduction, how to install maven & other things.
Okay, so let us talk about maven build life cycle. Maven build process is via command prompt or in eclipse IDE.
Every build follows the life cycle (Write code -> compile -> run test cases -> package -> install). In the similar way maven has its own build life cycle.
Maven commands can be run via command prompt or can be via eclipse IDE.
Once you have created a maven project & updated you POM.xml with required testng.xml (watch below video to understand how), we can run maven project from eclipse ide. refer below screenshot.
You can right click on maven project & then click on “Run As”, You will find multiple options to run your maven projects. Will talks about multiple options in below few para.
Once your maven project is ready, Go to command prompt & go to the directory where your maven project is located at.
My maven project is present at – C:\Users\Prakash\workspace\kdf1
So, go to this location & enter the maven command here. Execution should start. Refer below screenshot.
So, above is the way how to execute maven commands.
. . . . . there can be few more commands as well . . . .
This is all about maven build life cycle.
Do post you comments / improvement suggestions. Thanks !!!!!
Maven is basically a project management tool, where we manage our projects, we can build projects, deploy projects & many more features like reporting & documentation. It refers concept of Project Object Model (POM). In a short term, it is a build tool.
If you are using build tool like mavn, it simplifies your build process. everything becomes very simple because it follows some life-cycle what it has. we will talk about mavn life-cycle in couple of next para.
Earlier project who does not have mavn, they need to add jar files manually & if multiple persons are working on the same project, they have to use same jar files. If one has changed, it cause issues. Also earlier to mavn, we used to create project folder structure by our own. Where as after mavn, this is not manually required, because –
1. In case of maven project, you need not add any jar files in to the project. just add all your dependency in POM.xml file
2. Maven project creates a very good project folder structure for coding purpose.
As i said earlier, maven is a build tool, now let us try and understand what us build tool.
Build tool has a set of processes, certain outcomes like compile source code, copying resources, compiling and running tests, packaging project(in jar or zip file based on selected option), deploying project & finally cleanup. So when you compile your source code using maven, it will create a jar or war file for further use.
All settings of maven project are POM.xml file
POM is an xml file that contain an information about project and configuration details used by mavn to build project. Information is about name, version, artifact it, dependencies, plugin information & profiles. We will talk about POM.xml in more details in coming sessions.
For the person building a project, this means that it is only necessary to learn a small set of commands to build any Maven project, and the POM will ensure they get the results they desired.
There are three built-in build lifecycles: default, clean and site. The default lifecycle handles your project deployment, the clean lifecycle handles project cleaning, while the site lifecycle handles the creation of your project’s site documentation.
Each of these build lifecycles is defined by a different list of build phases, wherein a build phase represents a stage in the lifecycle.
For example, the default lifecycle comprises of the following phases (for a complete list of the lifecycle phases, refer to the Lifecycle Reference):
These lifecycle phases (plus the other lifecycle phases not shown here) are executed sequentially to complete the default lifecycle. Given the lifecycle phases above, this means that when the default lifecycle is used, Maven will first validate the project, then will try to compile the sources, run those against the tests, package the binaries (e.g. jar), run integration tests against that package, verify the integration tests, install the verified package to the local repository, then deploy the installed package to a remote repository.
I will talk about maven in more detail in next articles. Refer below links –
In this article i will talk about how to install maven in eclipse IDE. Maven is basically a build tool for java based projects.
There are two ways to install maven in eclipse IDE. First one is to install directly in Eclipse IDE via Eclipse MarketPlace / Install new software from help menu of Eclipse IDE & second one is, Install it on your machine manually & then configure the installation details in Eclipse IDE.
Let us talk about both methods.
Follow below steps one by one
Open Eclipse, Go to Help Menu on Top right hand side & click on install new software.
Add depository for downloading M2e as shown in below image.
Then select work with as the current URL that you have added. It will show you the list of Maven Integration tools for Eclipse IDE. Select the desired one & click on next. Shown in below image
Click on the next, below screen will show you the current installation items that you have selected.
Now accept the Terms and conditions of license agreement & click on finish button so as to start with installation of maven plugin inside eclipse IDE. Shown in below image.
Finally, You can see that software installation has begin. Shown in below image
Once installation is finished, do restart your machine.
This is how we can install maven inside Eclipse IDE.
Follow below steps to get the installation done using above method.
Download Maven files from : https://maven.apache.org/download.cgi
or click here apache-maven-3.5.2-bin.zip
Then Extract it to some folder. Let us say i am extracting it in my C:/ drive. So path of maven files is : C:\apache-maven-3.5.2
Now, Setup Environment Variables.
Go to start program, type “env“, in search result you can find option to edit environment variables. Click on it. Refer below image –
Once you click on it, System properties will open. Click on Environment variables. As shown in below image.
Now add below variables by clicking on new button for system variables.
JAVA_HOME=C:\Program Files\Java\jdk1.8.0_111
M2_HOME=C:\apache-maven-3.5.2
M2=%M2_HOME%\bin
Once above variable setup is done, Edit the “Path” variable. Enter below path at the end of current path (Do not modify anything other than this unless you are sure about it)
Path=;%M2%;%JAVA_HOME%\bin
Once this is done, click on OK. Now you are done with environment variable setup. Now you can verify if maven is installed correctly on your machine or not.
To check this, open command prompt & type below command.
mvn -version
Result of above command should show the currently installed maven version (This means maven is installed correctly) Refer below image.
Before we update anything in Eclipse IDE, first we need to modify setting.xml file from maven installation directory. Here we need to add the path of current maven repository at correct position. refer below image.
Location of settings.xml file: C:\apache-maven-3.5.2\conf\settings.xml
In the above image, the highlighted line need to be inserted at correct position, as shown above. Save the file.
Now let us update Eclipse IDE. To do this, Click on Window & go to preferences of your Eclipse IDE.
Go to maven –> Installations. Window will looks like below.
Now click on Add, and add the path of your current maven directory here & Click on OK. refer below image.
Now go to user Settings in the same menu & add path of your settings.xml file (Located in conf folder of your maven installation directory)
Click on OK.
That’s it, now done.
Clean your project & start with new maven project. You should be good to start with creation of maven project.
Refer below article to know about how to create new maven project.
Put your comments if you have any issues !!!! Thanks !!