What is Maven and how to create Maven Project using IntelliJ?

Mohammad Faisal Khatri
10 min readNov 15, 2022

--

What is maven and how to create a mvane project using IntelliJ— cover image

I have been approached by many folks on LinkedIn regarding learning Web automation, Mobile automation and API testing. Some of these folks are freshers who have just started their journey into Software Testing and sometimes experienced folks who are performing manual testing and now want to move into Automation Testing.

One thing I have noticed in common in both these kinds of folks that they haven’t tried their hands on IntelliJ yet and have lots of doubts creating and working with Maven projects. So, I thought I should create a blog post which might come handy to the beginners as most of the times I have seen them struggling a lot to create a new Maven Project in IntelliJ.

We would be discussing creating a new Maven Project in IntelliJ. Further, we will also see how to run a sample tests using Maven and different Maven commands which can be used to build project, compile and run the tests.

So, let's begin our journey with IntelliJ!

Download IntelliJ Idea:

The first and foremost step is to download IntelliJ idea IDE. It can be downloaded using the following link:

IntelliJ Idea download page

Once you land on the page with the link provided above, next step is quite straightforward, you need to select the required OS on which you are working on, like, Windows, macOS or Linux. There are 2 different versions which can be downloaded, Ultimate edition is a paid one and Community edition is free.

Here, As I am using Windows, I have clicked on Windows section, and downloading the Community edition and installing it.

The best thing about IntelliJ is, it comes bundled with Maven, TestNG, JUnit, etc. so you don’t have to download and install the plugins to make these frameworks work for you. There are lot more features in IntelliJ, I would recommend this course — IntelliJ for Test Automation Engineers by Corina Pip, where she has walked through lot of excellent things offered by IntelliJ.

Before we move into creating a new Maven Project , let’s understand some basics about Maven.

What is Maven?

Maven is a powerful project management tool that is based on POM(project object model). It is used for projects build, dependency and documentation.

How does Maven solve the following problems we face in project management?

Here is the list of tasks which Maven takes care of and you just need to sit back and enjoy your cup of coffee while Maven is working:

  1. It adds all the necessary jars to the project as per the dependencies put by user in pom.xml file.
  2. Automatically creates the right project structure.
  3. It makes a project easy to build.
  4. It provides project information like log document, cross references sources, mailing list, dependency list, unit test reports, etc.
  5. Works with CLI(Command Line Interface).
  6. CLI commands help us easily set up the CI CD pipeline so we can compile the source code, build the project and run the tests easily in the automated pipeline.

What is pom.xml?

POM is an acronym for Project Object Model. It contains information about project and configuration of the project such as dependencies, build directory, source directory, test source directory, plugins, goal, etc.
Maven reads pom.xml and then executes goal.

Here is a sample pom.xml file:

<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.mfaisalkhatri</groupId>
<artifactId>mavendemoproject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>mavendemoproject</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>

<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>3.7.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.github.mfaisalkhatri</groupId>
<artifactId>configreader</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<compilerVersion>1.8</compilerVersion>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<executions>
<execution>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
<configuration>
<properties>
<property>
<name>usedefaultlisteners</name>
<value> false</value>
</property>
</properties>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
<argLine>-Dfile.encoding=UTF8 -Xdebug -Xnoagent</argLine>
</configuration>
</plugin>
</plugins>
</build>
</project>

Creating a new Maven Project

Once the installation is complete, Start IntelliJ IDE and click on New Project button from the Welcome window and select Maven Archetype in the left hand panel displayed below the header — Generators.

In the New Project window, there are multiple fields in which we need to enter respective details to create the project, those fields are as follows:

  1. Name: Enter the name of the Project.
  2. Location: Browse the location in your computer locally where you want to save the project.
  3. JDK: Select the version of the JDK as per your project requirement, I would prefer setting this JDK 11 or above.
  4. Catalog: The following catalog names can be applied.
    - Internal: is the system catalog that is part the Maven distribution. —
    - Default Local: is the catalog that is located in the .m2 directory of your local machine.
    - Maven Central: is the catalog located at Maven central repository
  5. Archetype: It is a basic project template which can be used directly from maven. It will create the basic project architecture for you so you don’t have to manually do anything. Here, we would be using org.apache.maven.archetypes:maven-archetype-quickstart archetype. This is quick start archetype, so if you don’t know about what to use here, simple go ahead and use this archetype.
  6. Version: This field auto populates the version of the Archetype selected in the previous field. It is always recommended to use the latest version of the Archetype. In our case, 1.1 is the latest version.

Next, let’s move to the Advance Settings section where we would be updating the following details:

  1. Group Id: Here, ideally we update the website name in reverse order, so, for example my website name is mfaisalkhatri.github.io, hence, I would be update the Group Id as io.github.mfaisalkhatri
  2. Artifact Id: It is the name of the jar without version, project name is populated in this field by default, however, you can change it, if you want to.
  3. Version: This is the version of the jar file.

Click on Create button to proceed and create the project.

You will see that Maven automatically starts building the project for you as per the archetype selected. It will generate the pom.xml and update the junit dependency by default in it. You can remove it from the pom.xml file if it is not required by you and update all the dependencies as per your requirement.

