For those of you who use Java, Maven is a classic project builder. But if you’re a regular user of Maven, you might find some things about Maven uncomfortable:
1\. Maven configuration files are in XML format. If your project depends on many packages, the XML files can become very, very long;
The 2\.xml file is not very flexible and can be cumbersome if you need to add some custom logic during the build process;
3\. Maven is very stable, but there is relatively little support for new Versions of Java, even in order to compile Java 11, you need to update the built-in Maven plug-in.
If you’re feeling the same way about Maven’s shortcomings and ready to try other build tools, you can try Gradle, a new Java build tool that addresses some of Maven’s pain points.
Install Gradle
The most traditional way to install gradle is to go to the gradle website, download the binary package, unpack it, and add the path to the environment variable. If you have no other requirements, you can use this installation method. However, Gradle is a very modern project and releases a new version every few months, which may not keep up with the pace of gradle updates.
So I recommend using the package manager to install Gradle. If you use Linux, say no more. If you are using Windows, I recommend using the Scoop package manager to install Gradle. It is easy to install, use the SHIM directory to manage environment variables, and it is easy to configure Gradle in various tools.
Of course, you can also use Gradle if you don’t like installing so many messy things at all. Gradle provides a tool called Gradle Wrapper that can be used without gradle installed. Well, it’s just a script file. When you run the Wrapper script, if the script finds that gradle is not on your computer, it will automatically download and install one for you. There is even a Maven Wrapper, which is also a script file that automatically installs Maven.
I believe some of my friends heard about Gradle and tried to use it, only to give up because it was too slow. I gave up gradle for a while because of its speed. However, it is much easier to use Gradle now. Gradle has officially opened a CDN in China, and the download speed is very fast when using Gradle Wrapper. Now is a good time to learn how to use Gradle.
Use gradle Wrapper
Here I use IDEA to create and use Gradle projects.
IDEA creates projects using gradle Wrapper by default, so it works without installing Gradle. The project structure should be similar to the one shown below, which will be familiar to those using Maven, as it is almost identical to Maven’s project structure. The gradle folder and gradlew files are gradle Wrapper files, and the. Gradle suffix files are gradle configuration files corresponding to Maven’s POM.xml.
Gradle Wrapper one of the advantages of gradle Wrapper is that you can customize the gradle version that you download. This is very handy if you are working with a team, and a simple setup can unify the team’s build tool versions. Gradle 6.4 is the latest gradle 6.4. By default, the bin version is downloaded and installed, which contains only binary files. If you use IDEA, it recommends downloading the All version, which contains the source code, so 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, which is one of the main reasons we use build tools. This is one of gradle’s advantages over Maven. Compared to Maven’s long string of XML configurations, Gradle’s dependencies are one line long.
Dependencies {testImplementation 'junit: junit: 4.13' implementation 'com. Google. Code. Gson: gson: 2.8.6'}Copy the code
Jetbrains’ Package Search site is the best site for finding Maven and Gradle dependencies, making it very easy to search for and use dependencies.
Gradle relies on finer granularity control than Maven. Maven has only compile, Provided, Test, and Runtime scopes. Gradle has the following scopes:
1. Implementation, default scope. The scope of implementation includes dependencies at compile and run time, but not at compile time for the user of the library. For example, if our library includes Gson, then other people using our library will compile without gson dependencies.
API, like Implementation, is a dependency that is visible at compile and run time. But apis allow us to expose the dependencies of our libraries to the users of our libraries.
3.compileOnly and runtimeOnly, as their names suggest, are only visible at compile time and only at runtime. RuntimeOnly is close to Maven’s Provided.
4. TestImplementation, which is visible at test compile time and run time, similar to Maven’s test scope.
5. TestCompileOnly and testRuntimeOnly, which are similar to compileOnly and runtimeOnly but at compile time and runtime.
With a concise dependency configuration and a wide variety of functions and options, Gradle can provide much better dependency management than Maven.
Gradle tasks and plugins
The Gradle configuration file is a Groovy script file in which you can programmatically customize some of your build tasks. This gives us great flexibility and convenience because of the programming approach. For example, there is a requirement to look at the size of the JAR file while packing out the JAR. Gradle requires only a few lines of code in a build script. In Maven, you need to write Maven plug-ins, which are completely different levels of complexity.
Of course, at this point in Maven’s history, there are a number of plugins that provide a wide variety of functions to use. However, it is still not as flexible as Gradle. Gradle also has plugins, which are growing rapidly. There are a number of very useful plugins, such as the Gretty plugin. Gretty was originally a community plugin, but has since been adopted as an official plugin. It can run Web projects on Tomcat and Jetty servers, making it more powerful than any Maven plugin.
Gradle can be very flexible in writing custom script tasks, but in most cases you don’t need to write build scripts and use existing plug-ins and tasks. You can also easily check the number of tasks in your Gradle project in the IDEA. Basic tasks such as build and test are common between Maven and Gradle.
5. Configure the mirror
The download speed of Maven’s official repository is very slow, so we usually need to configure a domestic mirror source. Gradle is fully compatible with Maven in this respect, so you can use Maven’s images with a little configuration of the mirror source. If you have built gradle projects, you should be able to see gradle configuration and caches under the. Gradle folder in the user directory.
Gradle downloaded from wrapper is also stored in this folder, at Wrapper /dists.
The local cache is stored in caches\modules-2\files-2.1. The directory structure is similar to Maven’s local cache, with package name + version number, but gradle’s last layer of directory structure is different from Maven’s, which prevents them from sharing the local cache.
Gradle init file. Gradle init file. Gradle init file. Gradle init file. As a result, Gradle downloads images using the image source configured here, which is much faster. With gradle Wrapper setting up the CDN in China, you should be able to use Gradle quickly.
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" } }}Copy the code
Of course, if you have agents, I actually recommend that you set up global agents for Gradle directly. Because Gradle scripts are so flexible, some scripts may rely on remote scripts from Github or elsewhere. In this case, the download image source set above will not work.
So it is better to use the global proxy directly when possible. To set it up, simply create a new gradle.properties file in the. Gradle folder with the following contents. The middle lines are the configuration items that set up the proxy. Gradle runtime file encoding is set to UTF8 for cross-platform compatibility. Gradle runtime file encoding is set to UTF8.
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. The encoding = utf-8 org. Gradle. Warning. Mode = allCopy the code
Why use Gradle?
This should give you a basic understanding of Gradle and allow you to use it in your projects. However, if you are already familiar with Maven, you may be reluctant to use Gradle because it seems unnecessary. But since Gradle is out there, there are a lot of people who still have a problem with Maven. So let me summarize gradle’s advantages over Maven.
Gradle uses build caches, daemons, and more to speed up compilation. As a result, Gradle compiles much faster than Maven, on average several times faster than Maven, and the larger the project, the more pronounced the difference becomes.
Gradle is much more flexible than Maven, although sometimes being flexible is not a good thing. But in most cases, being flexible can be a huge benefit. Maven’s rigid XML file approach is cumbersome to do things. Many Maven projects execute external scripts to perform tasks that require flexibility. Gradle configuration files are build scripts, and build scripts are programming languages (groovy programming language) that are self-contained without external scripts.
**3\. Simplicity, ** Gradle scripts are much shorter than maven configuration files for the same function. Many people say that XML is easy to maintain, but I don’t think that maintaining an XML file with hundreds of lines of dependencies is any easier than gradle scripts.
Perhaps for the reasons I mentioned above, and perhaps for other reasons, one thing to admit is that Gradle as a new tool has been widely used. Projects such as Spring have switched from Maven to Gradle. Developing Android apps only supports Gradle. So whether you need to switch your project from Maven to Gradle now or not, at least learning Gradle is a must.