Friday, 28 April 2023

Maven Review

Maven is a project management tool, which is represented a the Project Model Object(POM).

Project Coordinate

Coordinate of the maven project is used to uniquely identify a maven project, and locate it. compulsorily, it requires the followings: group id: com.xxx; artefact id: project name;
version: version control.

group id: is used to identify the development team.
artefact id: is a short name to identify the project and code.
project name: the name of the project.

Parent POM

Parent POM is independent and doesn't belong to any project. It stands at a high level and may be used for an organisation to provide common versioned dependencies and plugins. It behaviors like an abstract class. It provides control on the versioned artefacts and licensed dependencies in one place.

Parent pom must be packaged as a POM.

Effective POM

Maven project is set up with default behaviors. It because Maven POM automatically extends from a super POM; the following command may show the effective pom file.

mvn help:effective-pom

Maven Project Structure

Maven project has a fixed file structure to follow.
The POM file is located at a project root.

src folder:

The source is located at the src/main
Java code is located at the src/main/java

Resources are located at src/main/resources
The test is located at the src/test.
Java test code is located at the src/test/java
Test resources are located at src/test/resources

target folder:
used to take all compiled results.

Build Lifecycle

The Lifecycle consists of many different phases and defines the processes for building and distributing a project. Maven plugin goals are attached in maven life cycles. Meaning that a big project management process is carried out by plugin goals respectively. For a developer building a project, this means that it is only necessary to learn a small set of commands to build any Maven project and the POM will ensure they get the results they desired. 
  • validate: is all the necessary information for this project available? 
  • compile: compile the source code of the project, and put them in the target folder.
  • test: test the compiled source code using a suitable unit test framework.
  • package: take the compiled code and package it in its distribution format, fx: jar. 
  • verify: run integration tests to ensure quality criteria are met. 
  • install: install the package into the local maven repository for other project's dependency. 
  • deploy: copying the final package to the remote repository, for sharing with other developers. 
The default Maven lifecycle is listed as the above. there are other life cycles.

Plugins

The plugin is the reusable common build logic across different projects. All the tasks like compiling code, testing them with JUnit, packaging files, and documentation are carried out.  Maven main functions are carried out by the plugins.

each maven plugin may have several goals

mvn maven-plugin-name:goal

Type of Maven plugins: 
  • Build plugins: defined under the <build>
  • Report plugins: defined under the <reporting>

Archetype

The archetype is a Maven project template. A developer may create a project base on an archetype.

mvn archetype: generate

maven-plugin: archetype; and goal: generate

Using maven archetype plugin to generate a web app project skeleton from a command line.

mvn archetype:generate -DgroupId=com.ynz -DartifactId=sampleWeb -DarchetypeArtifactId=maven-archetype-webapp -DarchetypeVersion=1.4 -DinteractiveMode=false

Package

package information is stored in a pom file. 
package types: jar, war, and ear. 
default package is a jar if a type is omitted. 

Dependency Scope

  • Compile a default dependency scope. The dependent jar is available in all the stages.   
  • Test: The dependent jar is available during the test. It is designed to reduce the size of the artifact. 
  • Import: indicates this dependency(like BOM) will be replaced by another POM.

Force Repository Update

When Maven executes this pom.xml file, the dependencies will be downloaded from a central Maven repository and put into your local Maven repository. If we already have the dependencies in our local repository, Maven will not download them.

The download happens when we execute the following commands:
  • mvn package
  • mvn install
Both of the above include executing the following command:
  • mvn dependency:resolve 
We can solely resolve the dependencies by just running the command above.

Corrupted Dependencies

A network glitch while downloading dependencies, may cause corrupted dependencies.  The maven will feedback an error message. 

Could not resolve dependencies for project ...

As we know, maven won't download an existing dependency, so we may force download all dependencies, by attaching a command option, -U. 
  • mvn package -U
  • mvn install -U
  • mvn dependency:resolve -U 
In some cases, we need a deeper command, purge a local repository: 

mvn dependency:purge-local-repository

Analyse Maven Dependencies

  • mvn dependency:analyze
  • mvn dependency:tree
Some of the information provided by the dependency:analyze goal includes:
  • Dependency convergence issues: When different versions of the same dependency are used in the project, which can lead to runtime errors.
  • Unused dependencies: When a dependency is declared in the project but is not actually used by any code.
  • Missing dependencies: When a dependency is required by the project but is not declared in the pom.xml file.
  • Dependency conflicts: When multiple dependencies declare the same artifact with different versions.

Mediation Principle

Maven uses the principle of Dependency Mediation to resolve conflicts between different versions of the same dependency. Dependency mediation is the process by which Maven selects a single version of a dependency when multiple versions are available in the dependency tree.

The basic principle of dependency mediation is to select the "nearest" version of a dependency, i.e., the version that is closest to the root of the dependency tree. This means that if two different versions of the same dependency are declared in different parts of the dependency tree, Maven will choose the version that is nearest to the root of the tree and use that version for the entire tree.

For example, suppose your project depends on two libraries, A and B, and both libraries depend on the same library, C, but with different versions. Library A depends on version 1.0 of library C, while library B depends on version 2.0 of library C. In this case, Maven will select the version of C that is closest to the root of the dependency tree and use that version for both libraries A and B. If library A is closer to the root, then version 1.0 of library C will be used for both libraries, and if library B is closer to the root, then version 2.0 of library C will be used for both libraries.

In addition to selecting the nearest version of a dependency, Maven also supports the concept of Dependency Exclusion. This allows you to exclude specific transitive dependencies that are causing conflicts in your project. You can exclude a dependency by adding an exclusion element to the dependency declaration in your pom.xml file.

Overall, Maven's dependency mediation principle helps to ensure that your project has a consistent set of dependencies that are compatible with each other, even when different versions of the same dependency are used in different parts of the project's dependency tree.




Skipping Tests

To skip running the tests for a particular project, set the skipTests property to true.

  1. <project>
  2. [...]
  3. <build>
  4. <plugins>
  5. <plugin>
  6. <groupId>org.apache.maven.plugins</groupId>
  7. <artifactId>maven-surefire-plugin</artifactId>
  8. <version>3.0.0</version>
  9. <configuration>
  10. <skipTests>true</skipTests>
  11. </configuration>
  12. </plugin>
  13. </plugins>
  14. </build>
  15. [...]
  16. </project>

You can also skip the tests via the command line by executing the following command:

  1. mvn install -DskipTests

If you absolutely must, you can also use the maven.test.skip property to skip compiling the tests. maven.test.skip is honored by Surefire, Failsafe and the Compiler Plugin.

  1. mvn install -Dmaven.test.skip=true

 


Reference: 


Super Pom

No comments:

Can Jackson Deserialize Java Time ZonedDateTime

Yes, but must include JSR310. Thus ZonedDateTime can be deserialized directly from JSON response to POJO field. <dependency> <g...