logo logo

Getting Started with Maven in Less Than 10 Minutes – Part 1

Maven Tutorial: Getting Started with Maven in Less Than 10 Minutes - Part 1

Maven is a highly popular tool used by developers and testers to create, build, and run code projects and to manage the project dependencies. As testers, we can also use it to run our tests, with different types of settings, in different ways. This article and the following one, is a short Maven tutorial that will go over the basics of Maven that you need to know, to help you create a brand new project, manage it, and run your tests 😀

Maven Tutorial: Table of Contents

  1. You’re here → Getting Started with Maven in Less Than 10 Minutes – Part 1
  2. Getting Started with Maven in Less Than 10 Minutes – Part 2

Downloading and Installation

When it comes to having Maven available on your computer, you have 2 options: manually grabbing the version you want and installing it, or using IntelliJ which already comes with a pre-installed version.

Installing Maven yourself

If you choose to manage the Maven installation yourself, first you need to download the latest available version from their official website. You simply need to download the archive corresponding to your operating system and unpack it to a location of your choosing. Then, because you will at times need to use the command line, it’s a good idea to also add Maven to your computer’s path variables. And that concludes your installation 💪

Using the IntelliJ Maven installation

If you instead opt not to install Maven yourself, you already have it available in IntelliJ. The only thing you might still need if you will be using Maven commands from the command line is to add the available installation to your computer’s path variables, as in the paragraph above. From IntelliJ, you have a dedicated panel for working with Maven, and an option to use command line commands, without the command line. I will show examples in the following sections.

Maven Projects

Project groupId, artifactId, version

The 3 elements that define a Maven project are as follows:

  • A groupId: is an identifier that is common to all the projects of your organization. This is usually the domain name of your organization, written in reverse. For example, it could be: org.yourOrganizationName, com.yourOrganizationName.shop, io.testproject, etc.
  • An artifactId: this is the unique identifier of your project. It is basically your project’s name. This is either one word or several words separated by a dash (-). For example: nameOfProject, name-of-project, java-sdk, etc.
  • A version: the version of the project helps identify what code is in a project at any given time. Whenever new releases are made, the version is being changed.

Creating a project

Maven helps you create simple projects, to easily get you started with your work. Maven has defined a set of so-called ‘archetypes’, or templates if you want. Based on these templates, you will generate different types of projects, with a corresponding folder structure and a few dependencies added to the projects. The simplest project you can create is a ‘quickstart’ one.

In order to create a new Maven project, for example, the quickstart one, there are again 2 options: from the command line (if you set the path variables corresponding to your Maven installation) or from IntelliJ 💡

Creating a project from the command line

Creating the project starts with running the following command in the command line 👩‍💻:

mvn archetype:generate

This kicks off an interactive process, in which you will need to provide your desired groupId, artifactId, and version. But first, you need to choose the desired archetype to use for creating the project. The list of all available archetypes is displayed in the command line, with a number assigned to each. The quickstart one is already suggested in the command line:

command line

In this example, it has the number 1787 associated with it, and that is the number you need to type in the command line.

The next step is choosing a quickstart archetype version for your new project. Type in the suggested number. Then, once you are prompted for your groupId, type the one corresponding to your organization. The next step is typing in the desired artifactId for your new project (which corresponds to your project name). For the version, type 1.0-SNAPSHOT, and for packaging choose ‘jar’. Confirm your selections by typing ‘Y’ when prompted, and your project is now created.

Creating a project from IntelliJ

The second option for creating your Maven project is to open IntelliJ and go to File –> New –> Project. There, in the ‘New project’ screen, on the left-hand side choose Maven, then check the ‘Create from archetype’ checkbox, and select org.apache.maven.archetypes:maven-archetype-quickstart:

Maven Tutorial

Clicking the ‘Next’ button will take you to the next screen, where you need to type your values for the groupId, artifactId, and version:

next

Click ‘Next’, then ‘Finish’, and your project will be created and loaded in the IntelliJ editor 😎

Quickstart Project Structure

In src/main/java, all the nontest code should be placed (Java classes). This includes helper/utility classes. In src/test/java you will store all the test classes of the project. In both these locations, you should create so-called ‘packages’, which are nothing more than folders. The folders help group tests for easier management. Additionally, on the root level, you have a file called pom.xml which will be discussed in the next chapter. You should also manually create 2 additional folders: in src/main – a folder called resources (to store non-Java files); in src/test – a folder called resources (to store non-Java files).

