Before we go into the concepts and details of Maven, let us have a hands-on experience.
$ brew update
$ brew install maven
$ sudo yum install maven
or
$ sudo apt-get update
$ sudo apt-get -y install maven
apache-maven-3.6.3-bin.zip
).M2_HOME
and MAVEN_HOME
variables to the Windows environment using system properties, and point it to your Maven folder.%M2_HOME%\bin
, so that you can run the Maven’s command everywhere.If the previous instructions are not sufficient for you, try one of the tutorials below:
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"
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>
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/
We can now start using some maven commands. To compile your project, execute:
$ mvn compile
To clean your project's environment, execute:
$ mvn clean
To compile and create a jar file for your project, execute:
$ mvn package
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>
See more at JetBrain's support page.
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>