Maven Project created successfully

After creating the project Maven will automatically compile and build the project and download the respective dependencies.

There is dedicated Maven Toolbar in IntelliJ, which can be found on the right hand side and used to run maven goals.

Maven Toolbar in IntelliJ

Now, let’s say if I want to use this project for Web automation using Selenium and TestNG, then I will just have to navigate to Maven Repository website, copy the respective dependencies and paste it inside the dependencies block.

If you have worked with Eclipse before, you might be knowing very well that after pasting the dependencies in the pom.xml if you save the file, it automatically downloads the jar files for those dependencies, however in IntelliJ we need to one more step to download the jars.

We need to click on the blue maven changes icon displayed on the top right after saving the pom.xml.

On clicking this icon, Maven will download all the respective jars’ for you. We can verify the jars' are downloaded successfully after expanding the External Libraries folder on the left hand (Project) panel.

Maven Dependencies — External Libraries
Maven Dependencies — Jar downloads

Likewise, we can also view the dependencies downloaded by Maven in its toolbar in the Dependencies section as shown in the screenshot below:

Maven toolbar — Dependencies

Congratulations, we have created the Maven Project successfully using IntelliJ.

Maven Project Structure

Maven project structure

Maven automatically creates the project structure for you. Hence you don’t have to worry about organizing your project folders. There are following two folders generated by Maven:

  1. src\main\
  2. src\test\

You can use the src\main folder for all writing the source code which will be packaged in the jar file of your project. src\test folder can be used to write the tests for your source code.

Let’s move towards knowing Maven commands now which can be used for building the project, downloading dependencies, compiling the project and running the tests.

Maven Commands:

1. mvn validate : This command validates whether the project is correct and all necessary information is available.
2. mvn compile : It compiles the source code of the project.
3. mvn test : It is used to test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed.
4. mvn package : It is used to take the compiled code and package it in its distributable format, such as a JAR.
5. mvn integration-test : Process and deploy the package if necessary into an environment where integration tests can be run.
6. mvn verify : This command is used to run any checks to verify the package is valid and meets quality criteria.
7. mvn install : It will install the package into the local repository, for use as a dependency in other projects locally.
8. mvn deploy : This command can be used in an integration or release environment. This command copies the final package to the remote repository for sharing with other developers and projects.

There are two other Maven lifecycle commands to note beyond the default list above, they are as follows:

1. mvn clean : This command will clean up artifacts created by prior builds.
2. mvn site : This command will generate site documentation for the project.

Phases are actually mapped to underlying goals. The specific goals executed per phase is dependent upon the packaging type of the project.

The above commands can also be run using life cycles commands for e.g.
1. mvn clean install.
2. mvn clean test
3. mvn clean verify, etc..

Lets execute maven goal clean install in the project we just created and see the output:

Output on running mvn clean install

Note: In case if the project execution fails due to the compiler error. Update the maven.compiler.source and maven.compiler.target in the properties block in the pom.xml

  <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>

Since our project didn’t have any tests, no tests were run and this has been displayed in the console as well.

No tests

Using the Properties block in pom.xml

Properties block in pom.xml are value placeholders, there values are accessible anywhere within the pom by using the notation ${X}, where X is the property. [Source]

We would be using the properties block for storing the dependencies version values. For doing so, we would be updating the <version> in the dependency tag as follows(note the <version> tag): ${X} is the syntax used here.

    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>${selenium-java.version}</version>
</dependency>

And we need to update the properties block as follows with the placeholder name we added in the properties block:

  <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<selenium-java.version>4.6.0</selenium-java.version>
</properties>

And these changes can be done for the storing the plugin version as well which are updated within the <build> tags.

Not only this looks clean and is a best practice to store the versions inside the properties block, we can also on the fly run our tests using different versions from the CLI directly after updating the versions in Properties block.

For example, say we have updated the Selenium-java.version as 4.6.0 in the Properties block. Now, if we want to run the tests using lower version of Selenium,say, 4.5.3, we can do this using CLI:

mvn clean install -Dselenium-java.version=4.5.3

This will run the tests by downloading Selenium’s 4.5.3 version.

Conclusion

To recap, Maven is a popular build tool and is used widely by many Software Engineers, as it makes their life easy to setup, build and compile the source code and run the tests. It has various commands which helps us to integrate the project with CI CD Pipelines.

In this blog, we discussed about creating new Maven project using IntelliJ and also studied in depth what Maven is and how to run the tests using Maven.

I hope you enjoyed reading this blog and learned something useful which would help you.

Please feel free to ping me in case you need any help. Happy Testing!!

Freelance Work / Paid Trainings/Mentoring

Contact me for Paid trainings/Mentoring related to Test Automation and Software Testing, ping me using any of the social media site listed on LinkTree or email me @mohammadfaisalkhatri@gmail.com.

References:

https://maven.apache.org/

--

--

Mohammad Faisal Khatri

QA with 14+ years of experience in automation as well as manual testing. Freelancer, blogger and open source contributor.