:::: MENU ::::

Maven


Maven is a tool used to automatize the development of Java projects.

Installation

It is necessary to have the Java JDK installed in the system.

We open to https://maven.apache.org/download.cgi and download the latest version. We extract the file in a path with write permission.

We add the bin directory path (inside the directory where we have installed Maven) to the system PATH variable. This varies between operating systems. For Windows 10 we have to go to “Control Panel / System / Advanced configuration / Environment variables / System variables”,  and there add the path to the bin folder to the PATH variable.

Configuration

Maven is configurated editing the settings.xml file, in the conf folder. This file contains, with commentaries, instructions about different available configuration options.

As an example, to configure a local repository where the dependencies will be downloaded (in case we use that Maven option) we have to edit the “localRepository” section. We can uncomment it and edit the line with the path to one more suitable for us:

/path/to/local/repo

Creation of a Maven project

To start using Maven first we need to create a base structure (that will contain the src/java and src/test folders, and also the pom.xml descriptor file). We create a folder to our new project and enter. Inside, we launch the archetype:generate command, changing the “com.mycompany.app” and “my-app” parameters:

mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

This will create a basic structure from we can start working.

 

The project descriptor (POM)

Maven Java projects have a pom.xml file, called “project descriptor”, that define the operations to perform when Maven is executed, and also the dependencies and other properties we define.

Life cycle phases

To fully understand the different possible execusions of Maven, we are going to see the life cycle phases. They are a set of blocks of operations to build the project.

The default life cycle phases (there are others) are the following:

  • validate: it validates the project correction.
  • compile: it compiles the source code, generating the .class files.
  • test: it launches the available tests.
  • package: is foldes the project in a .jar, .war o .ear Java archive, as needed.
  • verify: it launches tests on the integration tests.
  • install: it copies the Java archive in the local repository.
  • deploy: it copies the Java archive in the remote repository.

Compile a project

For example, to compile a project, we have to go to its folder root and execute:

mvn compile

We will see the compiled .class files inside the target folder.

Pack the project

And to pack it, we launch:

mvn package

We will see the final packeted file in the target folder.

Use for dependency inclusion

A very interesating Maven use consists in automatize the dependency downloads (instead of get them manually and put them inside the project). This is achieved by editing the pom.xml file.

To do this, the easiest way is to go to https://mvnrepository.com/ and search the needed dependency. We copy the code associated to the dependency and copy it to our pom.xml.

By doing this, the dependency will be downloaded automatically to our local repository and it will be referenced by our project to its future use.


So, what do you think ?