Where we learn technology

Category: Maven

Steps to Configure Maven Profile with Maven Project

Let us see how to configure Maven Profile for a Maven Project.

What is a Maven Profile..??                                                               

It is a feature that can be defined in a pom.xml file which allows developers and testers to configure and customize builds based on requirement. 

What is the advantage of implementing Maven Profile in our Automation Projects..??                                                                 

As shown below, In real time we generally maintain different testng.xml files in frameworks that are designed either for Web Applications, Backend APIs or Mobile Applications like Smoke Suite, Regression Suite, Sanity Suite etc. for executing test cases based on requirement.             

Based on business requirement, we will be required to execute testng.xml files from Eclipse IDE, Command Prompt or from Jenkins etc.

In general, if we do not use Maven Profiling. Each time when we need to execute testng.xml file. We need to go to pom.xml file and change the name of testng.xml inside Maven Surefire Plugin which needs to be executed and that is a very tedious task to do every time.  Now the question is, how can we avoid changing testng.xml file name in pom.xml every time. when we need to execute test suite.

Now let us see configuring Maven Profile with Maven Project.

1. Project should be a Maven Project. 

2. Maven Jar files should be downloaded and path must be set for the same in Environment Variables and ensure that you are able to execute Maven Project from Command Line prompt before implementing Maven Profile. 

Refer below blog with respect to above points to Install Maven Jars and to execute Maven Project from Command Prompt:  http://naveenautomationlabs.com/executing-maven-project-in-windows-from-command-prompt/

Below is Maven Surefire Plugin where we mention which testng.xml file needs to be executed from project.

Code for Maven Surefire Plugin:                                                     

<build>
 <pluginManagement>
 <plugins>
 <plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-surefire-plugin</artifactId>
 <version>2.18.1</version>
 <configuration>
 <suiteXmlFiles>
 <suiteXmlFile>src/main/resources/testng_smoke.xml</suiteXmlFile>
 </suiteXmlFiles>
 </configuration>
 </plugin>
 </plugins>
 </pluginManagement>
 </build>

Now, let us see how can we build Maven Profile using Maven Surefire Plugin.

We can build Maven Profile for each testng.xml file in pom.xml as shown below, for each Maven Profile we can give Id/Name as highlighted in below screenshot.

All we need to add is followed tags with Maven Surefire Plugin <profiles> </profiles>, <profile> </profile> and <id> </id>.

All we need to do is that providing Id/Name for each Profile. For example, if we have multiple testng.xml files like Smoke, Sanity and Regression then we can build 3 Maven Profiles.       

One-One Profile for each suite, so that we can avoid changing testng.xml file name inside Maven Surefire Plugin whenever we need to execute test suite.

In my case, I have 2 testng.xml files. One is for Smoke and other one is for Regression.

You can refer below screenshots for giving Maven Profile Names for Test Suites.

Note: Profile Name should always be written inside <id> tag.

Refer below pom.xml to build Maven Profile for each testng.xml file.    https://github.com/PavanReddy77/MavenSurefirePlugin/blob/master/pom.xml

Once Maven Profiles are built for all testng.xml files then we are good to execute from Command Prompt or from Jenkins.

Maven Commands to be followed when executing testng.xml files. 

mvn test –P followed by Maven Profile Id like below:

mvn test –PSmoke     

mvn test –PRegression

Well, we are now good to execute our testng.xml file based on our requirement.

Refer below repository for implementing Maven Surefire Plugin and Maven Profile.  

https://github.com/PavanReddy77/MavenSurefirePlugin

Conclusion: In this blog, we have seen implementing Maven Surefire Plugin and Maven Profile in a Maven Project.

Blog Contributors:

Author:  Pavan K

Pavan, having good knowledge in Selenium Web UI and API Automation frameworks. Also, he is helping QA community with his knowledge along with Naveen AutomationLabs!!

LinkedIn

Reviewer: Naveen Khunteta 

https://www.linkedin.com/in/naveenkhunteta

Steps to Configure Maven Project with Jenkins

Jenkins is an Open Source Continuous integration tool that is written in Java which is often used in Devops.

It is mainly used by Developers and Testers, whenever Developers/Testers does any changes in Source Code then Jenkins will automatically pull the code and continuously build and test code that has been added.           

Step 1: Installing Jenkins War File.

Follow below steps to install Jenkins War File. 

