Fork me on GitHub

Usage

The examples below show how to use this plugin. For advanced configurations, see the Example section. See the documentation for Maven Archiver.

The plugin uses the JDK jar tool internally to create and verify the JAR file. This version of the Maven JAR Plugin uses Maven Archiver 4.0.0-beta-6 to handle manifest configuration.

Note: If you need to sign your JAR files, use the Maven Jarsigner Plugin.

How to build a JAR file

To create a JAR file with Maven, create a pom.xml file with at least the following content:

<project>
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.mycompany.project</groupId>
  <artifactId>core</artifactId>
  <version>1.0-SNAPSHOT</version>
  <!-- <packaging>jar</packaging>  -->
</project>

The jar packaging type is the default. You do not have to set it in this case.

  1. Place your Java source files in src/main/java.
  2. If you need extra resources, such as property files, on the classpath, place them in src/main/resources.
  3. Create the JAR file with the command below:
mvn package

The package phase bundles all the files into the artifact. In this case, the artifact is a JAR file.

The generated JAR file is in the target directory. Its name is core-1.0-SNAPSHOT.jar. The JAR file contains the compiled Java class files and the files from src/main/resources.

Usually you do not need to declare the maven-jar-plugin in your pom.xml. The plugin is bound to the Maven Build Life Cycle.

For full documentation, see plugin-info.

Modular JAR files

The Java Module System introduced Modular JAR files in the JAR file specifications. A Modular JAR file contains a module-info.class file in the root directory. For multi-release JAR files, the file can be in the versioned area. If the project contains module-info.class, the final JAR is a modular JAR. This happens without any configuration and regardless of the plugin version.

Since version 4.0.0-beta-2, this plugin can verify the JAR file with a validate option. The checks depend on the JDK tool and can include a verification of module-info.class.

If the JAR manifest contains the Main-Class attribute, this plugin sets the module main class to the same value. For an example, see Make The Jar Executable.