Back-end programming

MAVEN

Build process

Build process

  1. Translate the human-readable source code into a machine-executable program
  2. Creating the application binaries for a software release

Java Build process Tools

maven

What is maven?

Maven is a tool that can now be used for building and managing any Java-based project. 

 

Maven provides tools to:

  • Facilitate and uniformize the build process
  • Provide quality project documentation
  • Manage project dependencies efficiently

Convention over configuration

A manual build process, or even a build process created with the ANT tool, is extensive, repetitive, and not very exciting.

 

With Maven, developers do not have to mention each and every configuration detail; Maven provides the default behaviour for projects.

Directory structure

Maven defines a standard directory structure so that every developer will be immediately familiar with every Maven project.

main/java - Application/Library sources

main/resources - Application/Library resources

test/java - Test sources

test/resources - Test resources

target - Build output

Project Object Model

POM is an XML file, and it's a fundamental unit of work in Maven, because it contains various informations and configuration details about the project.

 

Some configurations details include:

  • project organisation/group (groupID)
  • project name (artifactId)
  • project version
  • source code locations
  • dependencies
  • plugins

XML

Extensible Markup Language (XML) is a markup language (i.e., a language that uses tags) that defines a set rules for encoding documents in a format that is readable by both machines and human beings.

 

XML was designed to store and transport data.

<?xml version="1.0" encoding="UTF-8"?>
<note>
  <to>Karl</to>
  <from>Laura</from>
  <heading>Reminder</heading>
  <body>Don't forget our meeting this weekend!</body>
</note>

Project Object Model

<?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>academy.mindswap</groupId>
    <artifactId>my-first-maven-project</artifactId>
    <version>1.0.0</version>
  
</project>

Every information that isn't explicitly specified by the developer, is inherited by from the Super POM.

BUILd lifecycle

A well-defined sequence of phases, which define the order in which the goals are to be executed.

A goal is a specific task which contributes to the building and managing of a project; it may be bound to zero or more build phases.

standard BUILd lifecycle phases

We can add goals to phases by configuring the plugins in our project.

// Execute all phases up to package
$ mvn package

Plugins

Tasks on Maven are executed by plugins. A plugin generally provides a set of goals.

 

Maven plugins are generally used to:

  • create a jar file
  • create a war file
  • compiling code files
  • unit testing the code
  • ...
// Executing a Maven goal
mvn [plugin-name]:[goal-name]

BUILD PLUGIN

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-compiler-plugin</artifactId>
   <version>3.8.1</version>
   <configuration>
      <source>15</source>
      <target>15</target>
   </configuration>
</plugin>
// Executing the compile goal
mvn compiler:compile

Dependencies

In the POM file, we can also specify what external libraries the project depends on, and Maven will download them.

<!--EXAMPLE DEPENDENCY-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot</artifactId>
    <version>2.4.4</version>
</dependency>

But how can Maven know all of this?

Maven repository

Plugins and dependencies are loaded from remote repositories when needed.

 

Downloaded software is saved in a local repository for use by all maven projects.

Live coding

Environment Setup

Live Coding

Project Setup

Maven

By Soraia Veríssimo

Maven

  • 1,525