In this post, we will create a simple Maven project using Eclipse as IDE. Eclipse provides excellent support for creating Maven projects. We will take a step by step approach to create a TestNG project using Maven.
Install Maven in Eclipse:
- Normally Eclipse comes with Maven plugin installed. We can confirm by clicking File -> New -> Project. In the new project wizard search for ‘Maven’. If Maven is already installed in IDE, then it will be listed as below.
- If Maven is not available, then we have to go to Eclipse Marketplace to get the plugin. From the top Menu bar, click Help -> Eclipse Marketplace.
- Click Eclipse Marketplace to open up the wizard. Type in ‘Maven’ and click Go, to search for Maven plugins. We have to select and install ‘Maven Integration for Eclipse’ plugin. In below case, since Maven is already installed, it will show as ‘installed’.
Maven Project Explanation:
- After successful installation, our next step is to create a TestNG project in Eclipse using Maven.
- Go to File -> New -> Other. In the wizard, type in ‘Maven’. If Maven is installed then it will be displayed.
- Select Maven Project and click Next. We have the option to provide a new workspace location or else use the default one. We also have an option to create a simple project by skipping the archetype selection. For now, we will select our own archetype.
- What is an archetype in Maven? Archetype is a plugin in Maven which will first ask to choose a type of archetype from the internal catalog and then generate a project template based on archetype selection. The type of archetype selection depends on the type of project that we need to build. For example if its a simple Java Project, then we will use ‘maven-archetype-quickstart’ archetype and if it’s a Java Web Application project then we will use ‘maven-archetype-webapp’ archetype. Thus in short Maven will create all the required folders, download all required classes and files based on archetype selection.
- In this project, we will be selecting quick-start archetype. Click next to proceed.
- Based on the selection of quick-start archetype, Maven will create a project structure inside our project.
- Enter Group ID which is the package name, Artifact ID which is Project folder name. Click Finish to complete the project creation.
- As we click Finish, Maven automatically creates the project structure and download all the necessary jars based on our archetype selection. As displayed below, inside project folder, there is a ‘src’ folder and which has ‘main’ and ‘test’ folders. ‘main’ folder will contain source code for the application and ‘test’ folder will contain source code for tests. Always remember Maven identifies a test file which has ‘Test’ added as a suffix. Like in below case, Maven by default creates two java files, one in ‘main’ folder and another in ‘test’ folder. The java file inside ‘test’ folder is named as ‘AppTest.java’.
- As mentioned earlier, Maven not only creates project structures but also download all necessary jars for the archetype selected. If we expand ‘Maven Dependency’, we can see that Maven automatically downloads the JUnit jar and stores it in ‘.m2’ folder, which is on our local machine.
- Now, what is ‘.m2’ folder? ‘.m2’ folder is created automatically when we create our first Maven project. This folder is called local repository and contains a
settings.xml
file that contains global settings for all maven executions and a folder calledrepository
that holds all of the local copies of various maven artifacts, either caches of artifacts pulled down from remote repositories, such as Maven Central, or artifacts built by your local maven builds. The artifacts are organized in there in folder structures that mirror the groupId’s of the artifacts. In Windows OS, it will be placed in ‘C://Users/Username/.m2’ path and in Mac OS, it will be placed in ‘/Users/username/.m2’ path. In Mac OS, by default folder starting with ‘.’ are hidden. To display the hidden folders in Mac OC, type in the following command in Terminal and hit Enter.-- Display hidden folders in Mac OS -- defaults write com.apple.Finder AppleShowAllFiles YES
settings.xml
file is not required unless we want to change the default settings. We can create our ownsettings.xml
file and place in our ‘.m2’ directory. To know more details onsettings.xml
file, visit Settings.xml page.- Next, comes the target directory. It is created by Maven and contains all the compiled classes, JAR files etc.
- The most important part of a Maven project is
pom.xml
file. A Project Object Model or POM is the fundamental unit of work in Maven. It is an XML file that contains information about the project and configuration details used by Maven to build the project. It contains default values for most projects. Examples for this is the build directory, which is target; the source directory, which is src/main/java; the test source directory, which is src/test/java; and so on. All POMs extend the Super POM unless explicitly set. For more details onpom.xml
file, visit pom.xml page.
Add TestNG class into Maven Project:
- Our next task is to add a TestNG class and execute it as a Maven project.
- For adding a TestNG class, we will need required jars as mentioned in our previous post Part 1: Scripting using TestNG…. However, in a Maven project, we don’t have to manually upload the jars. We will just update the
pom.xml
file to include the TestNG dependency. - To get a Maven dependency for any jar, we can directly Google it. Go to Google and type in ‘testng maven dependency’ and hit enter. Else we can directly go to mvn repository testng.
- Copy the below code and paste it inside dependencies tag in
pom.xml
file. Note that we are using TestNG version 6.10.<dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.10</version> </dependency>
- This is how our updated
pom.xml
file looks like. - Once we save the
pom.xml
file, Maven automatically downloads the required jars and stores them in our local repository. If we expand the Maven dependency, we can see the jar location. - To create a TestNG class, we will follow the same steps and reuse the same code mentioned in our previous post Part 1: Scripting using TestNG…. One point to remember is that the TestNG class is actually a test script, hence we have to place it in projectname/src/test/java folder. And also as mentioned before we have to add ‘Test‘ to the file name. Only then Maven will execute the test script.
- Next step is to execute our TestNG script. Now there are many ways to execute the Maven Project. One way is to directly open the Command Prompt or Terminal and execute the below command. The commands are same for Windows OS and Mac OS.
<!-- Go to Project folder --> cd /users/sejijohn/documents/workspace/TestMaven <!-- First clean and the run test--> mvn clean test
- This maven command will first delete all the class files from the target folder, recompile all the class file and then run the test. The final result will be displayed in the Command Prompt or Terminal.
- Another way is to execute using Eclipse. Right click Project Name and click Run As -> Maven Test.
- Maven is based around the central concept of a build lifecycle. When we select Maven test, Maven actually executes all lifecycle phases above test in a sequential manner, which means it will first execute validate, followed by compile and then test. To know more Maven lifecycle, visit Maven lifecycle page.
With this, we completed our first Maven Project creation in Eclipse. Learning Maven is no easy task but it will help you in coming posts. Hope you were able to follow most of it. Till next post, Happy Learning!