Maven
Maven is a project management tool that deals with building projects (build), dependencies (dependency), distribution, versions, etc. Software building is the process of compiling the program code to an executable form. Maven facilitates teamwork, documentation, and distribution of the project.
Also, a Maven repository is a directory of a packaged JAR file with a pom.xml file. It has information about the build configuration of the project. Here, a JAR is a package that combines multiple Java class files and related resources into a single file for distribution.
Configuring Maven
A Maven project is configured using the Project Object Model. A POM is an XML file that contains information about the project and configuration details used by Maven to build the project. The general configuration includes the project name, its owner, and its dependencies on other projects (libraries). A developer can configure individual phases of the build process, which are implemented as plugins. For example, a compiler plugin can be configured to use Java version 1.5 for compilation, or to use a specific previous version of a Java package if there are errors in the current version. Larger projects should be divided into several modules or subprojects, each with its own POM. Using a general POM, all modules can be compiled with a single command. A POM can also inherit configuration from other POM configurations. All POMs inherit the Super POM - this is a default configuration that specifies the directories for storing project files, plugins, etc.
Minimal pom.xml
The required fields for configuring a Maven project are:
· The parent XML tag (project)
· The version of the object model in the POM (modelVersion) - The model version itself changes very rarely, but is required to ensure the stability of use if and when Maven developers consider it necessary to change the model.
· The group identifier of which the project is a part (groupId) - The unique identifier of the organization or group that created the project. GroupId is one of the key identifiers of the project and is usually based on the fully qualified domain name of an organization. For example (bg.tu.varna.sit)
· The project/artifact identifier (artifactId) - The unique base name of the main artifact generated by this project. The main artifact for a project is usually a JAR file. The remaining artifacts in the project use the name of the main artifact to form their own.
· The version of the artifact from the specified group (version) - The version of the artifact generated by the project. Maven supports project development by notifying about the availability of updates.
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>bg.tu.varna.sit</groupId>
<artifactId>lab1-maven-app</artifactId>
<version>1</version>
</project>
Plugins
The plugin provides a set of goals that can be executed using the command mvn [plugin-name]:[goal-name]. For example, a Java project can be compiled with the compile goal by running mvn compiler:compile. There are plugins for building, testing, version control, running a web server, generating Eclipse project files, and many more.
Plugins are introduced and configured in the plugins section of the pom.xml file. Some basic plugins are included in every project by default with default settings.
Executing the build, test, and package goals of a software project requires manually executing each corresponding goal:
mvn compiler:compile
mvn surefire:test
mvn jar:jar
Концепцията за жизнения цикъл на Maven се справя с този проблем.
The Build Lifecycle
Maven is based on the basic concept of the build lifecycle. This means that the process of building and distributing a specific artifact (project) is clearly defined. A developer building a project needs to learn a small set of commands to build a Maven project, and the POM will ensure that the desired result is obtained. There are three built-in build cycles: default, clean and site.
default - The default build lifecycle handles building the project.
clean - The cleanup lifecycle takes care of removing project files from a previous build, in order to start a new build with a clean project.
site - The lifecycle handles creating the documentation for the project.
Each of these build lifecycles is defined by different build phase steps, where the build phase represents a stage of the lifecycle. For example, the default lifecycle consists of the following phases:
validate - checks that the project is correct and all the necessary information is available
compile - compiles the program code
test - runs tests on the compiled source code using a suitable testing framework
package - creates distribution files (e.g. .jar)
verify - runs and verifies the results of integrated tests to verify the operation of the source project
install - installs the source files to a local repository for use in other projects
deploy - deploys to a build environment, copies the final package to a remote repository for sharing with other developers and projects.
Goals can be associated with different phases of the lifecycle. For example, by default, the “compiler:compile” goal is associated with the “compile” phase, while the “surefire:test” goal is associated with the “test” phase. When the mvn test command is executed, Maven executes all the goals associated with the phases and thus executes the corresponding goals associated with the lifecycle phases in the order of execution of the phases.
Example
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
...
Dependencies
The dependencies section contains a description of external projects and libraries that will be used in the project.
Example
...
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
...
Maven stores dependencies in a repository. The repository is arranged at 3 levels: local, central and remote.
· The local repository stores the latest version of the libraries that have been downloaded to the development machine.
· The central repository is a remote version control server when teams work on a common software project, it is accessed via the network to which all developers are connected.
· The remote repository is located on a public server maintained by library developers, access to these repositories is via a global network.
When a new library is added to the project, Maven first searches for it in the local repository, then in the central and remote repositories.
Structure of a Maven project
lab1-maven-app
|-- pom.xml
`-- src
|-- main
| |-- java
| | `-- bg
| | `-- tu
| | `-- varna
| | `-- sit
| | `-- App.java
| `-- resources
| `-- META-INF
| |-- application.properties
`-- test
|-- java
| `-- bg
| `-- tu
| `-- varna
| `-- sit
| `-- AppTest.java
`-- resources
`-- test.properties