Apache Maven is a build automation tool for Java projects. Think of Ant, or Make, but much more powerful and easier to use. If you've ever had to deal with building a Java project with dependencies or special build requirements then you've probably gone through the frustrations that Maven aims to eliminate.
Throughout this article I'll explain a bit more about the tool, how it's used, and hopefully answer your question - What is Maven?
Why is Maven useful?
Maven has been an open source project under Apache since 2003, starting at Sonatype before that. Given its strong backing and immense popularity, Maven is very stable and feature-rich, providing numerous plugins that can do anything from generate PDF versions of your project's documentation to generating a list of recent changes from your SCM. And all it takes to add this functionality is a small amount of extra XML or an extra command line parameter.
Have a lot of dependencies? No problem. Maven connects to remote repositories (or you can set up your own local repos) and automatically downloads all of the dependencies needed to build your project.
For example, lets say you have a project that uses Apache's Camel routing tool, version 2.10.6, but then a new version of Camel is released, 2.11.1. Instead of having to go to Apache's website, download the 2.11.1 distribution and replace 2.10.6 with it, you can just tell Maven to use the new distribution and the work will be done for you. Not that it's hard to download a single Java package and place it in your project's folder, but think about having to do this for multiple dependencies every time one is updated. It's a small amount of time, but adds up quickly.
How do you use Maven?
Using Maven is extremely easy, once you learn a few of the main concepts. Each project contains a file called a POM (Project Object Model), which is just an XML file containing details of the project. Some of these details might include project name, version, package type, dependencies, Maven plugins, etc.
A (very) simple pom.xml file might look something like this:
<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>com.stackabuse.myproject</groupId> <artifactId>myproject</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>myproject</name> <url>http://stackabuse.com</url> <build> <plugins> <plugin> <artifactId>maven-myquery-plugin</artifactId> <version>1.0</version> <configuration> <url>http://foobar.com</url> <timeout>10</timeout> <options> <option>foo</option> <option>bar</option> </options> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-core</artifactId> <version>2.11.1</version> </dependency> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-http</artifactId> <version>2.11.1</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
Don't worry about all of the small details in this XML, just notice what kind of contents are in the pom.xml, like name, version, and dependencies. This is where most of your interaction with Maven will take place, while the rest is through command line calls.
In order to actually run Maven, all you need to do is navigate to the project folder and run:
$ mvn [command]
The command could be a number of things, but to name a few:
test: Runs all of the tests for the project
clean: Deletes all of the old build files
package: Packages the project in to a jar (or other format depending on what was specified)
install: Places the project in the local Maven repository
deploy: Places the project in the remote Maven repository
Maven is a huge topic, and this brief introduction was only meant to give you an idea as to what Maven does, so be sure to check out the resources below for more information on how to actually use it.
Want to learn more about Apache Maven? Try taking an online course like Maven Crash Course, which will go in to a lot more detail than we did in this article. It'll teach you all about how to use this crucial Java automation tool, like setting it up, key concepts, and even integrating it in to Eclipse. Seriously, save yourself some time and learn from one of the best resources out there.