What Is Maven?
Maven is a build and management tool for Java projects. Maven was originally created to create a simple and consistent build process for java projects, so that each time a projects code base was built it would be the same everytime and that build would be easy to manage.
To make the build process simple and to ensure it follows a standard, Maven has the concept of the Project Object Model, or commonly know now as the POM. The POM is combined with various plugins to create that simplified build configuration and process.
In general, for someone to understand the build process of your project all they should have to do is look at the contents of your projects POM.
How To Run A Maven Project By Example
As a quick example, lets look at how we get a project setup, and then create an executable build that we can use to demonstrate easily the use of Maven and some of the things it can do.
In this example we will create our code in eclipse, then create junit tests to test it. We will then proceed to us Maven to create a jar file, edit the manifest of the jar file, and bundle our jar, the dependencies and other resources into a zip file.
We will assume you have setup your development environment already with Maven, and have followed steps similar to my previous development environment setup article, https://colwil.com/setting-up-your-eclipse-java-development-environment/ . Once you do that you should have a basic pom.xml in the root of your project.
So heres our basic project structure in eclipse that we will start off with
If we start off with a basic HelloWorld class to get us going which will be as below:
package com.colwil.hello;
public class HelloWorld {
private static Logger LOGGER = Logger.getLogger(HelloWorld.class);
public HelloWorld() {
LOGGER.info("Hello World!");
}
}
The screenshot of the code shows that the Logger class is in error. This is because we havent built the new project based on our POM yet, so the dependencies are not resolved. So lets look at the basic POM we need, and then run it so that we can get all our dependencies sorted out. The POM looks like this…
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.colwil</groupId>
<artifactId>mavenExample</artifactId>
<version>1.0.0</version>
<properties>
<java.version>1.8</java.version>
<junit-version>4.12</junit-version>
<hamcrest-version>1.3</hamcrest-version>
<log4j-version>2.12.1</log4j-version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>${hamcrest-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j-version}</version>
</dependency>
</dependencies>
</project>
Our groupId, artifactid and version define our project in Maven speak. And then the dependencies show which public projects we are going to depend on in our project. So lets execute the POM so our project gets built properly. Click on Run, Run Configurations in eclipse which will show us a list of configs and we select the relevant maven build config that we copied earlier.
And here is our maven run config, with our Maven goals that ensure that the projects gets built based on the POM and the dependencies get downloaded.
So lets run that and update the project. We can see the log from the run below. The project gets identified with the details we specified in the POM
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------< com.colwil:mavenExample >-----------------------
[INFO] Building mavenExample 1.0.0
The Maven eclipse plugin sorts out the eclipse project structure such as resetting the project layout and classpath to ensure the classpath includes the dependencies.
[INFO] --- maven-eclipse-plugin:2.10:clean (default-cli) @ mavenExample ---
[INFO] Deleting file: .project
[INFO] Deleting file: .classpath
[INFO] Deleting file: .wtpmodules
[INFO] Deleting file: .settings
Any additional dependencies are downloaded, any that are already in you repository will be added. The repository is where Maven stores all the dependencies you have ever needed in one location that you specify in the maven install config. If you are using maven that is built into eclipse, then your repository should be in your windows user folder somewhere.
[INFO] Downloaded from : https://repo.maven.apache.org/maven2/org/apache/logging/log4j/log4j-core/2.12.1/log4j-core-2.12.1-sources.jar (1.3 MB at 1.4 MB/s)
[INFO] Downloading from : https://repo.maven.apache.org/maven2/org/apache/logging/log4j/log4j-api/2.12.1/log4j-api-2.12.1-sources.jar
[INFO] Downloaded from : https://repo.maven.apache.org/maven2/org/apache/logging/log4j/log4j-api/2.12.1/log4j-api-2.12.1-sources.jar (254 kB at 2.0 MB/s)
[INFO] Downloading from : https://repo.maven.apache.org/maven2/org/apache/logging/log4j/log4j-core/2.12.1/log4j-core-2.12.1-javadoc.jar
[INFO] Downloaded from : https://repo.maven.apache.org/maven2/org/apache/logging/log4j/log4j-core/2.12.1/log4j-core-2.12.1-javadoc.jar (5.2 MB at 5.2 MB/s)
[INFO] Downloading from : https://repo.maven.apache.org/maven2/org/apache/logging/log4j/log4j-api/2.12.1/log4j-api-2.12.1-javadoc.jar
[INFO] Downloaded from : https://repo.maven.apache.org/maven2/org/apache/logging/log4j/log4j-api/2.12.1/log4j-api-2.12.1-javadoc.jar (1.0 MB at 4.3 MB/s)
Once the configuration has finished then you should see a Build Success at the end of the log
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 14.063 s
[INFO] Finished at: 2019-08-10T12:25:47+01:00
[INFO] ------------------------------------------------------------------------
So if we look at our project layout in Eclipse now it should look a little different. And as we can see the project now shows the dependencies we had in the POM.