Go to any browser and search by Jenkins Download as shown below and click on  below highlighted link.

After clicking on above highlighted link, you will be navigated to below page.      

Click on “Permalink to the latest” – Latest and Stable version of Jenkins War File will be installed into your local system.

Now you are good to execute your Jenkins War File from Command Prompt.

Step 2: Executing Jenkins War File from Command Prompt.

Create a separate folder in your local system and place Jenkins War file once it is downloaded. Note: We must run Jenkins.war file through Command Line Prompt.

Step 3: Use below command to Run Jenkins.war file

Note:  By default, Jenkins will always run in localhost:8080. Make sure nothing else is running on port 8080. If localhost:8080 is already occupied then you won’t be able to execute Jenkins.war file.                    In such case you can execute your Jenkins.war file in other port using below command.

Once Jenkins is successfully launched, user should be able to see below message in
Command Prompt.

“Jenkins is Fully Up and Running”.

Step 4: Now, go to browser and open localhost:8084 where you have run your Jenkins.               
In my case, I have run Jenkins in port 8084.                                                                            

When launching it for first time, you have to set username and password and install default plug-ins that suggests etc and then you will be landed on to Jenkins Dashboard Page. 

Step 5: Configure your system for Java and Maven in Jenkins as shown below                

Go to Manage Jenkins >> Global Tool Configuration.

Step 6: Install Required Plug-ins >> Go to Manage Jenkins >> Manage Plug-ins and install from Available Section.

Step 7: Create Job >> New Item >> Enter Item Name >> Select Maven Project and Click on OK. After installing all Maven Plug-ins user should be able to see Maven Project Option.

Step 8:                                                                                                                                           A. Go to General >> Enter Description of your Project.

B. Go to Maven Info Plug-in Configuration >> Select None.

C. Go to Source Code Management. [You have to select any one option from it].       

Select None, if you are not using any Source Code Management tool for maintaining Code.

Select Git and provide Repository URL and Branch Name, if you are maintaining code in Git.

D. Go to Build Triggers.                                                                                                      

You can select as shown below, if you are not scheduling jobs using CRON Command and such. [In this case you have to build a job manually].

If you have to schedule jobs automatically, select Build Periodically and use CRON Command. 

If we have to build a job automatically when in any commit happens to Git then we
can select Poll SCM and use CRON Command.                                                                 

[As soon as code commit happens to Git, it will trigger a build].

E. From Build Environment, Select None

F. From Pre Steps, Select None.

G. From Build, Enter Path for your Root POM.xml and command to be executed as shown below.                                                                                                                  
Note: When in giving Maven Command to Run Test Cases in Jenkins, ignore mvn and just give commands as below.                                                                                
clean install Or test Or test -PSmoke etc.

H. From Post Steps, you can select any option as per your requirement.

I. If you want to be notified, when in any job is getting failed then you can select Build Settings and configure it.

Note: To do this we need to do other extra configuration [You can ignore for it now].

J. Post Build Actions can be used to Generate Results. [To achieve it, we need to install required plug-ins like TestNG Plug-in, HTML Report Plug-in for Generating Extent Report]

Below Configurations needs to be done for generating Extent Reports in Project Folder.

Step 9: Apply and Save, now click on Build now to trigger your Job.

Conclusion: In this blog, we have seen how to create a Job for Maven Project, Configuring Job and Executing a Job in Jenkins.


Cheers!!

Naveen AutomationLabs

Blog Contributors:

Author:  Pavan K

Pavan, having good knowledge in Selenium Web UI and API Automation frameworks. Also, he is helping QA community with his knowledge along with Naveen AutomationLabs!!

LinkedIn

Reviewer: Naveen Khunteta 

https://www.linkedin.com/in/naveenkhunteta

Executing Maven Project in windows from Command Prompt:


Executing Maven Project in windows from Command Prompt


Section-I:        

Pre-requisites:
Go to Menu Bar in Eclipse >> Window >> Preferences.     
Check if Maven is integrated with Eclipse or No.
[If you download Eclipse Neon Version, by default Maven will be integrated with Eclipse].

If Maven is integrated with Eclipse then we are good to create Maven Project.                           
If not installed go to Menu Bar >> Help >> Eclipse Market Place and Search by Maven and install required Maven Plugins shown below.




Section II:                                                                                                                                                                                    
 Steps to install Maven Jar Files and Setting Path in Environment Variables: https://maven.apache.org/download.cgi                                                                                                   
