12. Maven
Programming Project 2021/22

12.3. Getting Started

Before we go into the concepts and details of Maven, let us have a hands-on experience.

Installing Maven on macOS

$ brew update
$ brew install maven

Installing Maven on Linux

$ sudo yum install maven

or

$ sudo apt-get update
$ sudo apt-get -y install maven

Installing Maven on Windows

  1. Download the latest Maven from the Apache Maven website https://maven.apache.org/download.cgi (e.g. apache-maven-3.6.3-bin.zip).
  2. Unzip it in the folder where you want maven to live in.
  3. Add both M2_HOME and MAVEN_HOME variables to the Windows environment using system properties, and point it to your Maven folder.
  4. Update the PATH variable by appending the Maven bin folder – %M2_HOME%\bin, so that you can run the Maven’s command everywhere.

Tutorials: How to install Maven

If the previous instructions are not sufficient for you, try one of the tutorials below:

Checking Maven installation

You can test if Maven is properly installed in your machine by running:

$ mvn --version
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /usr/local/Cellar/maven/3.6.3/libexec
Java version: 13.0.1, vendor: Oracle Corporation, runtime: /Library/Java/Jav
aVirtualMachines/openjdk-13.0.1.jdk/Contents/Home
Default locale: it_IT, platform encoding: UTF-8
OS name: "mac os x", version: "10.14.6", arch: "x86_64", family: "mac"

Setting up our first Maven project

  1. Create a pom.xml file:

    <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/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      
      <groupId>it.unibz.pp2022</groupId>
      <artifactId>hello-world</artifactId>
      <version>1.0-SNAPSHOT</version>
      
      <properties>
        <maven.compiler.target>17</maven.compiler.target>
        <maven.compiler.source>17</maven.compiler.source>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      </properties>
    </project>
  2. Reorganize our project structure such that it complies with the Maven's default project structure.

    $ mkdir -p src/main/java
    $ mv HelloWorld.java ./src/main/java/
  3. We can now start using some maven commands. To compile your project, execute:

    $ mvn compile
  4. To clean your project's environment, execute:

    $ mvn clean
  5. To compile and create a jar file for your project, execute:

    $ mvn package

Including dependencies in the project

On your pom.xml file, add the following before the </project> tag:

<dependencies>
  <dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.12.0</version>
  </dependency>
</dependencies>

Creating a new Maven project in IntelliJ IDEA

  1. If no project is currently open in IntelliJ IDEA, click Create New Project on the Welcome screen. Otherwise, select File | New | Project from the main menu.
  2. Enter the name of the project and select its location.
  3. Select Java as the language from the options.
  4. Select Maven as the build system from the options.
  5. Specify the project's JDK or use the default one.
  6. Click on the advanced settings to specify the GroupId to define a package of the new project (and the ArtifactId).
  7. (If you are creating a project using a Maven archetype, IntelliJ IDEA displays the Maven settings that you can use to set the Maven home directory and Maven repositories. Also, you can check the archetype properties.)
  8. Click Create.

See more at JetBrain's support page.

Creating a fat jar using

We add the following snippet to our pom.xml file (which uses the Assembly plugin).

<build>
  <plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
            <configuration>
              <archive>
                <manifest>
                  <mainClass>HelloWorld</mainClass>
                </manifest>
              </archive>
              <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
              </descriptorRefs>
            </configuration>
          </execution>
        </executions>
      </plugin>
  </plugins>
</build>