Build tools and Maven
Use one pom.xml to compile, test and package your Java project into a runnable jar.
- Declare the project with groupId, artifactId and version.
- List every external library as a dependency.
- Run mvn test to compile and test, then mvn package to build.
- Configure the jar manifest so java -jar starts your app.
In plain English
Maven is a build tool that compiles your Java code, runs tests and packages the result using a single configuration file. The pom.xml is that configuration file, and it declares the project coordinates, which are a groupId, artifactId and version that name the project. A dependency is an external library your project needs, and you list each one in the pom so Maven downloads it and adds it to the classpath. The Maven lifecycle is a fixed sequence of phases such as compile, test and package that run in order, where each phase triggers the ones before it. Packaging a runnable jar means building a jar file that includes a main class in its manifest, so a user can start the program with a simple java -jar command.
Step by step
- 1Create a pom.xml at the root of the project.
- 2Set the groupId, artifactId, version and packaging.
- 3Add each library you use as a dependency with its scope.
- 4Run mvn test, then mvn package to build a jar.
- 5Configure the jar plugin manifest and run java -jar on the output.
<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.edubyte</groupId>
<artifactId>java-lessons</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<properties>
<maven.compiler.release>17</maven.compiler.release>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.4.1</version>
<configuration>
<archive>
<manifest>
<mainClass>com.edubyte.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>Common mistakes
- Hardcoding a dependency jar on the classpath instead of declaring it in the pom makes the build impossible for other developers to reproduce.
- Running mvn package and expecting tests to be skipped is wrong, because package runs the test phase first and fails the build on a failing test.
- Forgetting the mainClass in the manifest produces a jar that cannot be started with java -jar, because the JVM does not know where to begin.
- Leaving the version out of the coordinates breaks dependency resolution, because Maven needs the full groupId, artifactId and version to identify an artifact.
Add a runnable jar configuration
Given a minimal pom.xml, add a maven-jar-plugin configuration whose manifest sets the mainClass to com.edubyte.Main.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.4.1</version>
</plugin>Which Maven phase also runs the tests that come before it?
Make it yours.
Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.
Lock in what you just learned.
Mark this lesson complete to save your place and move one step closer to the certificate.