Dependencies and the Maven repository

The pom.xml file is a very important piece in your project. It helps manage the dependencies of the project. And by dependencies, we mean any service, library of framework you need to import, to help you implement functionality. You will call code from these libraries in your own code. Such libraries might be: Selenium, Appium, TestProject, RestAssured, etc.

In order to be able to download any library into a Maven project, it needs to be “released”. That means it needs to be available in the so-called Maven central repository, with a version that is not a SNAPSHOT version.  The URL to the Maven repository where you can search for any project is here. By default, this is the repository from where you will grab any of your Maven project’s dependencies.

As mentioned in a previous paragraph, a project is defined by the combination of the groupId, artifactId and version. When you want to search for a library in the Maven repository, you need to know at least the artifactId. You can search by it, and then, in case there are multiple available options, you will need to pick the project with the needed groupId. Ideally, you will download the latest released version (unless it is not working properly for your project).

When you are defining a dependency for a project, this dependency is downloaded to the machine from where you are running the project. The location where you can find all the files of the downloaded dependency is: ${user.home}/.m2/repository.

In case you want to change the location of the local repository (the .m2 folder) or the repository from where you are downloading dependencies (to an internal repository), you would need to find the ‘settings.xml’ file. In some installations, it can be found in the .m2 folder, in other cases in the ‘conf’ folder of your Maven installation folder 📂 You can find further details here.

Dependencies and the pom.xml file

How are dependencies added/downloaded to a Maven project? Through the pom.xml file. This file is in fact a sort of a definition of a project. It has several sections that deal with different aspects of the project. In the first section, you can find tags (entries found between ‘<‘ and ‘>’ characters, since this is an XML) defining the project groupId, artifactId, and version. The main tag that encompasses this information, plus the ones I will describe later on, is the <project> tag.

The dependencies are declared in the <dependencies> section, each dependency information being declared within a <dependency> tag. For each dependency, the groupId, artifactId, and version need to be declared.

For example, if in the project we want to add the TestProject library, we would need to search for it on the Maven repository website. Once we find it, we need to pick the version we want to add to our project. Let’s say we want the latest, which at the time of writing is 1.2.0-RELEASE. Once we click on this version in the list of available versions, we are taken to the page dedicated to it. Here we just need to copy the information of the project, as below:

information

We then need to paste it to our project’s pom.xml file, in the <dependencies> section:

dependencies

Every time we add new dependencies, before we can use them in our project, we need to import them, either by running a ‘clean install’ command (described in the next article) or from IntelliJ. In IntelliJ, we have a dedicated Maven panel:

Maven Tutorial - maven panel

Here, we just need to click the ‘Reload all Maven projects’ icon highlighted in the above screenshot. Then we can see that the project is downloaded in the project’s ‘External libraries’ section,  in the Project pane of IntelliJ. We can at this point start using the features of the downloaded dependency.

In my next Maven Tutorial I will discuss further Maven basics, such as how to compile projects, run tests, exclude dependencies, use profiles, and much more.
💥 TAKE ME TO MAVEN TUTORIAL #2 💥

Avatar

About the author

Corina Pip

Corina is a Test & Automation Lead, with focus on testing by means of Java, Selenium, TestNG, Spring, Maven, and other cool frameworks and tools. Previous endeavours from her 11+ years testing career include working on navigation devices, in the online gaming industry, in the aviation software and automotive industries.
Apart from work, Corina is a testing blogger (https://imalittletester.com/) and a GitHub contributor (https://github.com/iamalittletester).
She is the creator of a wait based library for Selenium testing (https://github.com/iamalittletester/thewaiter) and creator of “The Little Tester” comic series (https://imalittletester.com/category/comics/). She also tweets at @imalittletester.

Join TestProject Community

Get full access to the world's first cloud-based, open source friendly testing community. Enjoy TestProject's end-to-end test automation Platform, Forum, Blog and Docs - All for FREE.

Join Us Now  

Leave a Reply

popup image

TestProject X is Here!

Selenium Grid, Docker OR TestProject X? Learn how to achieve fast release cycles – no matter your skillset, and enhance whole-team collaboration between testers and developers!
Get Started Today
FacebookLinkedInTwitterEmail