Introduction

Ease of working in team is crucial in Software Development. There is not many projects You will be working on on your own. It’s important to keep in mind teamwork before starting project. One of the most difficult thing is dependence management. Maven can help with it.

I didn’t know it when I was working with my friends on first team project during the study. Our colleague created initial version of project, added some needed libraries and send us its names per mail. I downloaded given libraries and added them to project but there were problem with running an application. There were some compilation issues. Do You know why? Yes, my friend gave me only names of libraries but we were working on different versions, so some features that my teammate used was already dropped from same library in newer version. We’ve started sending them per email. As You may already know this also wasn’t good idea too. We had to put lot of effort into synchronizing our environments. We thought “It would be nice to have tool that will synchronize it for us”. Thereat we discovered power of Maven. This is a tool that do exactly what we wanted to do.

Explanation

Maven is tool which helps not only with dependency synchronization in project but also with building  application, running automatic tests, checking code quality, building docker images, versions management and many more.

Basic knowledge of this tool, on the beginning of our programming journey, is enough. Getting acquainted with Maven consist of learning some definitions, pom.xml file structure and basic commands.

Theory

To be able to fully understand what we are talking about we have to know what some words mean.

  • Artifact – in simple words this is a file, that is deployed to repository. Artifact have to have groupId, which usually is reversed domain name (same way as package naming in Java), artifactId, which is just a name, and version. This three values uniquely identify the artifact.
  • Repository – set of artifacts. Each setup of Maven causes creating local repository. There is also central repository and You can configure maven to be used with other repositories also.

Pom file structure

Pom.xml is project configuration file. I’m not going to describe here structure in details but I will show basics that are most important to be able to start working with it. Sole thing that You should know before reading below text is basic knowledge about xml.

Nodes

  • <project> – main node in project,
  • <properties> – defines properties, you can hold here for instance versions for libraries used in project,
  • <dependencies> – this is node that holds collections of dependencies, there will be many <dependency> nodes inside,
  • <dependency> – node that is exact definition of artifact that we want to add to project. Additionally we can define here scope, some libraries are needed only for development but we don’t need it in final version of application.
  • <build> – Maven helps with building application, You can configure it here.
  • <plugins> – set of plugins,
  • <plugin> definition of single plugin. Plugin is “thing” where much of real action is performed. We can use it to generate jar/war files, compile code.
  • <profiles> – sometimes You want to tell Maven to do something additionally when building application. Example profile can run Integration tests, generate documentation. In profile we can configure something that we want to do on demand.

There are lot of other nodes in pom but for now we don’t need to know advanced things.

Example pom.xml from project built in this post.

<?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>com.customernotes</groupId>
   <artifactId>CustomerNotes</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>

   <name>CustomerNotes</name>
   <description>Demo project for Spring Boot</description>

   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.0.4.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>

   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      <java.version>1.8</java.version>
   </properties>

   <dependencies>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-data-jpa</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
      <dependency>
         <groupId>org.flywaydb</groupId>
         <artifactId>flyway-core</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
      </dependency>
   </dependencies>
   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
      </plugins>
   </build>
</project>

Commands

As a user of Maven You have to know how to run its features.I will describe commands that I’m using everyday.

mvn clean install

Clear last build and create new one, when there are any compilation issues in code Maven will inform You about it.

mvn clean install -DskipTests

Do same as above but without running unit tests.

mvn clean deploy

clean build and deploy artifacts into the repository.

To turn on any profile we can use -P flag. For example mvn clean install -Pitest would run profile with name itest.

How to find dependencies?

When You already know which library or framework You want to add to project, go to maven’s repository and search for desired artifact.

Let’s assume that You need add library called itext that helps in PDF files generation. Type this name in search field and choose wanted library.

Search for Maven artifact

Next choose version which You want to use.

Choose maven artifact version

Copy dependency node which is visible on below image and copy to your pom.xml inside <dependencies> node.

Copy maven dependencies node

Now execute mvn clean install command. Maven will download this library for You and You can use it from now. Also every team member will be able to use it after pulling code and rebuilding project with Maven.

Summary

That’s all for today. I hope You understand basic and when someone in team will ask You to add new library You will be able to do that it.

For more detailed explanation see official documentation.

About author

Hi,
my name is Michał. I’m software engineer. I like sharing my knowledge and ideas to help other people who struggle with technologies and design of todays IT systems or just want to learn something new.
If you want to be up to date leave your email below or leave invitation on LinkedIn, XING or Twitter.

Add your email to be up to date with content that I publish

Categories: Learning

1 Comment

Junko Walczynski · 10/29/2019 at 00:41

Hi there, of course this piece of writing is truly fastidious and I have learned lot of things from it concerning blogging. thanks.|

Leave a Reply

Your email address will not be published. Required fields are marked *