Maven

This is a quick introduction to Maven.

Maven helps you capture all of the project configuration , dependencies , plugins in a central file called as POM.XML. This enables a much more easier experience in managing dependencies. When you create a project , you will have a pom.xml file in the root directory as follows

Root —>src —-> Main

—> pom.xml

The root folder that has the src directory will also have the pom.xml file

POM stands for project object model and this has all of the information about the project

pom will have sections that are meant for Properties  , dependencies , build report , Repositories , Plugin repositories , Profiles

Since this defined in pom.xml file this helps with reducing duplication , streamlining configuration , keeping items in sync and it aids in upgrades

Here are some commands that you could use

mvn clean package – this will build the artifacts , clean is optional but recommended , clean will delete all the previously built files and start fresh

mvn clean package site – creates a site dir under your target directory

If you go into site dir , there is a index.html , if you open that up it gives you access to all of the documentation.

mvn clean install – compile, test & package your Java project and even install/copy your built .jar/.war file into your local Maven repository

for large projects that have multiple modules , you will have a structure as follows

root —> src —> Module 1

—-> pom.xml ( module 1’s pom.xml )

Module 2

—–> pom.xml ( module 2’s pom.xml )

—> pom .xml ( parent pom <—-)

a little bit about transitive dependencies . Maven avoids the need to discover and specify the libraries that your own dependencies require by including transitive dependencies automatically.

So with Transitive dependencies you have

  • Dependencies of dependencies
  • Reduce scope of declaring dependencies
  • Reduce need to know inner workings
  • Reduce risk of upgrading

Rules when picking the underlying dependencies  – the closest version to the project is chosen , if project a is dependent on ver x 1.0 , but project a is depndedent on B that needs version 1.2 , maven picks 1.0 since its closer to the project

However we specifically mention the version in the dependency management section then it picks the closest version

Scope can play a role in whats included, local defn rules them all.

Only declare what you need

Dependency analyze to analyze

Validate scope

Consider using parent POMS

Always declare when risk of breaking

Always declare when risk of security

—-

We can move the dependencies from the underlying pom file to the pom file in the root and declare it there .

Running mvn clean verify will show if the dependency from the parent pom file is applied and if the project compiles successfully.

Running mvn dependency:analyze  will show the used  undeclared dependencies and unused declared  dependencies

Its good to run this before code is pushed further

Running mvn dependecy:resolve will list all of the  dependencies that are declared.   , its easier to use this instead of reading through the pom file. 

Running mvn dependency:tree will list all of the transitive dependencies that are being brought into the project

Running jar tf  xxxx.jar  – should show all the files included in the jar

When using the maven shade plugin  , we are aggregating classes/ resources from several artifact into one uber JAR and this would work as long as there is no overlap , however if there is an overlap we need some logic to merge resources and this is where transformers kick in   ( … from apaches site )

Automate the documentation build using the mvn site command   – keeps it refresh

Use the site plugin for building custom skin to match with the destination plugin

Reporting plugins

  • Changelog  – if aggregating multiple builds this is very helpful
  • Checkstyle  – build + report plugin  – allows to create rules to check code
  • Javadoc  – very commonly used  – generate javadocs
  • Surefire report – test coverage report