Maven Basic Concepts

In this workshop we are going to talk about the most basic concepts of Maven. A lot of IDE's like IntelliJ, Netbeans or others have support for maven pluggins, but in this laboratory we must to use.

Deliverable

At the end of this workshop you have to deliver a repository on GitHub where you demonstrate the execution for each step. Additionally, in the README file you have to present an inform where you respond to all the questions or tasks that appear during the workshop.

Install Maven

Follow the instructions at maven.apache.org/install.html to install Maven on Windows, MacOS, or Linux. Or, you can choose from one of the options below.

sudo apt-get install maven

Create Maven Project

Creating Maven projects is based on the concept of an archetype. Think of an archetype as a predefined template, like a blueprint from which we can generate projects.

POM file

POM stands for Project Object Model And this file represents a one stop configuration for the entire project. Open the file to answer the next questions.

Dependency Management

A dependency is a Java library that this project depends on. While developing Java applications, you'll almost always be using other developers libraries, in addition to your own. So if you want to use code that isn't a part of the Java core library, you'll need to add that library as a dependency. Dependencies and Maven are declared inside this dependencies element. With each dependency represented by its own dependency element. Naming follows the GAV convention which stands for Group Artifact and Version.

The multiple dependencies that could be added into the project using Maven could be consulted in the MVNRepository https://mvnrepository.com/.

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>
FileSpy.java
package edu.eci;

import java.nio.file.*;

import org.apache.tika.Tika;

import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;

public class FileSpy {
    private static final String FILE_TYPE = "text/csv";
    private static final String DIR_TO_WATCH = "C:\\Users\\DiegoPT\\Downloads\\temp";

    public static void main(String[] args) throws Exception {
        Path dir = Paths.get(DIR_TO_WATCH);
        Tika tika = new Tika();
        WatchService watchService = FileSystems.getDefault().newWatchService();
        dir.register(watchService, ENTRY_CREATE);

        WatchKey key;
        do {
            key = watchService.take();

            key.pollEvents().stream().filter(e -> {
                Path filename = (Path) e.context();
                String type = tika.detect(filename.toString());
                return FILE_TYPE.equals(type);
            }).forEach(e ->
                    System.out.printf("File found: %s%n", e.context())
            );
        } while (key.reset());
    }
}

This XML in here which indicated both the version of our source code as well as the version of our compiled code that we would like when we compile this code and said source 1.8 target 1.8. Therefore, we can accommodate lambdas.

Building Lifecycles and Plugins

Maven is based on the idea of a build lifecycle which refers to the process of assembling and distributing an artifact like a JAR file. Maven ships with three lifecycles and you can think of them in terms of distributing an application.

Now within lifecycle, there are phases. For examples, some of the phases that make up the default build lifecycle are the compile, test, package, and install phases.

package edu.eci;

import edu.eci.FileSpy;

public class App 
{
    public static void main( String[] args )throws Exception
    {
        FileSpy.main(args);
    }
}

All the commands showed here such as mvn compile, mvn install, mvn package, they all come from plugins. And even if you didn't have to explicitly add a plugin to your pom file, a Maven plugin provides developers a way to attach their own tasks called goals to phases. So we have lifecycles, phases, and goals putting it all together, here's how they're related.

Each Maven builds lifecycle such as the default lifecycle has specific phases associated with it. Think of these phases as categories of tasks that need to be performed. We've talked about some of these already such as compile, test, and package. Now at these phases are like categories of tasks, the actual tasks in Maven are called goals, these goals are provided by plugins. In other words, plugins bind goals to phases. These specific plugins are shipped with Maven but you can also use third party plugins and even develop your own. Speaking of plugins and goals, let's run a goal from the exact plugin short for execute.

Last updated

Was this helpful?