7.2 Dependency management

Dependency management is one of the most powerful features of maven. After declaring explicitly all the dependencies for a project in the POM file Maven reads all the information and decides which jars must be loaded from the repositories.

Maven selects a defined list of dependencies by dependency mediation mechanism. If two dependency versions are at the same depth in the dependency tree then the first declared dependency is used.

7.2.1 Transitive dependencies

Maven resolves transitively all the required jars for a build. Transitiveness means that:

if A->B and B->C then A->C

Transitive dependencies can generate conflicts in the dependency tree for example:

The conflict in the above diagram example comes from the different versions of the jars commons-logging one having the version 1.0.2 and other version 1.1.1. With dependency mediation mechanism Maven will select the version 1.1.1 of commons-logging jar. The explanation should be that the dependency with version 1.1.1 is the newest. But in reality the version value has is not importance for the dependency mediation mechanism.

Other example:

Maven will select the dependency located at the smallest distance from the root of the tree. The dependency commons-logging-1.1.1 has the distance 1 from the root and commons-logging-1.0. 2

It is possible that the version of a dependency could be specified explicitly like this:

Square brackets were added around the version this means that the version will be only 1.0.2 all the time during the build.

7.2.2 Dependency scope

Because dependencies can be resolved transitively are they are defined in POM, we don’t need all the dependencies in all the cases. The role of a dependency can be defined by <scope/> tag or by adding the tag <optional>true</optional>. Maven dependencies have six scopes:

Compile

Default scope. The dependencies are available in the classpath.

Provided

The JDK environment provides dependencies at runtime.For a web application it is not necessary to include in the WEB-INF/lib this dependency.

Runtime

Dependencies are required at runtime and are specified in the runtime classpaths. It is not needed to compile the main source, tests or running the final artifact.

7.3 Quality assurance

Maven has many tools for software quality assurance (SQA). The SQA consist of a means of monitoring the software engineering processes and methods used to ensure quality. The used methods in QA are many and varied, and may include ensuring conformance to one or more standards. SQA covers the entire software development process.

7.4 Test Driven Development

Maven makes unit testing and integration testing being part of the build lifecycle. If the project is generated with maven-achetype-quickstart / achetype it would already contain the Junit dependency and some test classes.

Unit tests run always before package and require 100% success so that the build could go on.

Integration Tests require the project artifact and are run after package phase but before install. Like Unit Tests require 100% success before going on.

Acceptance test can verify problems that unit tests cannot do. The acceptance profile will be executed on the Continuous Integration (CI) server. This method helps also to separate slow tests by fasts tests.

7.6 Deployment automation

Automating Continuous Integration pipeline process can be very difficult with extra effort necessaryand configurations hard to do. The process is made easier with Maven.

The Maven Plugins designed for deployment process can add artifact(s) to a remote repository during the deploy phase.

The artifacts are not only copied to a repository but the metadata updating.

Deployment locations are handled in the distribution Management tag section from POM file. It is located after dependencies tag:

ServerId refers to the same server from settings.xml file. In settings.xml file the user and password are used for authentication information for the repositories URL can be based on protocols like FTP,SCP or SFTP and depending on this it will be necessary additional information (authentication or other permissions).

Deployment can be done to a cross platform (wagon transport with wagon ftp and wagon –ssh-external) or system-specific manner.

Apache Maven Deploy plugin is the tool used by Maven for automating deployment.

It uses two goals:

deploy:deploy

deploy all the project’s artifacts

deploy:deploy-file:

deploy a single artifact file

The deployment can be executed manually from the project folder location with the command:

mvn deploy

7.7 Reports generating

Reports generated by Maven can be classified in more categories:

Code quality reports

Unit testing reports

Test coverage reports

Maven Surefire Report Plugin can be used to generate reports of the project’s unit tests. In POM the tag report has to be added.