Introduction to Apache Maven

What is Apache Maven?

If we don’t use maven, we face several issues. We have to add the relevant jar files in each project manually. Also have to create the correct project structure. Otherwise that won’t be executed.

Basically maven provides developers a complete build lifecycle framework.

Maven configuration

1.In Windows

Then download Apache Maven from their official website. Add JAVA_HOME and MAVEN_HOME in environment variable. Then add maven_path in environment variable. Finally verify it using the “mvn -version” command.

If you configured maven correctly, you will see the version in the command line.

Check this site more details about windows configuration.

2.In Mac

You can do this in 2 ways. Either manually or using the Homebrew package manager. Homebrew installation is easy to do. Just the “brew install maven” command will continue the process.

But when you do this manually, you can learn more facts about maven configuration.

If you do this manually, download maven from their official site and extract the file. Watch this video to see the steps clearly. I’m not going to put the command list here since this video explained that simply. If you don’t have a .bash_profile, use the below command.

touch .bash_profile(make sure to put a space between touch and .bash_profile). Verify the maven configuration using “maven -version” command.

What is groupId, artifactId and version?

Eg:

<groupId>com.yourcompanyname.automation</group>

<artifactId>sample-application</artifactId>

<version>0.0.1-SNAPSHOT</version>

POM file (Page Object Model)

When executing a goal, maven reads the POM file and gets the relevant configuration details and then executes it.

Maven Architecture

Image 1: High level Maven Architecture

Here maven creates the local repository with all required dependency files, jar files when we build the project for the 1st time. This local repository is located in your machine

Central repository is provided by maven community. This contains large number of common libraries. If the necessary jar files or dependency files aren’t included in the local repository, maven download it from the central repository and then copy that to our local repository as well. That’s the usage of this kind of build automation tool.

As an example, our local repository doesn’t have JUnit 4.10 version. But we add the JUnit version in our POM file as 4.10. Then we build the project. Maven checks the 4.10 version in our local repository. Since it doesn’t have it, maven download that from the central repository and copy to our local repository and build the project successfully. If we don’t have maven, we have to download those files manually.

Remote repository is a place where we copy our final packages in a distribution format. So the other developers also can use that file for their project requirements.

Other than that, the site plugin is used to generate a site for the project which includes reports and documentation. When you work with maven practically, you will understand this architecture well.

Maven Commands

mvn clean — Delete the previously compiled java files and resources(Files inside the target folder will be deleted)

mvn compile — Compile the source java classes. If you need to compile the test classes of the maven project, you can use the “compiler:testCompile” command.

mvn package — Wrap the source code into a file format which we can share or distribute (eg:war, jar). Simply converts in to an executable java program.

mvn install — Install the package or the wrapped compiled code(jar or war file)in our local repository. So we can use it for our other projects as well.

mvn test — Run tests using an unit test framework and you can see the output in console.

mvn deploy — Install or copy the final package to a remote repository. So other developers also can use it.

Remember that when you use these commands in eclipse IDE, you don’t need to use “mvn”. But when you work with the terminal, use the full command.

Maven Plugins

Eg:

maven surefire plugin to generate reports and maven site plugin for documentation.

We can use “mvn surefire-report:report” and “mvn site” commands to generate reports and documentation.

I have added 2 sample videos below to show how the above commands work in both eclipse IDE and terminal. Go through it if you are interested. It will help you to understand how the maven works.

https://www.dropbox.com/s/7u5o658sste52le/maven_demo_IDE.mov?dl=0

https://www.dropbox.com/s/kryffachdlg91a6/maven_demo_terminal.mov?dl=0

Your comments/suggestion are welcome!

Thank You!

References:

Quality Engineer | Blogger | Tech Enthusiast

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store