1. Overview
1.概述
Manually building a complex project is quite cumbersome. There are much easier ways to do this using build tools. As we know, one of the main build tools for Java projects is Maven. Maven helps standardize the build and deployment of applications.
手动构建一个复杂的项目是相当麻烦的。使用构建工具来做这件事要容易得多。 正如我们所知,Java项目的主要构建工具之一是Maven。Maven有助于实现应用程序构建和部署的标准化。
In this tutorial, we’ll discuss what a Maven artifact is and what its key elements are. We’ll also look at Maven coordinates, dependency management, and finally, Maven repositories.
在本教程中,我们将讨论什么是Maven工件以及它的关键元素。我们还将探讨Maven坐标、依赖性管理,最后是Maven仓库。
2. What Is Maven?
2.什么是Maven?
We can use Maven to build and manage any Java-based project. It provides many functionalities such as:
我们可以使用Maven来构建和管理任何基于Java的项目。它提供了许多功能,例如。
- building and compiling
- documentation and report
- dependency management
- source management
- project update
- deployment
Each Maven project has its own POM file. We can configure Maven to build either one project or multiple projects.
每个Maven项目都有自己的POM文件。我们可以配置Maven来构建一个项目或多个项目。
A multi-project typically defines a POM file in the root directory and lists the individual projects in a modules section. In short, a Maven build produces one or more artifacts.
一个多项目通常在根目录下定义一个POM文件,并在modules部分列出各个项目。简而言之,一个Maven构建会产生一个或多个工件。
3. What Is a Maven Artifact?
3.什么是Maven工件?
An artifact is an element that a project can either use or produce. In Maven terminology, an artifact is an output generated after a Maven project build. It can be, for example, a jar, war, or any other executable file.
工件是一个项目可以使用或产生的元素。在Maven术语中,工件是Maven项目构建后产生的输出。jar、war或任何其他可执行文件。
Also, Maven artifacts include five key elements, groupId, artifactId, version, packaging, and classifier. Those are the elements we use to identify the artifact and are known as Maven coordinates.
另外,Maven工件包括五个关键元素:groupId、artifactId、version、packaging和classifier。这些都是我们用来识别工件的元素,被称为Maven坐标。
4. Maven Coordinates
4.Maven坐标
Maven coordinate is a combination of the values for the groupId, artifactId, and version for a given artifact. Furthermore, Maven uses coordinates to find any component matching the values for groupId, artifactId, and version.
Maven坐标是特定工件的groupId、artifactId和version值的组合。此外,Maven使用坐标来寻找与groupId、artifactId和version的值相匹配的任何组件。
Among the coordinate elements, we must define the groupId, artifactId, and version. The packaging element is optional, and we can’t directly define the classifier.
在坐标元素中,我们必须定义groupId、artifactId和version。packaging元素是可选的,而且我们不能直接定义classifier。
For instance, the pom.xml configuration file below shows an example of Maven coordinates:
例如,下面的pom.xml配置文件显示了一个Maven坐标的例子。
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.baeldung</groupId>
<artifactId>org.baeldung.java</artifactId>
<packaging>jar</packaging>
<version>1.0.0-SNAPSHOT</version>
<name>org.baeldung.java</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.1.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Let’s look in detail at each of the Maven coordinates.
让我们详细了解一下Maven的每一个坐标。
4.1. The groupId Element
4.1.groupId元素
The groupId element is the identifier of the group at the origin of the project. This key makes it easier and quicker to organize and find the project.
groupId元素是项目起源处的组的标识符。这个关键使组织和寻找项目变得更容易、更快捷。
Also, a groupId follows the same naming rules as Java packages, and we generally choose as groupId the name of the top package of the project.
另外,groupId遵循与Java包相同的命名规则,我们一般选择项目的顶级包的名称作为groupId。
For instance, a groupId of org.apache.commons corresponds to ${repository_home}/org/apache/commons.
例如,groupId为org.apache.commons,对应于${repository_home}/org/apache/commons.。
4.2. The artifactId Element
4.2.artifactId元素
The artifactId element is the identifier of the project within the group. It is used by default to construct the artifact’s final name. Therefore, this name has some specifications as it should ideally be small in length. The best practice for naming an artifactId is to use the actual project name as a prefix. The advantage of this is that it makes it easier to find the artifacts.
artifactId元素是该组中项目的标识符。它被默认用于构建工件的最终名称。因此,这个名字有一些规范,因为它的长度最好是小的。命名artifactId的最佳做法是使用实际的项目名称作为前缀。这样做的好处是,它可以让人更容易找到工件。
Like the groupId, the artifactId manifests itself as a sub-directory under the directory tree that represents the groupId.
与groupId一样,artifactId表现为目录树下的一个子目录,代表groupId。
For example, an artifactId of commons-lang3 under a groupId of org.apache.commons, would determine that the artifact is under : ${repository_home}/org/apache/commons/commons-lang3/.
例如,artifactId为commons-lang3,groupId为org.apache.commons,,将确定该工件是在:${repository_home}/org/apache/commons/commons-lang3/。
4.3. The version Element
4.3.版本元素
The version is used as part of an artifact’s identifier. It defines the current version of the Maven project. We should note that Maven defines a set of version specifications, as well as the concept of releases and snapshots, that we will cover later.
version被用作工件标识符的一部分。它定义了Maven项目的当前版本。我们应该注意,Maven定义了一套版本规范,以及发布和快照的概念,我们将在后面介绍。
A version is represented as a subdirectory in the directory tree, which is formed by the groupId and the artifactId.
一个版本被表示为目录树中的一个子目录,它由groupId和artifactId组成。
For instance, a version of 3.1.1 for an artifactId commons-lang3 under the groupId of org.apache.commons would determine that the artifact is located under: ${repository_home}/org/apache/commons/commons-lang3/3.1.1/.
例如,在groupId 的org.apache.commons下,artifactId commons-lang3的版本为3.1.1,将确定该工件位于。${repository_home}/org/apache/commons/commons-lang3/3.1.1/.。
4.4. The packaging Element
4.4.包装元素
This element is used to specify the type of artifact generated by the project. The packaging can be anything that describes any binary software format, including ZIP, EAR, WAR, SWC, NAR, SWF, SAR.
这个元素用来指定项目产生的工件的类型。包装可以是任何描述任何二进制软件格式的东西,包括ZIP, EAR, WAR, SWC, NAR, SWF, SAR.。
Also, the packaging defines the different goals to execute during the project’s default lifecycle. For instance, the package phase executes the jar:jar goal for a jar-type artifact and the war:war goal for a war-type artifact.
另外,packaging定义了在项目的默认生命周期内执行的不同目标。例如,打包阶段对一个jar类型的工件执行jar:jar目标,对一个war类型的工件执行war:war目标。
4.5. The classifier Element
4.5.分类器元素
We usually use a classifier for technical reasons when delivering the same code but as several separate artifacts.
在交付相同的代码但作为几个独立的工件时,我们通常出于技术原因使用分类器。
For example, if we want to build two artifacts of a JAR with different Java compilers, we can do it easily using the classifier as it allows the production of two different artifacts with the same combination for groupId:artifactId:version.
例如,如果我们想用不同的Java编译器构建两个JAR工件,我们可以使用分类器轻松完成,因为它允许用groupId:artifactId:version的相同组合来生产两个不同的工件。
Also, we can use classifiers when packaging source code, an artifact’s JavaDoc, or assembling binaries.
另外,在打包源代码、工件的JavaDoc或组装二进制文件时,我们可以使用分类器s。
For our above example of commons-lang3, the artifact to look for is: commons-lang3-3.10-javadoc.jar or commons-lang3-3.10-sources.jar, under ${repository_home}/org/apache/commons/commons-lang3/3.1.0/.
对于我们上面的commons-lang3的例子,要寻找的工件是。commons-lang3-3.10-javadoc.jar或commons-lang3-3.10-sources.jar,在${repository_home}/org/apache/commons/lang3/3.1.0/.下面。
5. Release vs. Snapshot Artifacts
5.发布与快照工件
Now, let’s see the differences between a snapshot artifact and a release artifact.
现在,让我们看看快照工件和发布工件之间的区别.。
5.1. Release Artifacts
5.1.发布工件
A release artifact indicates that the version is stable and can be used outside development processes like integration tests, customer qualification, pre-production, etc.
一个发布版工件表明该版本是稳定的并且可以在开发过程之外使用,如集成测试、客户鉴定、预生产等等。
Also, a release artifact is unique. Running the mvn deploy command will deploy our project to the repository. But, subsequent execution of the same command on the same project with the same version will result in a failure.
另外,release artifact是唯一的。运行mvn deploy命令将把我们的项目部署到版本库中。但是,随后在同一项目上以相同的版本执行同一命令将导致失败。
5.2. Snapshot Artifacts
5.2.快照工件
A snapshot artifact indicates that the project is under development. When we install or publish a component, Maven checks the version attribute. If it contains the string “SNAPSHOT”, Maven converts this key into a date and time value in UTC (Coordinated Universal Time) format.
快照工件表明该项目正在开发中。当我们安装或发布一个组件时,Maven会检查version属性。如果它包含字符串 “SNAPSHOT”,Maven会将这个键转换为UTC(协调世界时)格式的日期和时间值。
For example, if our project is in version 1.0-SNAPSHOT and we deploy its artifacts on a Maven repository, Maven converts this version to “1.0-202202019-230800”, assuming we deploy on February 19, 2022, at 23:08 UTC.
例如,如果我们的项目是1.0-SNAPSHOT版本,我们在Maven资源库上部署其工件,Maven会将该版本转换为 “1.0-202202019-230800″,假设我们在2022年2月19日23:08 UTC部署。
In other words, when we deploy a snapshot, we are not delivering a software component, but we only deliver a snapshot of it.
换句话说,当我们部署一个快照时,我们不是在交付一个软件组件,而只是交付它的一个快照。
6. Dependency Management
6.依赖性管理
Dependency management is essential in the Maven world. For instance, when a project depends on other classes in its operating process (compilation, execution), it becomes necessary to identify and import these dependencies from the remote repositories to the local repository. Therefore, the project will have dependencies on these libraries, which will ultimately be added to the classpath of the project.
依赖性管理在Maven世界中至关重要。例如,当一个项目在运行过程中依赖其他类时(编译、执行),就有必要识别并将这些依赖关系从远程库导入本地库。因此,项目会有对这些库的依赖,这些库最终会被添加到项目的classpath中。
Furthermore, Maven’s dependency management is based on several concepts:
此外,Maven的依赖性管理是基于几个概念。
- repositories: essential to store artifacts
- scope: allows us to specify in which context we use a dependency
- transitivity: allows us to manage the dependencies of dependencies
- inheritance: POMs inheriting from the parent can set their dependency by providing only the dependency’s groupId and artifactId without the version attribute. Maven gets the appropriate version from the parent POM file.
With Maven, dependency management is done through the pom.xml. For instance, a dependency declaration in the Maven projects looks like this:
在Maven中,依赖性管理是通过pom.xml完成的。例如,Maven项目中的依赖声明看起来像这样。
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>3.8.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.3.15</version>
</dependency>
</dependencies>
7. Maven Repository
7.Maven资源库
Maven uses a repository to store elements like dependencies and plugins required to build projects. This makes it possible to centralize these elements, which are generally used in several projects.
Maven使用一个资源库来存储构建项目所需的依赖和插件等元素。这使得集中这些元素成为可能,这些元素通常在多个项目中使用。
As we mentioned earlier, repositories store artifacts using a set of coordinates: groupId, artifactId, version, and packaging. Furthermore, Maven uses a particular directory structure to organize the contents of a repository and allow it to find the required elements: ${repository_home}/groupId/artifactId/version.
正如我们前面提到的,存储库使用一组坐标来存储工件。groupId、artifactId、version、packaging。此外,Maven使用特定的目录结构来组织版本库的内容,使其能够找到所需元素。${repository_home}/groupId/artifactId/version。
For instance, let’s consider this POM configuration.
例如,让我们考虑这个POM配置。
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>myApp</artifactId>
<version>1.0.0</version>
</project>
With the above configuration, our project will be stored in a repository in the ${repository_home}/com/baeldung/myApp/1.0.0/ path.
通过上述配置,我们的项目将被存储在${repository_home}/com/baeldung/myApp/1.0.0/路径下的一个仓库中。
8. Conclusion
8.结论
In this article, we discussed the concept of Maven artifacts and their coordinates system. Also, we learned related concepts such as dependencies and repositories.
在这篇文章中,我们讨论了Maven工件的概念及其坐标系统。此外,我们还学习了相关概念,如依赖关系和资源库。