After giving up Maven, I used it. . .

Transfer from: Le Baichuan

Link: http://toutiao.com/i6824937779193971207

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 use Maven a lot, you may find that Maven is uncomfortable in some places:

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. XML files are not very flexible. If you need to add some custom logic during the build process, it will be very troublesome;

3. Maven is very stable, but the relative lack of support for the new version of java, even if it is to compile java11, it is necessary to update the built-in Maven plug-in.

If you feel about these shortcomings of Maven and are ready to try other build tools, then you can try gradle, which is a brand new java build tool that solves some of the pain points of Maven.

One, 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 the update speed of gradle.

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 use gradle without gradle installed. Well, it's actually a script file. When you run the wrapper script, if the script finds that there is no gradle in 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 some friends have heard of gradle before, and then tried to use it, but because the speed is too slow, finally gave up. I also gave up gradle for a while because of its speed. But now it is much more convenient to use gradle. Gradle is officially opened in China, CDN, download speed is very fast when using gradle wrapper. It can be said that now is a good time to learn to use gradle.

 

Two, use gradle wrapper

Here I use IDEA to create and use gradle projects.

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. Students who use Maven should be 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.

One of the advantages of gradle wrapper is that you can customize the downloaded version of gradle. If it is a team collaboration, this function is very convenient. Simple settings 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.

 

Three, dependency management

Let's take a look at the dependency management function of gradle, which can be regarded as one of the main purposes of using the build tool. This is also one of the advantages of gradle over maven. Compared to a long list of XML configurations in maven, gradle's dependencies only need one line.

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

Here I recommend the package search website of Jetbrains, which is the best website to find dependency packages for maven and gradle. You can search and use dependencies very easily.

Compared with Maven, the granular control of gradle dependency is more refined. Maven only has four scopes: compile, provided, test, and runtime, while gradle has the following scopes:

1.implementation, the default scope. The scope of implementation will include dependencies at compile and runtime, 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.

2. Api, similar to implementation, is a dependency that is visible at both compilation and runtime. But api allows us to expose the dependencies of our class library to users of our class library.

3. CompileOnly and runtimeOnly, 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.

4. testImplementation, this dependency is visible at test compile time and run time, similar to Maven's test scope.

5. testCompileOnly and testRuntimeOnly, these two are similar to compileOnly and runtimeOnly, but are used for 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.

 

Four, gradle tasks and plugins

The gradle 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. Just write a few lines of code in the build script in gradle. 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 gretty plug-ins. Gretty was originally a community plug-in, and 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, test, etc. Maven and Gradle are connected.

Five, 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 little bit to use Maven mirroring. If you have built a project with gradle, 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.

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.

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 download the mirror source configured here, which 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 the gradle script is 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. Set the file encoding of gradle runtime to UTF8 to increase cross-platform compatibility. For more tool articles, follow the WeChat subscription number.

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


 

6. 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 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 compared to maven.

1. Speed, gradle uses build cache, daemon and other methods to improve compilation speed. The result is that the compilation speed of gradle is much faster than that of maven, and the average compilation speed is several times faster than Maven, and the larger the project, the more obvious the gap.

2. Flexibility, gradle is much more flexible than Maven, although flexibility is not a good thing sometimes. But in most cases, being flexible can greatly facilitate us. Maven's rigid XML file approach is very troublesome. 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 without external scripts.

3. 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 think that maintaining an XML file with a few hundred 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 been 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.

Welfare time

GIFT TIME

The monthly book delivery link is here again

Thank you for your continued company and support

Give you 5 books today

Sponsored by BlogView

No comment or forwarding is required for this delivery

Use the fairest lottery method.

Pay attention to the public account: programmer interview site

Official account backstage reply: send book 

To participate in the lottery


往期推荐

Brother H was interviewed by the little sister of Ali Technology and talked about these things.

After unplugging the oxygen tube of Tencent Weibo, can Sina Weibo escape the midlife crisis?

I have always thought that count(1) is more effective than count(*), which is despised by my colleagues.

This article is provided by "Yiban Editor" technical support

 

Facing Java Issue 329: Which command can monitor various operating status information of the virtual machine?

In-depth concurrency Issue 013: Expanding synchronized-lock optimization

If you like this article,

Please press and hold the QR code to follow Hollis. 

Forwarding to the circle of friends is my greatest support.

Click  to see 

Like is a feeling

Watching is a kind of support

↘↘↘

Guess you like

Origin blog.csdn.net/hollis_chuang/article/details/108675335