Install Maven

Maven is downloadable as a zip file at https://maven.apache.org/download.cgi.

Once you have downloaded the zip file, unzip it to your computer. Then add the bin folder to your path.

1
$ mvn -v

Define a simple Maven build

Maven projects are defined with an XML file named pom.xml. Create a file named pom.xml at the root of the project (i.e. put it next to the src folder) and give it the following contents:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.springframework</groupId>
    <artifactId>gs-maven</artifactId>
    <packaging>jar</packaging>
    <version>0.1.0</version>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>hello.HelloWorld</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
  • <modelVersion>. POM model version (always 4.0.0).
  • <groupId>. Group or organization that the project belongs to. Often expressed as an inverted domain name.
  • <artifactId>. Name to be given to the project’s library artifact (for example, the name of its JAR or WAR file).
  • <version>. Version of the project that is being built.
  • <packaging> - How the project should be packaged. Defaults to “jar” for JAR file packaging. Use “war” for WAR file packaging.

Build Java code

compile the project’s code

1
mvn compile

run the package

1
mvn package

The package goal will compile your Java code, run any tests, and finish by packaging the code up in a JAR file within the target directory. The name of the JAR file will be based on the project’s <artifactId> and <version>

Maven also maintains a repository of dependencies on your local machine (usually in a .m2/repository directory in your home directory) for quick access to project dependencies.

install your project’s JAR file to that local repository

1
mvn install

The install goal will compile, test, and package your project’s code and then copy it into the local dependency repository, ready for another project to reference it as a dependency.

Declare Dependencies

1
2
3
// src/main/java/hello/HelloWorld.java

import org.joda.time.LocalTime;

adding the following lines to pom.xml (within the <project> element):

1
2
3
4
5
6
7
<dependencies>
		<dependency>
			<groupId>joda-time</groupId>
			<artifactId>joda-time</artifactId>
			<version>2.9.2</version>
		</dependency>
</dependencies>
  • <groupId> - The group or organization that the dependency belongs to.
  • <artifactId> - The library that is required.
  • <version> - The specific version of the library that is required.

Additionally, you may specify a <scope> element to specify one of the following scopes:

  • provided - Dependencies that are required for compiling the project code, but that will be provided at runtime by a container running the code (e.g., the Java Servlet API).
  • test - Dependencies that are used for compiling and running tests, but not required for building or running the project’s runtime code.
1
2
3
4
5
6
<dependency>
	<groupId>junit</groupId>
	<artifactId>junit</artifactId>
	<version>4.12</version>
	<scope>test</scope>
</dependency>

Maven uses a plugin called “surefire” to run unit tests. The default configuration of this plugin compiles and runs all classes in src/test/javawith a name matching *Test. You can run the tests on the command line like this

1
mvn test

Spring Boot Maven plugin

  • It collects all the jars on the classpath and builds a single, runnable “über-jar”, which makes it more convenient to execute and transport your service.
  • It searches for the public static void main() method to flag as a runnable class.
  • It provides a built-in dependency resolver that sets the version number to match Spring Boot dependencies.

Building an Application with Spring Boot