头图

Author of this article: Le Baichuan Source: toutiao.com/i6824937779193971207

"Before work, Guide has always used Maven. When others asked me to Amway Gradle, I always dismissed it. I think this thing is definitely not as good as Maven. After all, Maven is used by so many people, right! Later, I really use it in my work. After it, I really feel its usefulness.

What I want to express is: we want to judge whether a thing is good or bad, for example, if your project technical leader changes the build tool from Maven to Gradle, we must first understand the essence of this thing .

I personally feel that Gradle is better than Maven in many aspects! For example, Gradle's project dependency file build.Gradle is clearer and more concise than Maven's pom.xml (Maven is because of the xml pot), Gradel can also use Groovy language, custom execution Logic and so on.

In addition, in Spring Boot 2.3.0.M1, Gradle will be used instead of Maven to build Spring Boot projects for the first time. Migrating to Gradle is mainly to reduce the time it takes to build the project, because it takes too much time to build the project with Maven.

The official Spring Boot instructions are here: https://spring.io/blog/2020/06/08/migrating-spring-boot-s-build-to-gradle

In conclusion, Guide feels that it is necessary for everyone to learn Gradle.

I believe that all students who use Java have used Maven, which is a very classic and easy-to-use project building tool. But if you often use Maven, you may find that Maven has some places that make people uncomfortable:

  1. The configuration file of Maven is in XML format. If your project depends on more packages, the XML file will become very, very long;
  2. The XML file is not very flexible. If you need to add some custom logic during the build process, it will be very troublesome;
  3. It takes a long time to build the project;

If you feel about these shortcomings of Maven and are ready to try other build tools, then you can try Gradle, which is a very good Java build tool that solves some of the pain points of Maven.

gradle:现代高效的java构建工具

Install Gradle

The most traditional installation method is to download the binary package from the Gradle official website, unzip it, and then add the path to the environment variable. If you have no other requirements, you can use this installation method. However, Gradle is a very trendy project, a new version will be released every few months, this method may not keep up with Gradle's update speed.

So I recommend using a package manager to install Gradle. If you are using a Linux system, then there is no need to say more. If you use Windows, I recommend using scoop package manager to install Gradle. It is easy to install, and uses the SHIM directory to manage environment variables. It is also very convenient to configure Gradle in various tools.

Of course, if you don't like to install so many messy things at all, you can also use Gradle. Gradle provides a tool called Gradle wrapper, which can be used without Gradle installed. Well, it is actually a script file. When you run the wrapper script, if the script finds that there is no Gradle on your computer, it will automatically download and install one for you. Now there is even a Maven wrapper, which is also a script file that can automatically install Maven.

I believe that some friends have heard of Gradle before, and then tried to use it. As a result, it was too slow and finally gave up. I also gave up Gradle for a while because of its speed. But now it will be much more convenient to use Gradle. Gradle is officially opened in China, CDN, the download speed is very fast when using Gradle wrapper. It can be said that now is a good time to learn to use Gradle.

Use Gradle wrapper

Here I use IDEA to create and use Gradle projects.

在IDEA中创建Gradle项目

IDEA uses Gradle wrapper to create projects by default, so it can run normally without installing Gradle. At this time, the project structure should be similar to that shown in the figure below, and students who use Maven should be more familiar with it, because it is almost identical to the Maven project structure. The Gradle folder and gradlew files are the Gradle wrapper files, and the .Gradle suffix file is the Gradle configuration file, which corresponds to Maven's pom.xml.

gradle项目结构

One of the advantages of Gradle wrapper is that you can customize the version of Gradle that you download.

If it is a team collaboration, this function is very convenient, simple setting can unify the team's build tool version. Here I set it to the latest Gradle 6.4. The default download and installation is the bin version, which only contains binary. If you use IDEA, it will recommend downloading the all version, including the source code, so that IDEA can analyze the source code and provide more accurate Gradle script support.

Dependency management

Let's take a look at Gradle's dependency management function, which can be regarded as one of the main purposes of using build tools. This is also one of Gradle's advantages over Maven. Compared to Maven's large set of XML configurations, Gradle's dependencies only need one line.

dependencies {
    testImplementation 'junit:junit:4.13'
    implementation 'com.google.code.gson:gson:2.8.6'
}

Here is the package search website of Jetbrains, which is a very good website for finding Maven and Gradle dependency packages. It is very easy to search and use dependencies.

Package search website address: https://package-search.jetbrains.com/

package search网站

