JavaRush/Java Blog/Random EN/Part 4: Maven Basics

Part 4: Maven Basics

Published in the Random EN group
members
This material is part of the “Introduction to Enterprise Development” series. Previous articles: Part 4. Maven Basics - 1Maven is a tool for managing and building projects - a real assistant to the Java programmer. It makes life easier for the developer at all stages of work: from creating the project structure and connecting the necessary libraries to deploying the product on the server. When working with any framework, you will have to use Maven. So let's look at its main functions today and see how they should be used.

Step by step installation of Maven

  1. First you need to install Maven by downloading it from this link .

  2. Next, you need to unpack the downloaded archive and set the M2_HOME environment variable to a link to the location of the unpacked archive. For example, C:\Program Files\maven\

  3. To check that everything is installed, write on the command line:

    mvn-version

  4. If the version information for Maven, Java, etc. is displayed, you are ready to go.

  5. Now open IntelliJIDEA and create a new project. In the first window, select Maven:

    Part 4. Maven Basics - 2
  6. Click Next and fill out the following dialog box:

    Part 4. Maven Basics - 3
  7. Next, create a project in the required location as standard.

    After the project has been created, pay attention to its structure:

    Part 4. Maven Basics - 4
This is the standard structure for a Maven project :
  • the src/main/java folder contains java classes;
  • in src/main/resources - resources that our application uses (HTML pages, pictures, style sheets, etc.);
  • src/test - for tests.
Also pay attention to the file called pom.xml . This is the main file for managing Maven . The entire project description is contained here. There is not too much information there yet, but we will add it now.

Dependency Management in Maven

You may have come across the phrase “dependency manager” or “dependency manager”. Maven can do it all. Thanks to Maven, you don’t need to spend a lot of time searching for the required library on the Internet, downloading it, and then connecting it to the project: just add the required one to the list of Maven dependencies. Dependencies are written in the dependencies XML node. Let's say you need the Apache Commons IO library in your project for simplified work with files. To add a library, write five lines in pom.xml:
<dependency>
   <groupId>commons-io</groupId>
   <artifactId>commons-io</artifactId>
   <version>2.6</version>
</dependency>
Your pom.xml should now look like this:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>

   <groupId>example.com</groupId>
   <artifactId>example</artifactId>
   <version>1.0-SNAPSHOT</version>

   <dependencies>
       <dependency>
           <groupId>commons-io</groupId>
           <artifactId>commons-io</artifactId>
           <version>2.6</version>
       </dependency>
   </dependencies>
</project>
After this, allow IntelliJ IDEA to import the dependency (a dialog box should appear in the lower right corner). Now the library is ready to use:
import org.apache.commons.io.FileUtils;

import java.io.File;

public class TestMaven {
   public static void main(String[] args) {
       File tempDirectory = FileUtils.getTempDirectory();
   }
}
All subsequent dependencies should also be written inside the <dependencies> tag. You might be wondering: how do you know what information you need to include about a library inside the <dependency> tag? Everything is simple here. In such cases, three parameters are always specified: groupId, artifactId and version. You can find out these parameters in two ways:
  1. On the library website. If we need Apache Commons IO, go to the official website and select the Dependency Information tab. All the necessary information is here - you can simply copy it and add it to our dependencies section.

  2. In the Maven repository . Enter “apache commons io” in the search and you will see all available versions of the library. After selecting the one you need, just copy:

    <dependency>
               <groupId>commons-io</groupId>
               <artifactId>commons-io</artifactId>
               <version>2.6</version>
           </dependency>

    and add to your pom.xml.

Types of Maven repositories

It’s worth mentioning the Maven repository again, because we actually have two of them - external (global) and local, on your computer. All libraries that you add to your projects are saved in the local repository. When Maven adds a required dependency to a project, it first checks the local repository for the presence of such a library, and only if it does not find it there, it turns to the external one. As you can see, you can use Maven to add dependencies, but that's not all it can do.

Building a Java Project Using Maven

