Updated April 17, 2023
Introduction to Maven Jar Plugin
Maven jar plugin is used to build the jar files, if we defined our project as a jar file maven jar project will call it implicitly. We have no need to define it inside the pom.xml file this will be downloaded when it was required by maven. We can also define it into the pom.xml file to control the specified features of the jar file which was generated from maven jar file. The jar plugin has two goals defines i.e. jar and test-jar, default goal of jar plugin is jar.
How Maven Jar Plugin works?
Basically, maven jar plugin has two goals defined first one is jar and the second one is test-jar. Jar will allow the package into the main class as a jar file. Test jar will allow the test class package as our jar file. If suppose we have defined the default goal as jar then we have no need to define the goal into the pom.xml file. Maven jar will automatically take or invoke the goals when it has needs for this file.
Below are the apache maven assembly plugin version available for the general use.
- Version 2.0.x
- Version 2.1.x
- Version 2.2.x
- Version 2.3.x
- Version 2.4.x
- Version 2.5.x
- Version 2.6.x
- Version 3.0.x
- Version 3.1.x
- Version 3.2.x
It will allow us to use the default manifest file. We are storing this manifest file into the project directory path. The apache version 2.1 jar plugin will use the maven Archiver 2.1, so from this version, we have no need to add the implementation and specification details into the manifest file. If suppose we need to add this details into the file then we are defining the same manually. We can also alter the default content from the manifest file by using the element of archive configuration. Basically, the maven jar will provide build jar capability. We are creating a maven jar file with the use of the following content.
Below is the basic file syntax to create pom.xml.
<project> -- Start of the project.
<modelVersion> /* start of model version section */ 4.0.0</modelVersion> -- start of model version section
<groupId> /* start of groupid section */ name of groupid </groupId> -- End of groupid section
<artifactId> /* start of artifactId section */ core</artifactId> -- End of artifactId section
<version> /* start of version section */ name of version</version> -- End of version section
<!-- <packaging>jar</packaging> -->
</project> -- End of the project
Using it, jar is the default package which was required to set. We are creating the jar file using the mvn package command. The phase of the package is responsible to bundle the files into an artefact. Using the maven jar file we have no need for any additional dependencies to create an executable jar.
Below is the important parameter which was needed to use at the time of creating it.
- model version
- groupId
- artifactId
- version
- packaging
Model version is nothing but the version number which we are using at the time of creating pom.xml file using it. GroupId is nothing but the group name which was we have using at the time of creating pom.xml file using it. ArtifactId is nothing but the artefact name which was we have using at the time of creating pom.xml file using it. The version parameter is nothing but the snapshot version which was we have using at the time of creating pom.xml file using it. We can create jar files using the default manifest file, custom manifest file, exclude, include files and by using the additional jars.
Examples of Maven Jar Plugin
Given below are the examples mentioned:
Example #1
Using default manifest file to create project using maven jar plugin.
- The below example shows use of the default manifest file to create a project using it.
Code:
<project> -- Start of project section.
<parent> -- Start of parent section.
<groupId> /* Start of groupid section. */org.springframework.boot</groupId> -- -- end of groupid section.
<artifactId> /* Start of artifactid section. */ spring-boot-starter-parent</artifactId> -- -- end of artifactid section.
<version> /* Start of version section. */2.5.4</version>
<relativePath/> /* Start of relativepath section. */ <!-- lookup parent from repository -->
</parent> -- end of parent section.
<version> /* Start of version section. */ 0.0.1-SNAPSHOT</version>
<name> /* Start of name section. */ maven</name>
<description> /* Start of description section. */ Maven jar</description> -- -- end of description section.
<properties>
<java.version> /* Start of java version section. */ 1.8</java.version>
</properties>
<dependencies> -- Start of dependencies section.
<dependency> -- start of dependency section.
<groupId> /* Start of groupid section. */ org.springframework.boot</groupId> -- -- end of groupid section.
<artifactId> /* Start of artifactId section. */ spring-boot-starter-web</artifactId>
</dependency> -- end of dependency section.
</project> -- end of project section.
# maven package
Output:
Example #2
Using a custom manifest file to create project using the maven jar plugin.
- The below example shows the use of a custom manifest file to create a project using it.
Code:
<build> -- Start of build section.
<plugin> -- Start of second plugin section.
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration> -- Start of configuration section.
<archive> -- Start of archive section.
<index>true</index>
<manifest> -- Start of manifest section.
</manifest>
<manifestEntries> -- Start of manifestEntries section.
<javacodegeeks> /* Start of javacodegeeks section */ maven jar plugin example</javacodegeeks>
<codification> /* Start of codification section */ ${project.build.sourceEncoding}</codification> -- End of codification section.
<key>maven jar plugin </key>
</manifestEntries> -- End of manifestEntries section.
</archive> -- End of archive section.
</configuration> -- End of configuration section.
</plugin> -- End of plugin section.
</build> -- End of build section.
# maven package
Output:
Conclusion
It is used to build the jar file. If suppose we have defined our project as jar file, the maven project will implicitly invoke this plugin. Model version, groupId, artifactId, version and packaging are important parameters while creating it.
Recommended Articles
We hope that this EDUCBA information on “Maven Jar Plugin” was beneficial to you. You can view EDUCBA’s recommended articles for more information.