This tutorial will explain all about Jenkins & Integration of Maven with Jenkins. You will also learn to Create Documentation for the Project using Maven:
We will get to know the what is Jenkins, Maven Jenkins Integration, advantages of Jenkins, how to set up Java and Maven in Jenkins, how to create Jenkins job with different parameters, execution result reports of Jenkins with TestNG plugins, how to pass Maven commands in the form of Goals in Jenkins, Maven documentation for projects, etc in detail.
=> Watch Out The Simple Maven Training Series Here.
Table of Contents:
What Is Jenkins?
Jenkins is a continuous integration tool that has immense importance in software testing and development world. It is open-source and platform-independent. In our day to day work, we deal with automation frameworks comprising of several hundreds of test cases.
Till now, we have seen that for executing our scripts from the Maven project we either have to run Maven commands from the command prompt or directly open Eclipse and trigger the scripts.
So we are manually triggering the test cases for execution. In order to automate this process, we need to schedule jobs that shall run our test cases at a particular time and also take care of some important parameters. This whole process is automated with the help of Jenkins.
Thus in reality, if we have around 800 test cases that need to be executed at three in the morning, then it can be done easily without requiring any manual intervention with the help of Jenkin jobs.
Also, there are scenarios when automated Smoke or Sanity test cases need to be executed after there is some code check-in from the development side in the environment. All these are taken care of automatically by Jenkins.
Jenkins has the log of all the modifications done in repositories like SVN or Git. Jenkins can be easily installed in our system and configured. Also, it can be set up on more than one virtual machine if builds need to be triggered in a distributed manner. Moreover, it gives access through links to all the recent builds.
Jenkins has features like integration with TestNG with the help of plugins, email notifications on build status which make it more popular to use nowadays. Issues and bugs get caught and resolved very early.
A majority of the integration tasks are performed automatically, thereby saving resources and enabling the software to get released any time without issues.
In the Agile environment, we have business analysts, developers, and testers working together. As a result, there may be a case that a business analyst (a non-technical person) may need to see and monitor how the execution result is.
In such cases, he may not need to open an Eclipse or set up and run Maven commands to trigger the scripts. He can execute the test cases with the help of Jenkins and get the results of execution.
We are in the Jenkins home page with a job name as Project. If a business wants to see the results of the execution, then the job name needs to be clicked and it will redirect to the below page.
Then Build Now on the left of the screen needs to be clicked. Thus we just need to open the Jenkins URL (https://localhost:8080/jenkins).
While test cases are triggered via Jenkins, it is only the specific environment that is provided in Jenkins where the test cases get executed. It is a common scenario that if we are executing our test cases outside Jenkins, then they are getting failed due to the version of our tools or language or jars not aligned to one what it is supposed to work.
For example, the Java version or our Maven version got updated or downgraded. All these can be avoided if we are using Jenkin’s jobs for our execution.
Thus Jenkins has major contributions in our testing framework by maintaining a centralized set up across all geographical locations.
Advantages of Jenkins
The advantages of Jenkins are listed below:
- Jenkins is an open-source, platform-independent and portable.
- It can be installed and configured easily. It can be used with ease.
- Jenkins has automated the integration items and so many of the integration issues do not occur, thereby increasing the efficiency and productivity of developers.
- Its documentation is described well and makes it convenient to learn it.
- Jenkins was developed by the developers by keeping in mind the features that the developers actually need in work.
- It has a large number of plugins. All these plugins make Jenkins more powerful and popular among the developers.
- Jenkins assists the environments hosted on the cloud.
- It has a big community of users who are easily accessible. They work on feedbacks and touch base regularly.
- Jenkins provides aid to test automation in the following ways:
- Feature of test execution scheduling: Test professionals can schedule their test cases for execution and utilize their time for other tasks.
- Customized Test Results Report: Jenkins gives details on the pattern of results. The graphical representations and details of the execution results reflect on the total number of test cases, execution timings, and other important information.
- Works properly with the continuous integration processes: Testing time gets reduced with Jenkins.
Java And Maven Path Set In Jenkins
First of all, we need to have Java and Maven installed in our machines. Once we are on the landing page of Jenkins, initially there will be no jobs.
Now click on Manage Jenkins link from the left of the screen.
Then click on Global Tool Configurations. Here we have to do the configurations for Java and Maven. For Java set up, click on the JDK Installations button.
Here we have to either give Name and JAVA_HOME path or this can be taken care of by checking Install automatically checkbox.
Next, we need to move to the Maven section and click on the Maven Installations button.
Here we have to either give Name and MAVEN_HOME path or this can be taken care of by checking the Install automatically checkbox.
After performing these operations, click on Save at the bottom of the page.
Create A Jenkins Job
Next, we need to generate a job for triggering the scripts of our Maven project. Click on the New Item link from the left of the screen.
Enter an Item Name and select the Freestyle project option then click on OK.
We will be navigated to the job configuration page. Here we need to provide all the information specific to our project. At first, we need to give the source of our project code.
Jenkins Source Code Management
In the Source Code Management section, we generally select either from the GitHub or Subversion and provide the corresponding links. Jenkins will automatically obtain the code from there.
However, if our code is present in our local system, we need to click on the Advanced button under the General section, and then check Use custom workspace.
We need to place our Maven project inside the .jenkins folder and give the path of the pom file residing inside the Maven project starting with ${JENKINS_HOME}.
In this manner, Jenkins gets hold of the project source code.
Jenkins is not only used by testers for triggering the test execution but is also used extensively by developers for their work. Some of these options are provided under the Build Environment section.
Next, we need to move to the Build section and select Invoke top-level Maven target options from the dropdown.
Then Invoke top-level Maven targets section will be expanded. Here Maven Version will be pre-populated from the Maven version we have defined in the Global Tool Configuration.
Under Goals, we have to provide the Maven command to trigger the execution. mvn test –PRegression. In Jenkins, we can omit the term mvn and the rest of the command is sufficient.
Click on Save and we have successfully created a job.
Just a recap on what we have discussed so far:
- Location of our code (either Git, Subversions, or from a local directory).
- Maven command to trigger execution.
Our job will be created and will be visible on the Jenkins landing page:
Click on the Build Now from the left of the screen. There will be a Build History section on the left of the screen visible after the build is done. The build number will be visibly followed by # along with the time stamp. Now navigate to the Console Output.
The results of the execution:
TestNG Jenkins Integration For Execution Results Report
For the test execution result details, navigate to the Maven project in Eclipse, move to the target folder then to the surefire-reports folder. There is an index.html file inside the surefire-reports. Get the location of the file and open it on the browser.
testng-results.xml file and TestNG reports plugin is required for results in Jenkins.
Click on Manage Jenkins link from the left of the Jenkins screen. Then click on Manage Plugins link.
Install the TestNG Results Plugin.
Once done, we shall update the job we have created from the Configure link from the left of the screen. Then navigate to the Post-build Actions section and select Publish TestNG Results from the dropdown.
testng-results.xml file in our Maven Project gets automatically populated in the TestNG XML report pattern field by default. Then click on Save.
Job Scheduling In Jenkins
Navigate to the Build Triggers section for scheduling the build timings.
If we select Build periodically, then we will have the Schedule section visible. Here we have to mention five parameters to schedule the job as shown below.
- MINUTES in one hour
- HOURS in one day
- DAYMONTH day in a month
- MONTH in a year
- DAYWEEK day of week
To schedule the scripts at 8:00 AM in the morning we need to specify as shown below:
Now we shall trigger the build again, to verify the TestNG results. Go to the landing page of Jenkins and click on the job. We shall now have a TestNG Results link inside our job screen.
In case the execution does not have any failures, the Latest Test Result link will show no failures. Click on the TestNG Results link to get more details on results.
We can click on the build # number for more details on the results.
To check our code, we should click on the Workspace link to the left of the screen. In this way, Jenkins and Maven can work together.
Maven Documentation For Project
We can create documentation for our project with the help of Maven. As discussed earlier, Maven comprises three lifecycles i.e. clean, site, and default.
The task of removing any errors and removing the artifacts of the previous build is taken care of in the clean cycle. While the default is in charge of the total deployment process, the site lifecycle is for creating the project documentation.
It is considered a good practice, to perform clean before the site so that we have an up to date documentation.
Navigate to the project folder and run the following command from the command prompt.
mvn site
The process of building the project will start. After the success of the build, the project documentation shall be generated as shown below.
On clicking the index.html file, we can have detailed documentation of the project mainly consisting of the build and project information, about the project, continuous integration, dependencies, mailing list, licenses, and so on.
FAQs Section
Q #1) How does Jenkins work with Maven?
Answer: Jenkins works well with Maven by following certain preconditions. We need to set up Java and Maven paths in Jenkins. Then we need to create the Jenkins jobs with all the parameters including Post Build Actions, scheduling the builds, and so on.
Q #2) What are Maven and Jenkins?
Answer: Maven is a project management and builds tool developed in Java. Jenkins is a continuous integration tool that is open source. Both Maven and Jenkins are widely used in the industry nowadays.
Q #3) What is the Maven goal in Jenkins?
Answer: We provide the Maven commands inside the Goals section of Jenkins. This is a key step while creating a Jenkins job.
Q #4) Why do we use Maven?
Answer: Maven is a project management and build tool that is driven by a pom.xml file. It has made the build process simple, easy, and consistent.
Q #5) Is Maven a CI Tool?
Answer: No, Maven is not a continuous Integration tool. It is a build tool having more features than Ant. Jenkins is a continuous integration tool.
Q #6) What is pom xml in Maven?
Answer: Project Object Model or pom is an xml file that forms the core of Maven. It contains the list of project dependencies, configurations, profiles, and so on.
Q #7) Can we integrate TestNG and Jenkins with Maven?
Answer: Yes, we can integrate TestNG and Jenkins with Maven. All these are achieved with the help of plugins.
Q #8) What are the three builds of the Maven life cycle?
Answer: The three standard builds of the Maven cycle are listed below.
- Default
- Clean
- Site
Q #9) How to set Jenkins settings XML?
Answer: In Jenkins, navigate to the Build section of the job, then click on Advanced. Settings File option will be visible there. The location of the settings.xml is to be specified there.
Q #10) How do I set environmental variables in Jenkins?
Answer: The environmental variables in Jenkins can be set from the Configure screen. In the Build section, select Inject environment variables. There we need to provide the environment variable like VARIABLE_NAME=VALUE pattern.
Conclusion
We hope a large portion of Maven like its integration with Jenkins, TestNG plugins for Jenkins to display results, job creation on Jenkins, Java, and Maven set up in Jenkins, global configurations on Jenkins should be easily be understood now.
Also, Jenkins build schedule mechanisms, its post-build actions, how Jenkins deals with the code residing in Git, or any other repository or in locally has been illustrated in detail here. Practice each step one by one and Maven Jenkins fusion will be clear. We have also explored Maven documentation for projects.
Jenkins, a continuous integration tool, has a real use for developers, testers, business analysts, and so on in an agile world. Each organization looks up to use and adapt continuous integration processes every now and then.
=> Visit Here To See The Maven Training Series For All.