1. Overview
1.概述
The Maven Wrapper is an excellent choice for projects that need a specific version of Maven (or for users that don’t want to install Maven at all). Instead of installing many versions of it in the operating system, we can just use the project-specific wrapper script.
Maven包装器是需要特定版本Maven的项目(或者根本不想安装Maven的用户)的绝佳选择。我们不必在操作系统中安装许多版本,只需使用项目专用的包装器脚本即可。
In this quick article, we’ll show how to set up a Maven Wrapper for an existing Maven project.
在这篇快速文章中,我们将展示如何为现有的Maven项目设置Maven Wrapper。
2. Setting Up the Maven Wrapper
2.设置Maven包装器
There’re two ways to configure it in a project, where the simplest one is to use an appropriate plugin to automate it or by applying the manual installation.
在一个项目中,有两种方法来配置它,其中最简单的是使用一个适当的插件来自动化,或者通过应用手动安装。
2.1. Plugin
2.1.插件
Let’s use this Maven Wrapper plugin to make auto installation in a simple Spring Boot project.
让我们使用这个Maven Wrapper插件,在一个简单的Spring Boot项目中进行自动安装。
First, we need to go in the main folder of the project and run this command:
首先,我们需要进入项目的主文件夹,运行这个命令。
mvn -N wrapper:wrapper
We can also specify the version of Maven:
我们还可以指定Maven的版本。
mvn -N wrapper:wrapper -Dmaven=3.5.2
The option -N means –non-recursive so that the wrapper will only be applied to the main project of the current directory, not in any submodules.
选项-N表示-non-recursive,因此包装器将只应用于当前目录的主项目,而不是任何子模块。
After executing the goal, we’ll have more files and directories in the project:
执行目标后,我们将在项目中拥有更多的文件和目录。
- mvnw: it’s an executable Unix shell script used in place of a fully installed Maven
- mvnw.cmd: it’s the Batch version of the above script
- mvn: the hidden folder that holds the Maven Wrapper Java library and its properties file
2.2. Manual
2.2.手册
With a manual approach, we can copy files and folders seen above from another project to the main folder of the current project.
通过手动方式,我们可以将上面看到的文件和文件夹从另一个项目复制到当前项目的主文件夹。
Afterwards, we need to specify the version of Maven to use in the wrapper properties file located in .mvn/wrapper/maven-wrapper.properties file.
之后,我们需要在位于.mvn/wrapper/maven-wrapper.properties文件的包装器属性文件中指定要使用的Maven版本。
For instance, our properties file has the following line:
例如,我们的属性文件有以下一行。
distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.2/apache-maven-3.5.2-bin.zip
Consequently, the version 3.5.2 will be downloaded and used.
因此,将下载并使用3.5.2版本。
3. Use Cases
3.使用案例
The wrapper should work with different operating systems such as:
该包装器应能在不同的操作系统中工作,如:。
- Linux
- OSX
- Windows
- Solaris
After that, we can run our goals like this for the Unix system:
之后,我们可以像这样为Unix系统运行我们的目标。
./mvnw clean install
And the following command for Windows:
而在Windows下则使用以下命令。
mvnw.cmd clean install
If we don’t have the specified Maven in the wrapper properties, it’ll be downloaded and installed in the folder $USER_HOME/.m2/wrapper/dists of the system.
如果我们的包装器属性中没有指定的Maven,它将被下载并安装在系统的$USER_HOME/.m2/wrapper/dists文件夹中。
Let’s run our Spring-Boot project:
让我们来运行我们的Spring-Boot项目。
./mvnw spring-boot:run
The output is the same as for a fully installed Maven:
输出结果与完全安装的Maven相同。
Note: we use the executable mvnw in place of mvn, which stands now as the Maven command line program.
注意:我们使用可执行文件mvnw来代替mvn,它现在作为Maven的命令行程序。
Now that we know what Maven wrapper is, let’s answer one of the common questions: should mvnw files be added to our projects?
现在我们知道了什么是Maven包装器,让我们来回答一个常见问题:mvnw文件是否应该添加到我们的项目中?
The short answer is no. mvnw files are not necessarily part of our projects. However, including them could be beneficial. For example, it will allow anyone cloning our project to build it without installing Maven.
简短的回答是没有。mvnw文件不一定是我们项目的一部分。然而,包括它们可能是有益的。例如,这将使任何克隆我们项目的人都能在不安装Maven的情况下构建该项目。
4. Conclusion
4.结论
In this tutorial, we’ve seen how to set up and use Maven Wrapper in a Maven project.
在本教程中,我们已经看到了如何在Maven项目中设置和使用Maven Wrapper。
As always, the source code for this article can be found over on GitHub.
像往常一样,本文的源代码可以在GitHub上找到over。