Introduction to Maven
Estimated time to read: 4 minutes
From Java Development Environments and Tooling¶
Overview¶
- Maven is a tool to build source code and produce an artefact, this being a component, a JAR or even a ZIP file.
- Maven is also used as a dependency management system for projects. Maven also automatically acquires all of the transitive dependencies for other packages.
- Maven can also act as a Project Management Tool with version / release handling.
- Javadocs and Site information can also be generated by Maven.
Who owns Maven?¶
Maven is managed by the Apache Software Foundation, it is open source software released the the Apache License.
Why use Maven?¶
- Repeatable builds - The ability to create the build for any environment without having to change any settings on the development side. The code is independent to the environment it is being built in.
- Transitive dependencies - Downloading a dependency will automatically acquire any dependencies that are required for you.
- Environment - Maven contains everything needed for the environment to be built.
- Local repository - Maven works from a local repository, that enables dependencies to be downloaded once and then referenced into projects, rather than having to download the same file multiple times for use across different projects.
- IDE and Standalone - Works well with an IDE, with many IDE's having integrations for Maven, and can be used from the command line.
- Preferred method - It is the preferred method for working with continuous integration tools such as Jenkins, Travis, Github Actions.
ANT vs Maven¶
ANT¶
Ant was developed to be a replacement for make
. make
was designed as a build tool for a UNIX environment and was adapted to other platforms over the years. Whilst Ant is built on top of Java and XML, both systems that are, by-design, usable cross-platform. With Ant, everything has to be coded explicitly. Ant is more akin to a scripting tool for project builds.
ANT Example¶
In the example above; The target name is used to represent the action that can be run. The description is used to describe what the action will do. The action, in this case <delete>
performs an action on the filesystem or code.
There are no standards in Ant. This means that, using the example above, a clean function may be called clean, clear or another other name.
Maven¶
- Full Featured - Maven is aligned towards being a build tool over a scripting tool.
- Implicit - Compared to Ant, there are implicit practices built in to Maven, meaning that there is more consistency across projects.
- Inheritance - Maven also supports inheritance across projects.
- Transitive Dependencies - Select dependency, automatically acquire other dependencies!
- Versioned
Pros and Cons¶
Maven | Ant |
---|---|
Black Box approach | Easily trace each step |
Steeper Learning Curve | Quick to learn |
Convention over Configuration | Copy and Paste intensive |
IDE Support | Large Project Size |
Less Overhead | |
Different approach / mindset |
Installation Best Practices¶
macOS¶
- Install brew
- Run
brew install maven
- Profit?
Windows¶
Download Maven¶
- Access https://maven.apache.org and download the binary ZIP for the latest Maven version.
- Extract the contents of the ZIP file to a suitable location. For example,
C:\Program Files\Maven\apache-maven-$VERSION
Add Maven system variables¶
- Open the start menu and search for
System Properties
- Open the
System Properties
control panel pane from the search pane - In the
System Properties
pane, selectAdvanced
- Under the
Advanced
tab, selectEnvironment Variables
- In the new
Environment Variables
pane, selectNew...
underSystem Variables
- In the
New System Variable
pane, add the following: - Variable Name:
MAVEN_HOME
- Variable Value:
C:\Program Files\Maven\apache-maven-$VERSION
Add Maven path variables¶
- In the
Environment Variables
pane above, selectPath
underneathSystem Variables
and then selectEdit...
- In the
Edit Environment Variables
pane, selectNew
- Enter the following as a new variable,
%MAVEN_HOME%\bin
- Select
OK
to save the new path variable
Verify Maven¶
- Open CMD / Powershell
- Enter
mvn -version
Hello World: Demo¶
pom.xml (Project Object Model file)¶
There are six elements to a pom.xml file. These are: Project, groupId, artifactId, version, modelVersion, packaging.
<project>
<groupId>io.entityfour</groupId> <!-- Reversed FQDN URI -->
<artifactId>HelloWorld</artifactId> <!-- Artifact name -->
<version>1.0-SNAPSHOT</version> <!-- Version of the Artifact we are building -->
<modelVersion>4.0.0</modelVersion> <!-- Refers to the type of project within Maven -->
<packaging>jar</packaging>
</project>
Project Directories¶
src/main/java
is the default directory that Maven will use for source files.
Maven Commands¶
mvn clean
- Initialises the directory structure and acquires any required filesmvn compile
- Compiles the source code whilst also automatically acquiring any required dependencies and plugins
Java 10+¶
When using Java 10 or above, extra elements need to be added to the .pom file so that the project builds as Java 5's compiler is no longer supported.
Updated / Java 10+ .pom file¶
<project>
<groupId>io.entityfour</groupId> <!-- Reversed FQDN URI -->
<artifactId>HelloWorld</artifactId> <!-- Artifact name -->
<version>1.0-SNAPSHOT</version> <!-- Version of the Artifact we are building -->
<modelVersion>4.0.0</modelVersion> <!-- Refers to the type of project within Maven -->
<packaging>jar</packaging>
<build>
<plugins> <!-- List of plugins in the project -->
<plugin> <!-- Nested Plugin for each dependency -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>12</release> <!-- Java version -->
</configuration>
</plugin>
</plugins>
</build>
</project>
As seen in the example above, there are at least three extra elements that have to be included when working with Java 10 or above. These are: build, plugins and plugin.
Procedure¶
- Create the directory structure
- Create pom.xml
- Run
mvn clean
in the target directory - Produce code
- Run
mvn compile
Maven Wrapper¶
mvn -N io.takari:maven:wrapper
exports a wrapper for Maven to the project structure. This wrapper ensures that either the specific or a specified version of Maven, using mvn -N io.takari:maven:wrapper -Dmaven=3.3.3
is used, irrespective of the system version.