The Maven JAR Plugin is used to package your project artifacts into jar files.
Goals:
- jar:jar create a jar file for your project classes inclusive resources.
- jar:test-jar create a jar file for your project test classes .
Build a JAR file:
Add the following into your Maven project POM:
<project> <modelVersion>4.0.0</modelVersion> <groupId>com.mycompany.project</groupId> <artifactId>core</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> </project>
Run the following Maven command:
mvn package
Output:
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ HelloWorld --- [INFO] Building jar: C:\projects\HelloWorldApplication\target\HelloWorld-1.0-SNAPSHOT.jar
Customize the Manifest file: The following configuration will modify the default manifest file included in your build Jar.
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.2.0</version> <configuration> <archive> <index>true</index> <manifest> <addClasspath>true</addClasspath> </manifest> <manifestEntries> <mode>development</mode> <url>${project.url}</url> <key>value</key> </manifestEntries> </archive> </configuration> ... </plugin> </plugins> </build>
Include/Exclude content from Jar: The following configuration will exclude all txt files in the build artifact.
<build> <plugins> ... <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.2.0</version> <configuration> <excludes> <exclude>**/*.txt/*</exclude> </excludes> </configuration> </plugin> ... </plugins> </build>