A glimpse of Maven as a powerful build tool developed in Java.

Using Maven for dependency management, builds, and reporting

Isurie K. Liyanage
4 min readSep 4, 2021

Java developers have several options when building applications. Apache Maven solves the build needs of most developers and extends itself to include such areas as dependency management, unit testing, documentation, and reporting. we can achieve these tasks with many other tools, but apache maven provides a coherent, clean, and extensible framework to build applications, either as run-of-the-mill components or as unique, complex, and customized builds all with the same tooling.

From this article, we will look at the following points to understand building java the maven way.

Java project structure
  • POM files
  • Build life cycle
  • Build plugins
  • Dependency management
  • Maven sites
  • Maven reporting

POM files

When it’s getting into the heart and soul of Maven, it is the POM.xml file. POM file is the Project Object Model. It contains all the details about the project such as the dependencies, the build, and the reporting of the project itself.

The project information is top-level details of the project. It includes the following items;

  • groupId — Id for the project group, corresponding to the root package of the project
  • artifactId — the common name of the project
  • Version — Version of the artifact under a given project

There are a lot of other sections in the POM file. They are as follows.

  • properties — files to store Maven configurations values for the project.
  • Dependencies — defines dependencies for the project
  • Build section — defines plugins used to build the project
  • report section — identifies how to report information about the project

Additional Sections are organizational-specific instead of project-specific but not always.

  • Repository — point to Maven repositories for artifacts.
  • plugin Repositories — point to the plugins used by Maven itself.
  • Profiles — provide default behavior for common tasks.

Build life cycle

Maven build lifecycle is made up of three main lifecycles, each of which serves a distinct purpose.

  • Default — main lifecycle
  • Clean — clean project
  • Site — generates project documentation

Each life cycle is made up of phases. Phase is a stage or a step in the lifecycle.

Default lifecycle has validation, compile, test, package, verify, install, and deploy phases.

All phases must be executed in order, so you get every phase in a lifecycle, up to and including, the one that you call with your Maven command. Phases themselves are made up of goals. Goals are individual tasks within a phase. They are,

  • bound to a phase in the lifecycle,
  • can be triggered individually, and
  • the dependency plugin has a dependency analysis goal,

This allows you to ensure that you are declaring the dependencies you are using and not loading jars that you don’t need.

Build plugins

Build plugins are the most utilized plugins in the maven build life cycles. Build plugin participates in all three of the default life cycles for Maven. The output of build plugins can be used for reporting and artifact creation. A list of plugins for both Apache and third party can be seen on the Apache website. There are three types of build plugins,

  1. Core plugins — Compilation, Installation, Deployment, Validation
  2. Packaging plugins — JAR, WAR, EAR, SHADE
  3. Tools plugins — Variety of uses, Release, Signing, Dependency JAR, etc.

Dependency management

Dependency management is a mechanism for centralizing dependency information.

Parent POMs can help an organization control the actual dependencies used by projects within the organization. The mechanism to doing this is through the use of dependency management. In this way, dependency management allows specifying version and scope information in a single place. This is useful for ensuring the consistency of versions for all modules.

Maven sites

Why do we use Maven sites?

  • Maven sites provide build-specific documentation.
  • It can be automated.
  • Maven sites never become stale.
  • Uniform — Maven sites allow for consistent behavior across all the teams wrt documentation, and everything across the organization becomes ubiquitous.

Site Generation

  • mvn site the command executes the pre-site and site phases.
  • Automate the build-in documentation
  • deploy Maven sites through plugin goal, Site Deploy
  • All of this can be scripted to be included in every single build, making uniform behavior as easy for documentation as it is for code.

Deployments

when deploying the site,

  • Maven Three supports File, HTTP, and HTTP protocols for transporting the site.
  • Push the documentation to a structured Apache webserver or an HTTPD server.
  • Configurable ‘wagons’ can be used to transport the site more secure way.

Maven reporting

Reporting is one of the great benefits of Apache Maven. with reporting structure, you can generate documentation about your project and documentation about the various aspects of the building process.

Common reporting plugins

Let's see some of the most common reports that we want to consider when deciding how to build the Maven site.

  1. changelog report — when we are using SCM the changelog plugin gives everything that needs to know about what changes are included in the build. If we are aggregating multiple changes into a single build this becomes invaluable.
  2. checkstyle report — this can be both a build plugin and a reporting plugin. Helps for consistent code across all of its projects.
  3. surefire-report — this gives upper-level management. the surefire report lets people know that we are running tests and let us know what our coverage looks like and how we are testing it.

You may get the idea about using Maven for dependency management, building, and reporting from this brief and comprehensive overview of Maven.

Thanks for reading, follow me to see more articles..

--

--