In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article, the editor introduces in detail the "Java build tool gradle how to install and use", the content is detailed, the steps are clear, and the details are handled properly. I hope that this "Java build tool gradle how to install and use" article can help you solve your doubts, following the editor's ideas slowly in depth, together to learn new knowledge.
For those of you who are studying Java, I believe you have all used Maven, a classic and practical project building tool. But if you use Maven a lot, you may find some uncomfortable things about Maven:
First, the configuration file of Maven is in XML format. If your project depends on more packages, then the XML file will become very, very long.
Second, the XML file is not very flexible. If you need to add some custom logic in the construction process, it will be very troublesome.
The third is that Maven is very stable, but relatively insufficient support for the new version of java, even in order to compile java11, also need to update the built-in Maven plug-in.
If you are aware of these shortcomings of Maven and are ready to try other build tools, you can try gradle, a brand new java build tool that solves some of the pain points of Maven.
Install gradle
The most traditional installation method is to go to the gradle website to download the binary package, extract it, and then add the path to the environment variable. If you don't have any other needs, you can use this installation method. However, gradle is a very up-to-date project, and a new version is released every few months, which may not keep up with the pace of gradle updates.
So I highly recommend using the package manager to install gradle. If you use the linux system, then needless to say. If you are using a Windows system, I recommend using the scoop package manager to install gradle. It is easy to install, and uses the SHIM directory to manage environment variables, and it is also convenient to configure gradle in various tools.
Of course, if you don't like installing so much clutter at all, you can also use gradle. Gradle provides a tool called gradle wrapper that allows you to use gradle without installing gradle. Well, it's just 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's even Maven wrapper, which is also a script file that automatically installs Maven.
I believe that some friends have heard of gradle before, and then try to use it, but finally give up because the speed is too slow. I also gave up gradle for a while because of its speed. But it's much more convenient to use gradle now. Gradle officially opened CDN in China, and 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 the IDEA to create and use gradle projects.
IDEA uses gradle wrapper to create projects by default, so it works without installing gradle. At this point, the project structure should be similar to that shown in the following figure, and students who use Maven should be familiar with it, because it is almost exactly the same as the project structure of Maven. The files in the gradle folder and gradlew are gradle wrapper files, and the file with the .gradle suffix name is the configuration file for gradle, which corresponds to Maven's pom.xml.
One of the advantages of gradle wrapper is that you can customize the version of gradle you download, which is very convenient if you work in a team, and you can unify the build tool version of the team with a simple setup. Here I will set it to the latest gradle 6. 4. The default download installs the bin version, which contains only binaries. If you use IDEA, it recommends downloading the all version, including the source code, so that IDEA can analyze the source code and provide more accurate gradle scripting support.
Dependency management
Let's take a look at the dependency management capabilities of gradle, which is one of the main purposes of using the build tool. This is also one of the advantages of gradle over maven. Compared to maven's long list of XML configurations, gradle's dependencies require only one line.
Dependencies {testImplementation 'junit:junit:4.13' implementation' com.google.code.gson:gson:2.8.6'}
Here is a recommendation for Jetbrains's package search website, which is the best site to find maven and gradle dependency packages. It is very easy to search and use dependencies.
The granularity control that gradle depends on is also finer than that of Maven. There are only four kinds of scope for maven: compile, provided, test, and runtime, while gradle has the following scope:
Implementation, the default scope. The scope of implementation allows dependencies to be included at both compile and run time, but is not exposed to the compile time of class library consumers. For example, if our class library contains gson, then when others use our class library, there will be no gson dependencies at compile time.
Api, like implementation, is a dependency that is visible both at compile and run time. But api allows us to expose the dependencies of our class libraries to our library users.
CompileOnly and runtimeOnly, as the name implies, are visible only at compile time and only at run time. The provided of runtimeOnly is similar to that of Maven.
TestImplementation, which is visible at test compile time and run time, is similar to Maven's test scope.
TestCompileOnly and testRuntimeOnly, which are similar to compileOnly and runtimeOnly, but work at test compile time and run time.
Through a short and concise dependency configuration and a variety of roles and options, Gradle can provide us with better dependency management capabilities than Maven.
Tasks and plug-ins for gradle
The configuration file for gradle is an groovy script file where we can programmatically customize some build tasks. Because of the use of programming, this gives us great flexibility and convenience. For example, there is a need to look at the size of the jar file when packaging out the jar. 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 all the same level.
Of course, with the development of Maven, there are a large number of plug-ins that provide a variety of functions to use. But it still can't compare with Gradle in terms of flexibility. And Gradle also has plug-in functionality, and now it is developing rapidly, and there are a large number of very useful plug-ins, such as the gretty plug-in. Gretty, which used to be a community plug-in, was later officially absorbed as an official plug-in that runs web projects on Tomcat and jetty servers, which is more powerful than Maven's related plug-ins.
Although gradle can be very flexible to write custom scripting tasks, in general, we do not need to write build scripts, using existing plug-ins and tasks to complete the relevant functions. In IDEA, you can also easily see how many tasks there are in the current gradle project. Basic tasks such as build, test and other Maven and Gradle are the same.
Configure Mirror
The download speed of the official Maven warehouse is very slow, so generally we have to configure domestic mirror sources. Gradle is fully compatible with Maven in this respect, so you only need to configure the mirror source a little to use Maven's mirror. If you have built a project with gradle, you should be able to see the configuration and cache of gradle in the .gradle folder of the user's directory.
The gradle previously downloaded by wrapper is also stored in this folder at wrapper/dists.
The dependent local cache is stored in the caches\ modules-2\ files-2.1 folder. The directory structure is similar to Maven's local cache, both in the form of package name and version number, but the last layer of the directory structure of gradle is different from Maven, which makes it impossible for them to share the local cache.
To get to the point, configuring a download image in gradle requires creating a new init.gradle initialization script directly in the .gradle folder, as shown in the script file. In this way, gradle will use the image source configured here to download the image, which will be much faster. Coupled with the fact that gradle wrapper has set up CDN in China, 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, in fact, I 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 elsewhere. At this time, the download mirror source set above will not work.
Therefore, it is better to use the global agent directly if there are conditions. It is easy to set up by creating a new gradle.properties file in the .gradle folder as follows. The middle lines are the configuration items for setting up the agent. Of course, I suggest you set the other lines to set the file encoding of the gradle runtime to UTF8 to increase cross-platform compatibility.
Why does org.gradle.jvmargs=-Xmx4g-XX:MaxPermSize=512m-XX:+HeapDumpOnOutOfMemoryError-Dfile.encoding=UTF-8systemProp.http.proxyHost=127.0.0.1systemProp.http.proxyPort=10800systemProp.https.proxyHost=127.0.0.1systemProp.https.proxyPort=10800systemProp.file.encoding=UTF-8org.gradle.warning.mode=all use gradle?
See here, you should have a basic understanding of gradle, you can also use it in your project. But if you are already familiar with Maven, you may be reluctant to use gradle because it seems unnecessary. But now that gradle has appeared, it shows that many people still have some opinions about Maven. So here I would like to summarize the advantages of gradle over maven.
1. Speed
Gradle uses build caching, daemons, and so on to improve compilation speed. The result is that gradle compiles much faster than maven, on average, several times faster than Maven, and the larger the project, the more obvious the gap.
two。 Flexibility
Gradle is much more flexible than Maven, although sometimes flexibility is not a good thing. But in most cases, being flexible can be of great convenience to us. Maven's rigid XML file way to do things is very troublesome. Many Maven projects do work that requires 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. Conciseness
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 easy to maintain, I don't think maintaining an XML file with hundreds of lines of dependencies alone is necessarily easier than gradle scripts.
Maybe because of the reasons I said above, maybe there are other reasons, one thing I have to admit is that gradle has been widely used as an emerging tool. Projects such as spring have been switched from Maven to gradle. Developing Android programs only supports gradle. So whether or not you need to switch your project from maven to gradle now, at least learning gradle is a must.
After reading this, the article "how to install and use Java build tool gradle" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.