1. Overview
1.概述
In this tutorial, we’ll learn how to convert a Maven build to a Gradle build. For this purpose, we will use the gradle init command on an existing Maven project.
在本教程中,我们将学习如何将Maven构建转换为Gradle构建。为此,我们将在一个现有的Maven项目上使用gradle init命令。
2. Gradle Setup
2.Gradle设置
Let’s install Gradle on our machine by downloading a Gradle distribution and following the specified instructions. We can also take a deep dive to learn more about Gradle.
让我们通过下载Gradle发行版并按照指定的说明在我们的机器上安装Gradle。我们还可以深入到了解更多关于Gradle的信息。
3. Maven Build File
3.Maven构建文件
Let’s start with a standard Maven Java project, which the following pom.xml file in its root directory:
让我们从一个标准的Maven Java项目开始,它的根目录下有以下pom.xml文件。
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>maven-to-gradle</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
</dependencies>
</project>
4. Converting to Gradle
4.转换到Gradle
Let’s go to the root directory of this Maven project, containing the master pom.xml, and execute the gradle init command. When prompted for a response, let’s type in yes and press enter. We should see the following output:
让我们进入该Maven项目的根目录(包含主目录pom.xml),并执行gradle init命令。当提示回复时,让我们输入yes并按回车。我们应该看到以下输出。
$ gradle init
Found a Maven build. Generate a Gradle build from this? (default: yes) [yes, no] yes
> Task :init
Maven to Gradle conversion is an incubating feature.
Get more help with your project: https://docs.gradle.org/6.1/userguide/migrating_from_maven.html
BUILD SUCCESSFUL in 3s
2 actionable tasks: 2 executed
5. Generated Gradle Build Files
5.生成的Gradle构建文件
Now let’s recheck the contents of our project’s root directory. We should now see a number of new files in our root directory. If we want to go deeper, we can have a look at gradle-build-settings-properties.
现在让我们重新检查一下我们项目根目录的内容。现在我们应该在根目录中看到一些新的文件。如果我们想深入了解,我们可以看看gradle-build-settings-properties>。
5.1. build.gradle
5.1.build.gradle
The build.gradle file is the core component of our Gradle build process, and it is the direct equivalent of the pom.xml file for Maven builds. We can see here that pom.xml attributes like groupId, version, and dependencies are translated to their Gradle equivalents. Also present is the attribute of sourceCompatibility, which tells us which Java version to use when compiling Java sources. In the plugins section, we have ‘java‘ which provides support for building any type of Java project, and ‘maven-publish’ which provides support for publishing artifacts to Maven-compatible repositories.
build.gradle文件是Gradle构建过程的核心组件,它直接等同于Maven构建的pom.xml文件。我们可以看到,pom.xml属性如groupId、version和dependencies被翻译成与之对应的Gradle。还有一个属性是sourceCompatibility,它告诉我们在编译Java源时要使用哪个Java版本。在插件部分,我们有’java‘,它为构建任何类型的Java项目提供支持,还有’maven-publish’,它为发布工件到Maven兼容的仓库提供支持。
/*
* This file was generated by the Gradle 'init' task.
*/
plugins {
id 'java'
id 'maven-publish'
}
repositories {
mavenLocal()
maven {
url = uri('https://repo.maven.apache.org/maven2/')
}
}
dependencies {
implementation 'org.apache.commons:commons-lang3:3.12.0'
}
group = 'com.baeldung'
version = '0.0.1-SNAPSHOT'<br />description = 'maven-to-gradle'
java.sourceCompatibility = JavaVersion.VERSION_1_8
publishing {
publications {
maven(MavenPublication) {
from(components.java)
}
}
}
5.2. settings.gradle
5.2. settings.gradle
The settings.gradle file is used by Gradle during the initialization phase to identify which projects are included in the build.
settings.gradle文件被Gradle在初始化阶段用来识别哪些项目被包含在构建中。
/*
* This file was generated by the Gradle 'init' task.
*/
rootProject.name = 'maven-to-gradle'
5.3. gradlew and gradlew.bat
5.3.gradlew和gradlew.bat
Two startup scripts, one for Windows and one for Unix are also generated by Gradle. These scripts can be used to run the project on a machine that does not have a prior Gradle setup. We can learn more about the Gradle wrapper files by taking a look at gradle-wrapper.
Gradle还生成了两个启动脚本,一个用于Windows,一个用于Unix。这些脚本可以用来在没有事先设置过Gradle的机器上运行项目。我们可以通过查看gradle-wrapper来了解更多关于Gradle包装文件的信息。
6. Conclusion
6.结语
In this article, we learned how a Maven build containing pom.xml can be converted to a Gradle build having multiple build files such as build.gradle, settings.gradle, gradlew, and gradlew.bat. The source code from this article can be found over on GitHub.
在这篇文章中,我们学习了如何将包含pom.xml的Maven构建转换为拥有多个构建文件(如build.gradle、settings.gradle、gradlew,和gradlew.bat)的Gradle构建。本文的源代码可以在GitHub上找到过。