Download below highlighted Zip File.                                                                                                                                                      
Once download is completed, unzip it and keep it in any drive in your local system.                                
It is always good practice to keep it in C Drive.

Now we need to set path for Maven in Environment Variables:                                                         
Go to My Computer >> Right Click >> Properties >> Advanced System Settings.                               
 Now Go to Environment Variables and Set Pat as shown below.                                                               
[In my case i have kept Maven Jars in C Drive]                            
                                                                                 

     
Now Add Bin Path of Maven in below Path 
highlighted                                                    
  
C:Program FilesMavenapache-maven-3.6.1bin
                                           
Once Path is set for Maven in Environment Variables.                                                                                              
Go to Command Prompt and type command as shown below to make sure that Maven is installed or not.
                                                                               

                                                                                                                               

If Maven is installed then we will see as highlighted above and now we are good to execute Maven Project. 

Section III:                                                                                                                                                                                           

 Now Executing Maven Project from Command Prompt.                             
                                                                   
To Execute Maven Project from Command Prompt, In pom.xml 
Maven Surefire Pluginmust be added.

<build>
<plugins>
                                    <plugin>
                                                      <groupId>org.apache.maven.plugins</groupId>
                                                      <artifactId>mavensurefireplugin</artifactId>
                                                      <version>2.18.1</version>
                                                      <configuration>
                                                                        <suiteXmlFiles>
                                                                                          <suiteXmlFile>testng_sanity.xml</suiteXmlFile>                                                                                                        </suiteXmlFiles>
                                                      </configuration>
                                    </plugin>
                  </plugins>
</build>

Now Go to Command Promp
Go to Project Folder as shown below

Now Use below commands to execute Maven Project from 
Command Prompt                          
1. mvn clean install : It will execute all test cases as well as it will generate a build. Build will be generated under target folder.                                                                                                                                                                   
2. mvn clean test : It will execute only test cases and it won’t generate build.


This blog is written by Pavan. He is an expert in Selenium Automation and currently working in Infosys as Automation QA Engineer. Also, he is helping QA community with his knowledge along with Naveen AutomationLabs!! 

Cheers!
Naveen AutomationLabs

How to solve Maven issue – Could not resolve archetype org.apache.maven.archetypes from any of the configured repositories.

How to solve Maven issue – Could not resolve archetype org.apache.maven.archetypes from any of the configured repositories.


  • Problem: – 



Solution
Steps to follow:
  1. Right click on project
  2. click on properties
  3. java build path
  4. select source
  5. add, apply then OK



6. Open window preferences
7. open Maven
8. deselect all dialog box


9. Open window : preferences, open Maven Archetype, click on ‘Add Remote catalog’ and add Catalog file: http://repo1.maven.org/maven2/archetype-catalog.xml
Description: Maven Catalog.



10. check library, right click on project

click on properties, go to java build path
click libraries
add external jars
add, apply ok. 

Error Resolved!! YEY!

This is Nazia. I have recently completed my post-graduation. Later I joined Naveen Automation Labs which is one of the best online training programs and improved my automation skills a lot. I face some difficulties while configuring Maven, so I tried some of the possible ways to solve them, hope this will help you as well.
Cheers!
Nazia

-Naveen AutomationLabs

What is scope under dependency in pom.xml for?


What is <scope> under <dependency> in pom.xml for?



The <scope> element can take 6 values: compile, provided, runtime, test, system and import.
This scope is used to limit the transitivity of a dependency, and also to affect the classpath used for various build tasks.
compile
This is the default scope, used if none is specified. Compile dependencies are available in all classpaths of a project. Furthermore, those dependencies are propagated to dependent projects.
provided
This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.
runtime
This scope indicates that the dependency is not required for compilation, but is for execution. It is in the runtime and test classpaths, but not the compile classpath.
test
This scope indicates that the dependency is not required for normal use of the application, and is only available for the test compilation and execution phases.
system
This scope is similar to provided except that you have to provide the JAR which contains it explicitly. The artifact is always available and is not looked up in a repository.
import (only available in Maven 2.0.9 or later)
This scope is only used on a dependency of type pom in the section. It indicates that the specified POM should be replaced with the dependencies in that POM’s section. Since they are replaced, dependencies with a scope of import do not actually participate in limiting the transitivity of a dependency.

Cheers!
Naveen AutomationLabs