• notice
  • Congratulations on the launch of the Sought Tech site

Maven packaging type

1 Overview

Packaging type is an important aspect of any Maven project. It specifies the type of artifacts produced by the project. Typically, the building generates jarwarpomor other executable files.

Maven provides many default packaging types, and also provides the flexibility to define custom packaging types.

In this tutorial, we will delve into Maven's packaging types. First, let's take a look at the build life cycle in Maven. Then, we will discuss each type of packaging, what they represent and their impact on the project life cycle. Finally, we will see how to define a custom packaging type.

2.Default packaging type

Maven provides a number of default package types, including jarwarearpomrarejband maven-pluginEach packaging type follows a build life cycle composed of phases. Usually, each stage is a series of goals and performs specific tasks.

At a specific stage, different packaging types may have different goals. For example, in jarthe packaging stage of the packaging type, maven-jar-pluginthe jar target will be executed . On the contrary, for warprojects, maven-war-pluginthe war goals are executed in the same phase.

2.1. jar

Java archive files (or jar) are one of the most popular types of packaging. Projects with this type of packaging will generate an extension .jarwhich may include pure Java classes, interfaces, resources and metadata files.

jarSome default targets are bound to the build phase:

  • resource: resources

  • translater: compile

  • resource: testResources

  • translater: testCompile

  • surefire: test

  • Jar: jar

  • Install: install

  • deploy: deploy

Without delay, let us define jarthe packaging type of a project:

<packaging>jar</packaging>

If nothing is specified, Maven assumes that the packaging type isjar.

2.2. war

In short, a web application archive file (or warfile) contains all the files related to the web application. It may include Java Servlet, JSP, HTML page, deployment descriptor and related resources. Overall, the war jarsame target binding, but with one exception- warthe packaging stage has different targets, ie war.

Undoubtedly, jarand waris the most popular type of packaging in the Java community. The detailed difference between the two may be an interesting reading.

Let's define the packaging type of the web application:

<packaging>war</packaging>

Other types of packaging ejbparand raralso has a similar life cycle, but each package has a different packaging goals.

ejb: ejb or par: par or rar: rar

2.3. ear

Enterprise application archive files (ie ear) are compressed files that contain J2EE applications. It consists of one or more modules.These modules can be Web modules (packaged as warfiles) or EJB modules (packaged as jarfiles), or these two modules.

ear jarsAnd warsa superset need an application server to run the application, and warrequires only a Web container or webserver can be deployed. For Java developers, the aspects that distinguish web servers from application servers and what those popular servers are in Java are important concepts.

earThe default target binding:

  • ear: generate-application-xml

  • resource: resources

  • ear: ear

  • Install: install

  • deploy: deploy

This is how we define the type of packaging for such items:

<packaging>ear</packaging>

2.4. pom

Among all packaging types, it pomis the simplest one. It helps to create aggregators and parent projects.

Aggregators or multi-module projects will assemble sub-modules from different sources . These submodules are regular Maven projects and follow their own build life cycle. modulesThere are all references to submodules under the element.

The parent project allows you to define the inheritance relationship between POMs. The parent POM shares certain configurations, plugins and dependencies and their versions. Most elements from the parent are inherited by their children-exceptions include artifactIdnameand prerequisites.

Because there are no resources to process, and no code to compile or test. Therefore, the artifacts of the pom project will be generated by itself instead of generating any executable files.

Let's define the packaging type of a multi-module project:

<packaging>pom</packaging>

Such a project has the simplest life cycle, which includes only two steps: installand deploy.

2.5. maven-plugin

Maven provides various useful plug-ins. However, it may happen that the default plugins are not enough. In this case, the tool can flexibly create Maven plug-ins based on project requirements.

To create a plug-in, set the packaging type of the project:

<packaging>maven-plugin</packaging>

maven-pluginThe life cycle of is similar to jarthe life cycle, with two exceptions:

  • Plug-in: descriptorbind to the generate-resources stage

  • Plug-in: addPluginArtifactMetadataadded to the packaging stage

For such projects, maven-plugin-apidependencies are required .

2.6. ejb

Enterprise Java Bean (or ejb) helps to create scalable distributed server-side applications. EJB usually provides the business logic of the application. A typical EJB architecture consists of three components: Enterprise Java Bean (EJB), EJB container and application server.

Now, let us define the packaging type of the EJB project:

<packaging>ejb</packaging>

ejbPackaging types also have a jarsimilar life cycle to packaging, but the packaging goals are different. Packaging objective of such projects is the EJB: ejb.

ejbPackage type projects maven-ejb-pluginto implement life cycle goals. Maven provides support for EJB 2 and 3. If the version is not specified, the default version 2 is used.

2.7. rar

A resource adapter (or rar) is an archive file used as an effective format for deploying a resource adapter to an application server. Basically, it is a system-level driver that connects Java applications to an enterprise information system (EIS).

This is the declaration of the packaging type of the resource adapter:

<packaging>rar</packaging>

Each resource adapter archive consists of two parts: one contains source code jarfiles and one is used as a deployment descriptorra.xml

Similarly, the life cycle phases are the same one jaror warthe packaging with one exception: during the **package**phase execution rar, it is **maven-rar-plugin**packaged and archived by a target ** . **

3.Other packaging types

So far, we have studied the various packaging types that Maven provides by default. Now, let's imagine that we want our project to generate an extension. .zipIn this case, the default packaging type will not help.

Maven also provides more packaging types through plugins. With the help of these plugins, we can define custom packaging types and their build life cycle. Some of these types are:

  • msi

  • rpm

  • tar

  • tar.bz2

  • tar.gz

  • tbz

  • zip

To define a custom type, we must define it packaging typeand in the life cycle for phases .this, please in the src/main/resources/META-INF/plexuscatalogcomponents.xml

<component>
<role>org.apache.maven.lifecycle.mapping.LifecycleMapping</role>
<role-hint>zip</role-hint>
<implementation>org.apache.maven.lifecycle.mapping.DefaultLifecycleMapping</implementation>
<configuration>
<phases>
<process-resources>org.apache.maven.plugins:maven-resources-plugin:resources</process-resources>
<package>com.baeldung.maven.plugins:maven-zip-plugin:zip</package>
<install>org.apache.maven.plugins:maven-install-plugin:install</install>
<deploy>org.apache.maven.plugins:maven-deploy-plugin:deploy</deploy>
</phases>
</configuration>
</component>

So far, Maven knows nothing about our new packaging type and its life cycle. To make it visible, let's pom extensionsset the project to true:

<plugins>
<plugin>
<groupId>com.baeldung.maven.plugins</groupId>
<artifactId>maven-zip-plugin</artifactId>
<extensions>true</extensions>
</plugin>
</plugins>

Now, the item will be available for scanning, and the system will also search pluginsand compnenets.xmlfiles.

In addition to all these types, Maven also provides many other packaging types through external projects and plugins. For example, nar(native archive), swfand swcare the packaging types for projects that produce Adobe Flash and Flex content. For this type of project, we need a plugin that defines a custom packaging and a repository that contains the plugin.

4. in conclusion

In this article, we looked at the various packaging types available in Maven. In addition, we are familiar with what these types represent and how they differ in the life cycle. Finally, we also learned how to define custom packaging types and custom default build life cycles.


Tags

Technical otaku

Sought technology together

Related Topic

1 Comments

author

order lipitor 10mg generic & lt;a href="https://lipiws.top/"& gt;buy lipitor 20mg for sale& lt;/a& gt; lipitor 20mg generic

Hkpfzb

2024-03-08

Leave a Reply

+