Introduction to Maven
Maven is a very flexible build tool. To understand it, you need to understand the following concepts:
- projects and the POM
- plugins and their goals
- build life cycle and phases
- packaging types
- dependency management and the local repository
Projects and the POM
The work unit in Maven is a project. Each project has its project object model (usually shortened to POM) defined in pom.xml, an XML file containing project information and details on how to build the project. The POM of a project can inherit from the POM of another project to form a hierarchy of POM inheritance; this is the mechanism used in Maven to avoid duplication. Eventually, the Maven Super POM inherits all POM files.
Plugins and their Goals
Maven is a plugin based execution framework. Each plugin defines one or more goals.
- Compiler plugin - compiles Java code
- Surefire plugin - runs Java tests
- Checkstyle plugin - analyzes Java source code
- Cobertura plugin - runs unit tests and measure their coverage
- Dependency plugin - analyzes Java dependencies
Build Lifecycle and Phases
compile
- compiles the project's source code (in other words, create.class
files from the.java
files)test
- runs the project's automated testspackage
- takes the compiled code and packages it in its distributable format (for example, put all the.class
files into a JAR)install
- installs the project's artifact into the local repository, for use as a dependency in other projects locallydeploy
- deploys the project's artifact to the artifact repository, usually Nexus (this phase should not normally be run by a developer)
Packaging Types
A Maven project's most important aspect is its type of packaging, which specifies the type of artifact produced by the project. There are many types of packaging built into Maven (e.g. jar, war, and ear). The packaging type of project specifies the goals of the plugin that are executed during each Maven build phase. In a jar project, for example, the jar goal of the maven-jar-plugin is executed. The war goal of the maven-war-plugin is executed in a war project.
Dependency Management and the Local Repository
Maven identifies all artifacts uniquely using group ID, artifact ID, version, and type (these values are collectively referred to as Maven coordinates for an artifact).
You never have to store artifacts that you rely on in source control with Maven. No more source control lib folders that do not have traceability and must be checked out as part of any submission!
A Maven build gets artifacts from the local Maven repository instead of artifacts in source control. For instance, if my machine compiles two projects using the same log4j version, on my machine there is only one copy of log4j.jar. The artifact is downloaded only once. Projects are typically configured to download all their artifacts from a corporate repository in a corporate environment.
Comments
Post a Comment