This feature may seem pointless to a newbie. Why is this needed if there is an IDE? But no! Firstly, the server on which you will have to build the application may not only not have a development environment, but also a graphical interface. Secondly, on large projects, Maven copes better with the tasks of assembling the project. Therefore, let's not torment ourselves with waiting, but consider the process of building an application using Maven.

Phases

The process of building an application is called the life cycle of a Maven project, and it consists of phases. You can look at them in IDEA by clicking on Maven>example>Lifecycle in the upper right corner: Part 4. Maven Basics - 5As you can see, there are 9 phases:
  1. clean - removes all compiled files from the target directory (the place where finished artifacts are saved);
  2. validate - checking whether all information is available for building the project;
  3. compile - files with source code are compiled;
  4. test — tests are launched;
  5. package - compiled files are packaged (into jar, war, etc. archive);
  6. verify — checks are performed to confirm the readiness of the packed file;
  7. install - the package is placed in the local repository. Now it can be used by other projects as an external library;
  8. site - project documentation is created;
  9. deploy - the collected archive is copied to a remote repository.
All phases are executed sequentially: you cannot start, say, the fourth phase until phases 1-3 are completed. There are two ways to launch a phase:
  • via command line:

    mvn package

    Part 4. Maven Basics - 6
  • using Intellij IDEA:

    Part 4. Maven Basics - 7

    Before the package is launched, the validate, compile, and test phases are performed. The exception is the clean phase. It is advisable to call it before each new project build. Phases can be listed separated by spaces:

    mvn clean package.

Also, each phase has pre- and post-phases: for example, pre-deploy, post-deploy, pre-clean, post-clean, but they are used quite rarely. In addition, each phase has goals. Standard goals are included by default, additional ones are added by Maven plugins. Sometimes during some phase you need to perform additional functionality. There are Maven plugins for this. The list of official plugins can be found on the Maven website . But know that there are still many custom plugins that can be found on third-party resources. Well, of course, if there is some exotic need, you always have the opportunity to write such a plugin yourself .

Plugins

To add a Maven plugin to a project, its description, similar to dependencies, must be placed in pom.xml in the <build> and <plugins> tags. For example, we need a plugin to check that all our external libraries are using the latest versions. After a little searching on the Internet, you can find this plugin with instructions for use. Let's set its groupId, artifactId and version. Let's write down what goals he must accomplish and in what phase. In our case, dependency checking in the current pom.xml is set to the validate phase. Now our “memory” looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>

   <groupId>example.com</groupId>
   <artifactId>example</artifactId>
   <version>1.0-SNAPSHOT</version>

   <build>
       <plugins>
           <plugin>
               <groupId>com.soebes.maven.plugins</groupId>
               <artifactId>uptodate-maven-plugin</artifactId>
               <version>0.2.0</version>
               <executions>
                   <execution>
                       <goals>
                           <goal>dependency</goal>
                       </goals>
                       <phase>validate</phase>
                   </execution>
               </executions>
           </plugin>
       </plugins>
   </build>

   <dependencies>
       <dependency>
           <groupId>commons-io</groupId>
           <artifactId>commons-io</artifactId>
           <version>2.6</version>
       </dependency>
   </dependencies>
</project>
We can still continue working on our project. But let's try changing the Apache Commons IO version to 2.0 and starting building the project. We get: [ERROR] Failed to execute goal com.soebes.maven.plugins:uptodate-maven-plugin:0.2.0:dependency (default) on project example: There is a more up-to-date version ( 2.6 ) of the dependency commons-io:commons-io:2.0 available. -> [Help 1] Here we have a build error caused by the plugin. The error message states that we are using version 2.0 when 2.6 is available. In general, Maven is a very useful tool. It may seem difficult to use at first, but practice, create your projects under Maven, and after a while you will be very happy with the result. This article deliberately omitted a lot of details about Maven - we concentrated on the essentials. But there is no limit to perfection: you can read more about Maven on the product’s official website . Part 5. Servlets. Writing a simple web application Part 6. Servlet containers Part 7. Introducing the MVC (Model-View-Controller) pattern Part 8. Writing a small spring-boot application
Comments
  • Popular
  • New
  • Old
You must be signed in to leave a comment
This page doesn't have any comments yet