Maven Tutorial
Maven translates to "expert" or "professional" and is an open-source project developed in pure Java under Apache. Based on the Project Object Model (POM) concept, Maven can manage a project's build, reporting, and documentation from a central piece of information.
Maven is a project management tool that can build and manage Java projects and handle dependencies.
Maven can also be used to build and manage projects written in languages such as C#, Ruby, Scala, and others. It was originally a subproject of Jakarta but is now an independent Apache project managed by the Apache Software Foundation.
Prerequisites for Reading This Tutorial
This tutorial is mainly for beginners, helping them learn the basic functionalities of the Maven tool. After completing this tutorial, your expertise in Apache Maven will reach an intermediate level, allowing you to proceed to more advanced topics.
To read this tutorial, you need to have the following foundational knowledge: Java Basics.
Maven Features
Maven can help developers with the following tasks:
- Build
- Documentation generation
- Reporting
- Dependencies
- SCMs
- Release
- Distribution
- Mailing lists
Convention Over Configuration
Maven advocates for a common standard directory structure and follows the principle of convention over configuration. It is recommended to adhere to this structure as follows:
Directory | Purpose |
---|---|
${basedir} | Contains pom.xml and all subdirectories |
${basedir}/src/main/java | Project's Java source code |
${basedir}/src/main/resources | Project's resources, such as property files, springmvc.xml |
${basedir}/src/test/java | Project's test classes, such as Junit code |
${basedir}/src/test/resources | Resources for testing |
${basedir}/src/main/webapp/WEB-INF | Web application files directory, containing web.xml, local images, JSP view pages |
${basedir}/target | Packaging output directory |
${basedir}/target/classes | Compilation output directory |
${basedir}/target/test-classes | Test compilation output directory |
Test.java | Maven automatically runs test classes that follow this naming convention |
~/.m2/repository | Maven's default local repository location |
Maven Characteristics
- Project setup follows unified rules.
- Sharing across any project.
- Dependency management includes automatic updates.
- A large and continuously growing library.
- Extensible, with the ability to easily write plugins in Java or scripting languages.
- Instant access to new features with minimal or no additional configuration.
- Model-based builds - Maven can build any number of projects into predefined output types such as JAR, WAR, or distributions based on project metadata without the need for scripts in most cases.
- Consistent project information site - Using the same metadata as the build process, Maven can generate a website or PDF, including any documentation you wish to add, and add to standard reports about the project's development status.
- Release management and separate outputs - Maven integrates with source code management systems like Subversion or Git without additional configuration and can manage project releases based on tags. It can also publish releases to distribution locations for use by other projects. Maven can publish separate outputs such as JARs, archives containing other dependencies and documentation, or source code.
- Backward compatibility - You can easily migrate from multiple modules of older Maven versions to Maven 3.
- When subprojects use parent project dependencies, the subprojects should inherit these dependencies without needing version numbers.
- Parallel builds - Compilation speeds can generally be improved by 20-50%.
- Improved error reporting - Maven provides better error reporting, linking you to the Maven wiki page where you can find a complete description of the error.