What is Apache Maven?
Maven is a popular open source build automation tool which is based on POM(Page Object Model). It helps to manage builds, documentation, reporting, releases and also distribution as well.
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.
Make sure to install the JDK and configure the JAVA_HOME environment variable in your machine.
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.
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?
When creating a maven project, we have to add groupId, artifactId and the version. Follow the standards when adding those attributes since it helps to uniquely identify our project in the repository.
POM file (Page Object Model)
This is the fundamental unit of maven. This is an XML file. It includes all the required dependencies, plugins, goals, project versions etc. Basically it contains the details and configurations which maven needs when building the project.
When executing a goal, maven reads the POM file and gets the relevant configuration details and then executes it.
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.
Basically we are using below commands in our projects.
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.
There are various plugins in maven. Basically the whole functionality of maven based on plugins. We can use those plugins to generate reports and documentation as well.
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.
Your comments/suggestion are welcome!
Maven - Introduction
Maven, a Yiddish word meaning accumulator of knowledge, began as an attempt to simplify the build processes in the…
20+ Maven Commands and Options (Cheat Sheet) - JournalDev
If you are working on Java, you must know Maven. Maven is the most popular project and dependency management tool for…
How to install Maven - javatpoint
You can download and install maven on windows, linux and MAC OS platforms. Here, we are going to learn how to install…
Packt Subscription | Learn more for less
Access over 7,500 Programming & Development eBooks and videos to advance your IT skills. Enjoy unlimited access to over…