Maven is a classic project builder that many of you have been using for a long time, but if you have tried Gradle, it will smell good. The faster Maven build tool, MVND, performs better than Gradle, but this article gives you an idea of who is the king.

1. MVND profile

MVND is short for Maven Daemon, which translates to Maven Daemon in Chinese. MVND is a subproject of Apache/Maven. It is not a new build tool, but an extension of Maven. Maven is built in by building one or more Maven daemons to perform build services.

2. Introduction of Gradle

Gradle is a new generation of open source automated build tools, with many advantages such as high efficiency and flexibility, widely used in Java development flow.

Advantages of Gradle

  • The syntax experience is better and you can get rid of the tedious configuration of XML.
  • Gradle builds quickly, and it can build quickly by reusing previously executed outputs, processing only changed inputs, and executing tasks in parallel.
  • You can write logic directly in scripts, and experience more flexibility than Maven.
  • Gradle is the official build tool for Android and supports many popular languages and technologies.
  • Maven can be natively compatible with Maven configurations, whereas Maven cannot be natively compatible with Gradle configurations.
  • Gradle versions update quickly.

3. Used Gradle

Using Idea, we can create Gradle projects directly. Take the Spring Boot project as an example, as shown in the following figure:Select the third Gradle Project.

Gradle, like Maven, does not need to be installed. You only need to use the default plugin provided by Idea.

3.1 Replace Gradle as the domestic source

For quick download, you can configure Gradle as a domestic source. First, open the.gradle folder in the user directory and create an init. Gradle file, as shown in the following figure:Add the following configuration:

allprojects {
  repositories {
    maven {
      url 'https://maven.aliyun.com/repository/public/'
    }
    mavenLocal()
    mavenCentral()
  }
}
Copy the code

The above is to set the download data source as Ali Cloud.

Note: Set the domestic source before creating the project so that you can use the domestic source directly when creating the Spring Boot project to speed up project creation and initialization.

Create a Gradle version of your Spring Boot project. This is similar to Maven.The Gradle project directory is almost the same as the Maven project directory.

3.2 Project dependency file comparison

The Maven project’s dependency configuration file is pom.xml, while the Gradle project’s dependency files are settings. Gradle and build.gradle.

settings.gradle VS build.gradle

Settings. gradle is the general configuration file for gradle projects. This file contains the general configuration of gradle projects. It is similar to maven’s parent POm file, and build.gradle is the specific configuration for a single project. We mainly use the build.gradle file. Build. Gradle configuration is as follows:

Plugins {id 'org.springFrameframe. boot' version '2.5.8' id 'io.spring. Dependency -management' version '1.0.11.RELEASE' id 'Java'} group = 'com.example' version = '0.0.1-SNAPSHOT' sourceCompatibility = '1.8' configurations {compileOnly { extendsFrom annotationProcessor } } repositories { mavenCentral() } dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' compileOnly 'org.projectlombok:lombok' developmentOnly 'org.springframework.boot:spring-boot-devtools' annotationProcessor 'org.projectlombok:lombok' testImplementation 'org.springframework.boot:spring-boot-starter-test' } test { useJUnitPlatform() }Copy the code

As you can see from the above configuration files, build.gradle is much more compact than the content in pom.xml, and their pairs are shown below:

3.3 Packaging Projects

Gradle and Maven package projects are similar. You can click on the right side of your Idea to create a Maven package project.Gradle is packaged as follows:

4. MVND use

MVND use is required to install, the previous article has been introduced, here will not be repeated, click on the details: juejin.cn/post/704618…

5. Performance comparison

Next, create two new Spring Boot 2.5.8 + Java 8 to test the performance of the package.

5.1 Maven packaging performance

First time Maven was packaged: 6.524 seconds.

5.2 MVND packaging performance

The first MVND package took 4.832 seconds, a 135% improvement over Maven.

5.3 Gradle packaging performance

Gradle took 1.560 seconds to package for the first time, a 418% improvement over Maven and 300% improvement over MVND.

Extension: Directory for storing Gradle package files

Gradle files are stored under “project root path \build\libs”, as shown below:

conclusion

Although MVND aims to use Gradle technology to provide faster Maven builds, Gradle package still delivers the highest performance in our tests, with 418% performance improvements over Maven and 300% performance improvements over MVND. Gradle configuration files are simpler and easier to read, and are still the most recommended tool for building projects.

Judge right and wrong from yourself, praise to listen to others, gain and loss in the number.

Public number: Java interview analysis