1. Overview
1.概述
The packaging type is an important aspect of any Maven project. It specifies the type of artifact the project produces. Generally, a build produces a jar, war, pom, or other executable.
包装类型是任何Maven项目的一个重要方面。它规定了项目产生的工件的类型。一般来说,构建时会产生jar、war、pom或其他可执行文件。
Maven offers many default packaging types and also provides the flexibility to define a custom one.
Maven提供了许多默认的打包类型,同时也提供了定义自定义类型的灵活性。
In this tutorial, we’ll take a deep dive into Maven packaging types. First, we’ll look at the build lifecycles in Maven. Then, we’ll discuss each packaging type, what they represent, and their effect on the project’s lifecycle. In the end, we’ll see how to define a custom packaging type.
在本教程中,我们将深入了解Maven的打包类型。首先,我们来看看Maven的构建生命周期。然后,我们将讨论每种打包类型,它们代表什么,以及它们对项目生命周期的影响。最后,我们将看到如何定义一个自定义的打包类型。
2. Default Packaging Types
2.默认的包装类型
Maven offers many default packaging types that include a jar, war, ear, pom, rar, ejb, and maven-plugin. Each packaging type follows a build lifecycle that consists of phases. Usually, every phase is a sequence of goals and performs a specific task.
Maven提供了许多默认的打包类型,包括jar、war、ear、pom、rar、ejb以及maven-plugin。每种打包类型都遵循一个由阶段组成的构建生命周期。通常情况下,每个阶段都是一个目标序列,并执行一项特定的任务。
Different packaging types may have different goals in a particular phase. For example, in the package phase of jar packaging type, maven-jar-plugin‘s jar goal is executed. Conversely, for a war project, maven-war-plugin‘s war goal is executed in the same phase.
不同的打包类型在某一阶段可能有不同的目标。例如,在jar打包类型的打包阶段,maven-jar-plugin的jar目标被执行。反之,对于war项目,maven-war-plugin的war目标在同一阶段执行。
2.1. jar
2.1. jar
Java archive – or jar – is one of the most popular packaging types. Projects with this packaging type produce a compressed zip file with the .jar extension. It may include pure Java classes, interfaces, resources, and metadata files.
Java归档–或jar–是最流行的打包类型之一。采用这种打包类型的项目会产生一个以.jar为扩展名的压缩zip文件。它可能包括纯Java类、接口、资源和元数据文件。
To begin with, let’s look at some of the default goal-to-build-phase bindings for the jar:
首先,让我们看看jar的一些默认目标到构建阶段的绑定。
- resources: resources
- compiler: compile
- resources: testResources
- compiler: testCompile
- surefire: test
- jar: jar
- install: install
- deploy: deploy
Without delay, let’s define the packaging type of a jar project:
毋庸置疑,让我们定义jar项目的打包类型。
<packaging>jar</packaging>
If nothing has been specified, Maven assumes the packaging type is a jar.
如果没有指定,Maven会假定打包类型为jar。。
2.2. war
2.2. 战争
Simply put, a web application archive – or war – contains all files related to a web application. It may include Java servlets, JSPs, HTML pages, a deployment descriptor, and related resources. Overall, war has the same goal bindings as a jar, but with one exception —the package phase of the war has a different goal, which is war.
简单地说,网络应用程序档案–或称war–包含与网络应用程序有关的所有文件。它可能包括Java servlets、JSP、HTML页面、部署描述符和相关资源。总的来说,war与jar具有相同的目标绑定,但有一个例外–war的打包阶段具有不同的目标,那就是war。
Without a doubt, jar and war are the most popular packaging types in the Java community. A detailed difference between these two might be an interesting read.
毫无疑问,jar和war是Java社区中最受欢迎的打包类型。详细的这两种之间的区别可能是一个有趣的阅读。
Let’s define the packaging type of a web application:
让我们来定义一个网络应用程序的包装类型。
<packaging>war</packaging>
The other packaging types ejb, par, and rar also have similar lifecycles, but each has a different package goal.
其他打包类型ejb、par和rar也有类似的生命周期,但每一种都有不同的打包目标。
ejb:ejb or par:par or rar:rar
2.3. ear
2.3. 耳朵
Enterprise application archive – or ear – is a compressed file that contains a J2EE application. It consists of one or more modules that can be either web modules (packaged as a war file) or EJB modules (packaged as a jar file) or both of them.
企业应用程序归档–或ear–是一个包含J2EE应用程序的压缩文件。它由一个或多个模块组成,这些模块可以是 Web 模块(打包成 war 文件)或 EJB 模块(打包成 jar 文件),或者两者都是。
To put it differently, the ear is a superset of jars and wars and requires an application server to run the application, whereas war requires only a web container or webserver to deploy it. The aspects that distinguish a web server from an application server, and what those popular servers are in Java, are important concepts for a Java developer.
换句话说,ear是jars和wars的超集,需要一个应用服务器来运行应用程序,而war只需要一个Web容器或Web服务器来部署。区分 Web 服务器和应用服务器的方面,以及那些Java 中流行的服务器是什么,对于一个 Java 开发者来说是很重要的概念。
Let’s define the default goal bindings for the ear:
让我们为ear定义默认的目标绑定。
- ear: generate-application-xml
- resources: resources
- ear: ear
- install: install
- deploy: deploy
Here’s how we can define the packaging type of such projects:
以下是我们如何定义此类项目的包装类型。
<packaging>ear</packaging>
2.4. pom
2.4 pom
Among all packaging types, pom is the simplest one. It helps to create aggregators and parent projects.
在所有打包类型中,pom是最简单的一种。它有助于创建聚合器和父项目。
An aggregator or multi-module project assembles submodules coming from different sources. These submodules are regular Maven projects and follow their own build lifecycles. The aggregator POM has all the references of submodules under the modules element.
聚合器或多模块项目集合了来自不同来源的子模块。这些子模块是普通的Maven项目,遵循自己的构建生命周期。聚合器POM的modules元素下有所有子模块的引用。
A parent project allows you to define the inheritance relationship between POMs. The parent POM shares certain configurations, plugins, and dependencies, along with their versions. Most elements from the parent are inherited by its children — exceptions include artifactId, name, and prerequisites.
父项目允许你定义POM之间的继承关系。父POM共享某些配置、插件和依赖,以及它们的版本。父项目的大多数元素都被其子项目继承–例外情况包括artifactId、name和prerequisites。
Because there are no resources to process and no code to compile or test. Hence, the artifacts of pom projects generate themselves instead of any executable.
因为没有资源需要处理,没有代码需要编译或测试。因此,pom项目的工件是自己生成的,而不是任何可执行的。
Let’s define the packaging type of a multi-module project:
让我们来定义一个多模块项目的包装类型。
<packaging>pom</packaging>
Such projects have the simplest lifecycle that consists of only two steps: install and deploy.
此类项目具有最简单的生命周期,只包括两个步骤。安装和部署。
2.5. maven-plugin
2.5.maven-plugin
Maven offers a variety of useful plugins. However, there might be cases when default plugins are not sufficient enough. In this case, the tool provides the flexibility to create a maven-plugin, according to project needs.
Maven提供了各种有用的插件。然而,在某些情况下,默认插件可能还不够用。在这种情况下,该工具提供了灵活性,可以根据项目需要,创建一个maven-plugin。
To create a plugin, set the packaging type of the project:
要创建一个插件,要设置项目的包装类型。
<packaging>maven-plugin</packaging>
The maven-plugin has a lifecycle similar to jar‘s lifecycle, but with two exceptions:
maven-plugin的生命周期与jar的生命周期类似,但有两个例外。
- plugin: descriptor is bound to the generate-resources phase
- plugin: addPluginArtifactMetadata is added to the package phase
For this type of project, a maven-plugin-api dependency is required.
对于这种类型的项目,需要一个maven-plugin-api依赖项。
2.6. ejb
2.6.ejb
Enterprise Java Beans – or ejb – help to create scalable, distributed server-side applications. EJBs often provide the business logic of an application. A typical EJB architecture consists of three components: Enterprise Java Beans (EJBs), the EJB container, and an application server.
企业Java Bean–或ejb–有助于创建可扩展的、分布式服务器端应用程序。EJB经常提供一个应用程序的业务逻辑。一个典型的EJB架构由三个部分组成。企业Java Bean(EJB)、EJB容器和应用服务器。
Now, let’s define the packaging type of the EJB project:
现在,我们来定义EJB项目的打包类型。
<packaging>ejb</packaging>
The ejb packaging type also has a similar lifecycle as jar packaging, but with a different package goal. The package goal for this type of project is ejb:ejb.
ejb打包类型也有与jar打包相似的生命周期,但有不同的打包目标。这种类型的项目的打包目标是ejb:ejb。
The project, with ejb packaging type, requires a maven-ejb-plugin to execute lifecycle goals. Maven provides support for EJB 2 and 3. If no version is specified, then default version 2 is used.
该项目采用ejb打包类型,需要一个maven-ejb-plugin来执行生命周期目标。Maven提供对EJB 2和3的支持。如果没有指定版本,则使用默认版本2。
2.7. rar
2.7. rar
Resource adapter – or rar – is an archive file that serves as a valid format for the deployment of resource adapters to an application server. Basically, it is a system-level driver that connects a Java application to an enterprise information system (EIS).
资源适配器–或称rar–是一个存档文件,作为向应用服务器部署资源适配器的有效格式。基本上,它是一个系统级驱动程序,将一个Java应用程序连接到企业信息系统(EIS)。
Here’s the declaration of packaging type for a resource adapter:
下面是资源适配器的包装类型声明。
<packaging>rar</packaging>
Every resource adapter archive consists of two parts: a jar file that contains source code and a ra.xml that serves as a deployment descriptor.
每个资源适配器存档由两部分组成:一个包含源代码的jar文件和一个作为部署描述符的ra.xml。
Again, the lifecycle phases are the same as a jar or war packaging with one exception: The package phase executes the rar goal that consists of a maven-rar-plugin to package the archives.
同样,生命周期阶段与jar或war打包相同,只有一个例外。 package 阶段执行rar目标,该目标由maven-rar-plugin 来打包存档。
3. Other Packaging Types
3.其他包装类型
So far, we’ve looked at various packaging types that Maven offers as default. Now, let’s imagine we want our project to produce an artifact with a .zip extension. In this case, the default packaging types can’t help us.
到目前为止,我们已经了解了Maven默认提供的各种打包类型。现在,让我们设想一下,我们希望我们的项目能产生一个带有.zip扩展名的工件。在这种情况下,默认的打包类型无法帮助我们。
Maven also provides some more packaging types through plugins. With the help of these plugins, we can define a custom packaging type and its build lifecycle. Some of these types are:
Maven还通过插件提供了一些更多的打包类型。在这些插件的帮助下,我们可以定义自定义打包类型及其构建生命周期。其中一些类型包括。
- msi
- rpm
- tar
- tar.bz2
- tar.gz
- tbz
- zip
To define a custom type, we have to define its packaging type and phases in its lifecycle. For this, create a components.xml file under the src/main/resources/META-INF/plexus directory:
要定义一个自定义类型,我们必须定义它的包装类型和生命周期中的阶段。为此,在src/main/resources/META-INF/plexus目录下创建一个components.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>
Until now, Maven doesn’t know anything about our new packaging type and its lifecycle. To make it visible, let’s add the plugin in the pom file of the project and set extensions to true:
到目前为止,Maven对我们的新包装类型及其生命周期一无所知。为使其可见,我们在项目的pom文件中添加该插件,并将extensions设为true。
<plugins>
<plugin>
<groupId>com.baeldung.maven.plugins</groupId>
<artifactId>maven-zip-plugin</artifactId>
<extensions>true</extensions>
</plugin>
</plugins>
Now, the project will be available for a scan, and the system will look into plugins and components.xml file, too.
现在,该项目将可用于扫描,系统也将查看plugins和components.xml文件。
Other than all these types, Maven offers a lot of other packaging types through external projects and plugins. For example, nar (native archive), swf, and swc are packaging types for the projects that produce Adobe Flash and Flex content. For such projects, we need a plugin that defines custom packaging and a repository that contains the plugin.
除了所有这些类型,Maven还通过外部项目和插件提供了很多其他打包类型。例如,nar(本地存档)、swf和swc是制作Adobe Flash和Flex内容的项目的打包类型。对于这样的项目,我们需要一个定义自定义打包的插件和一个包含该插件的资源库。
4. Conclusion
4.总结
In this article, we looked at various packaging types available in Maven. Also, we got familiar with what these types represent and how they differ in their lifecycles. In the end, we also learned how to define a custom packaging type and customize the default build lifecycle.
在这篇文章中,我们研究了Maven中的各种打包类型。此外,我们还熟悉了这些类型所代表的内容,以及它们在生命周期中的区别。最后,我们还学习了如何定义自定义打包类型和自定义默认的构建生命周期。
All code examples on Baeldung are built using Maven. Be sure to check out our various Maven configurations over 0n GitHub.