Compared with Maven, the granular control that Gradle relies on is more refined. Maven only has four scopes: compile, provided, test, and `runtime·, while Gradle has the following scopes:

  • implementation: The default scope. The scope of implementation will allow dependencies to be included at both compile and run time, but will not be exposed to the compile time of class library users. For example, if our class library includes gson, when other people use our class library, there will be no gson dependency at compile time.
  • api: Similar to implementation, both are dependencies that are visible during compilation and runtime. But the api allows us to expose the dependencies of our class library to the users of our class library.
  • compileOnly and runtimeOnly: These two are as the name suggests, one is only visible at compile time, and the other is only visible at runtime. And runtimeOnly and Maven's provided are relatively close.
  • testImplementation: This dependency is visible at test compile time and runtime, similar to Maven's test scope.
  • testCompileOnly and testRuntimeOnly: Two types are similar to compileOnly and runtimeOnly, but apply to test compile time and runtime.
    Through brief and concise dependency configuration and a variety of roles and options, Gradle can provide us with better dependency management functions than Maven.

Gradle tasks and plugins

Gradle's configuration file is a Groovy script file in which we can customize some build tasks programmatically. Because of the use of programming, this brings us great flexibility and convenience. For example, there is a demand now, to look at the size of the jar file when the jar is packaged. In Gradle, you only need to write a few lines of code in the build script. In Maven, you need to write Maven plug-ins, and the complexity is not at the same level.

Of course, since the development of Maven, there have been a large number of plug-ins, providing a variety of functions to use. But it still can't compare with Gradle in terms of flexibility. And Gradle also has plug-in functions, and it is developing very fast now. There are a large number of very useful plug-ins, such as the gretty plug-in. Gretty was originally a community plug-in, but was later absorbed by the government as an official plug-in. It can run web projects on Tomcat and jetty servers, and is more powerful than Maven's related plug-ins.

Although Gradle can write custom script tasks very flexibly, in general, we don't need to write build scripts, and we can use existing plugins and tasks to complete related functions. In IDEA, you can also easily view how many tasks there are in the current Gradle project. Basic tasks such as build and test are the same between Maven and Gradle.

gretty插件的任务

Configure mirroring

The download speed of the official Maven warehouse is very slow, so generally we have to configure the domestic mirror source. Gradle is fully compatible with Maven in this respect, so you only need to configure the mirror source a bit to use the Maven mirror. If you have used Gradle to build a project, you should be able to see the relevant configuration and cache of Gradle in the .Gradle folder of the user directory.

The Gradle downloaded by the wrapper before is also stored in this folder, the location is wrapper/dists.

gradle:现代高效的java构建工具

Where to save Gradle downloaded by wrapper

The dependent local cache is in the caches\modules-2\files-2.1 folder. The directory structure is similar to Maven's local cache, both package name + version number, but the last level of Gradle's directory structure is different from Maven, which makes them unable to share the local cache.

gradle:现代高效的java构建工具

Closer to home, to configure the download image in Gradle, you need to create a new init.Gradle initialization script directly in the .Gradle folder. The content of the script file is as follows. In this way, when Gradle downloads the mirror, it will use the mirror source configured here to download, and the speed will be much faster. In addition, Gradle wrapper has set up a CDN in China, and now the speed of using Gradle should be very fast.

allprojects {
   repositories {
       Maven {
           url "https://Maven.aliyun.com/repository/public"
       }
       Maven {
           url "https://Maven.aliyun.com/repository/jcenter"
       }
       Maven {
           url "https://Maven.aliyun.com/repository/spring"
       }
       Maven {
           url "https://Maven.aliyun.com/repository/spring-plugin"
       }
       Maven {
           url "https://Maven.aliyun.com/repository/Gradle-plugin"
       }
       Maven {
           url "https://Maven.aliyun.com/repository/google"
       }
       Maven {
           url "https://Maven.aliyun.com/repository/grails-core"
       }
       Maven {
           url "https://Maven.aliyun.com/repository/apache-snapshots"
       }
   }
}

Of course, if you have a proxy, I actually recommend that you directly set up a global proxy for Gradle. Because Gradle scripts are so flexible, some scripts may rely on remote scripts from github or other places. At this time, the download mirror source set above does not work.

Therefore, if possible, it is better to use the global proxy directly. The setting method is very simple. Create a new Gradle.properties file in the .Gradle folder with the following content. The middle few lines are the configuration items for setting up the proxy. Of course, I also suggest you to set the other few lines, and set the file encoding of Gradle runtime to UTF8 to increase cross-platform compatibility.

org.Gradle.jvmargs=-Xmx4g -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
systemProp.http.proxyHost=127.0.0.1
systemProp.http.proxyPort=10800
systemProp.https.proxyHost=127.0.0.1
systemProp.https.proxyPort=10800
systemProp.file.encoding=UTF-8
org.Gradle.warning.mode=all

Why use Gradle?

Seeing this, you should have a basic understanding of Gradle, and you can also use it in your projects. But if you are already very familiar with Maven, you may be reluctant to use Gradle, because it seems unnecessary. But since Gradle has appeared, it means that many people still have certain opinions on Maven. So here I will summarize the advantages of Gradle over Maven.

  1. Speed, Gradle uses build caches, daemons, etc. to improve compilation speed. The result is that Gradle's compilation speed is much faster than Maven, and the average compilation speed is several times faster than Maven, and the larger the project, the more obvious the gap.

Comparison of compilation time between Maven and Gradle for large multi-module projects, from Gradle official website

  1. Flexibility, Gradle is much more flexible than Maven, although sometimes flexibility is not a good thing. But in most cases, being flexible can greatly facilitate us. Maven's rigid XML file approach is very cumbersome to do things. Many Maven projects complete some tasks that require flexibility by executing external scripts. In Gradle, the configuration file is the build script, and the build script is the programming language (Groovy programming language), which is completely self-sufficient and does not require external scripts.
  2. Simplicity, to accomplish the same function, the length of the Gradle script is much shorter than the length of the Maven configuration file. Although many people say that XML is not troublesome to maintain, I feel that maintaining an XML file with hundreds of lines of dependencies is not necessarily easier than a Gradle script.

Maybe because of the reasons I said above, maybe there are other reasons, one thing I have to admit is that Gradle as an emerging tool has been widely used. Projects such as spring have switched from Maven to Gradle. Only Gradle is supported for Android development. So no matter whether you need to switch the project from Maven to Gradle now, but at least learning Gradle is a necessary thing.

is a technical platform focusing on programmer circles. You can gain latest technical trends, latest internal qualification, 161920c8d3ddfc BAT and other big factory experience, quality learning materials, career route, WeChat search counter-front start to pay attention!

code小生
165 声望18 粉丝