Jun 01, 2021 Article blog
Students who study
Java
believe you've all used
Maven
a classic and useful project builder.
But if you use
Maven
a lot, you may find some uncomfortable things about
Maven
Maven
profile is
XML
format, and if your project relies on more packages,
XML
file becomes very, very long;
XML
files are not very flexible, if you need to add some custom logic during the build process, it can be very cumbersome;
Maven
is very stable, but relatively not enough support for the new version of
java
even if it is just to compile
java11
you need to update the built-in
Maven
plug-in.
If you're also impressed by
Maven
shortcomings and are ready to try other build tools, try
gradle
a brand new
java
build tool that addresses some of
Maven
pain points.
The most traditional installation method is to go to
gradle
website to download the binary package, unzip it, and then add the path to the environment variable. I
f you don't have any other needs, you can use this installation method.
However,
gradle
is a very trendy project that releases a new version every few months, which may not keep pace with the update speed of
gradle
So I'd recommend using a package manager to install
gradle
I
f you use
linux
system, you don't have to say much. I
f you use
Windows
system, I recommend
scoop
package manager to install
gradle
It's easy to install, it uses
SHIM
directories to manage environment variables, and it's easy to configure
gradle
in a variety of tools.
Of course, if you don't like installing so many messes at all, you can also use
gradle
gradle
provides a tool called
gradle wrapper
that you can use
gradle
gradle
W
ell, it's actually a script file, and when you run
wrapper
script, if the script finds that there's no
gradle
on your computer, it automatically downloads and installs one for you.
There's even
Maven wrapper
which is also a script file that automatically installs
Maven
It was previously believed that some friends had
gradle
and tried to use it, only to give up because it was too slow. I
've been giving it up for a while before because of the speed of the
gradle
B
ut it's much easier to use
gradle
now.
gradle
officially opened
CDN
in China and downloads very quickly when using
gradle wrapper
It's a good time to learn to use
gradle
Here I use
IDEA
to create and use
gradle
projects.
IDEA
uses
gradle wrapper
to create projects by default, so it works without installing
gradle
A
t this point, the project structure should be similar to the one shown in the following image, and students who use
Maven
should be familiar because it is almost identical to
Maven
project structure.
gradle
folder and
gradlew
files are
gradle wrapper
files, and the files with
.gradle
suffix name are
gradle
profile, corresponding to
Maven
pom.xml
One of the advantages of
gradle wrapper
is that you can customize the downloaded version of
gradle
which is convenient if you're working as a team, and you can unify the team's build tool version with a simple setup. H
ere I set it to the latest
gradle 6.4
The default download is the
bin
version, which contains only binary.
If you use
IDEA
it recommends downloading
all
version with the source code so
IDEA
can analyze the source code and provide more accurate
gradle
script support.
Here's a look at
gradle
dependency management capabilities, which is one of the main purposes of our use of build tools. T
his is also one of the advantages of
gradle
over
maven
gradle
dependencies require only one line compared to
maven
long list of
XML
configurations.
dependencies {
testImplementation 'junit:junit:4.13'
implementation 'com.google.code.gson:gson:2.8.6'
}
The
package search
site for
Jetbrains
is recommended here, and is the best site to find
maven
and
gradle
packages, making it easy to search for and use dependencies.
(Recommended tutorial: Java tutorial)
gradle
on more granular control than
maven
which has only four
scope
compile
provided
test
and
runtime
and
gradle
has the following types of
scope
Maven
scope
implementation
scope of the effect allows dependencies to be included at compile and runtime, but is not exposed to the compilation time of the class library consumer.
For example, if our class library contains
gson
then when others use our class library, there is no
gson
dependency at compile time.
implementation
is a dependency that is visible at compile and runtime.
But
api
allows us to expose the dependencies of our own class libraries to the consumers of our class libraries.
runtimeOnly
and
Maven
provided
are closer.
Maven
test
scope.
compileOnly
and
runtimeOnly
but act on test compilation and runtime.
With a short, robust dependency configuration and a wide variety of roles and options,
Gradle
can provide us with better dependency management capabilities than
Maven
gradle
profile is a
groovy
script file where we can programmatically customize some of the build tasks. B
ecause of the programming approach, this gives us a lot of flexibility and convenience. F
or example, there is now a requirement to check the size of the
jar
file by the way when you package out the
jar
I
n
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 one level.
Of course,
Maven
has grown to the point where there are a number of plug-ins that offer a wide variety of features to use. B
ut in terms of flexibility, it's still not comparable to
Gradle
A
nd
Gradle
also has plug-in capabilities and is growing rapidly, with a number of very good plug-ins, such as
gretty
gretty
originally a community plug-in, was officially incorporated as an official plug-in that can run
web
projects on
Tomcat
and
jetty
servers, more powerful than
Maven
related plug-ins.
While
gradle
can be very flexible in writing custom scripting tasks, in general we don't need to write build scripts to do the functionality with existing plug-ins and tasks.
In
IDEA
you can also easily see how many tasks there are in your current
gradle
project, with basic tasks such as
build
test
Maven
and
Gradle
all connected.
Maven
official repository is very slow to download, so we generally have to configure the domestic mirror source.
gradle
fully compatible with
Maven
in this regard, so you can use
Maven
mirror with just a little configuration of the mirror source.
If you've built a project with
gradle
you should be able to see the configuration and cache of the gradle under the
.gradle
folder in the user directory.
gradle
(Recommended micro-class: Java micro-class)
gradle
by
wrapper
is also stored under this folder at
wrapper/dists
The dependent local cache is under the
caches\modules-2\files-2.1
folder.
The directory structure is similar to
Maven
local cache, both of which are package names and version numbers, but the last layer of
gradle
directory structure is different from
Maven
which prevents them from sharing the local cache.
To put it bluntly, configuring a download image in
gradle
requires a new
init.gradle
initialization script to be created directly in the .gradle folder, as follows.
.gradle
A
s a result,
gradle
downloads the image using the mirror source configured here, much faster.
Coupled with the
CDN
set up in China
gradle wrapper
using
gradle
should be fast now.
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 set up a global proxy directly for
gradle
B
ecause
gradle
scripts are so flexible, some scripts may rely on remote scripts from
github
or elsewhere.
That's when the download mirror source set up above doesn't work.
So it's better to use the global proxy directly if you have the conditions. T
he setup is simple: create a new
.gradle
file in the
gradle.properties
folder, as follows. T
he middle line is the configuration item that sets up the agent.
Of course, a few other lines I also recommend that you set up the file encoding for the
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
Seeing here, you should have a basic understanding of
gradle
and use it in your project. B
ut if you're already familiar with
Maven
you might be reluctant to use
gradle
because it doesn't seem necessary. B
ut now that
gradle
has appeared, there are a lot of people who still have some opinions about
Maven
So here I'll summarize the
gradle
over
maven
gradle
uses build caches, daemons, and more to speed up compilation.
The result is that
gradle
compiles much faster than
maven
with an average compilation speed several times faster than
Maven
and the larger the project, the more obvious the gap becomes.
gradle
is much more flexible than
Maven
although sometimes flexibility is not a good thing. B
ut for the most part, being flexible can be extremely convenient for us.
Maven
rigid
XML
file approach is cumbersome. M
any
Maven
projects do some work that requires flexibility by executing external scripts.
In
gradle
the configuration file is the build script, which is the programming language (groovy programming language) and is completely self-sufficient without the need for external scripts.
To accomplish the same functionality, the length of the
gradle
script is much shorter than the length of the
maven
profile.
While a lot of people say
XML
is not troublesome to maintain, I don't think it's necessarily easier to maintain an
XML
file that relies on hundreds of lines alone than
gradle
script.
Perhaps for the reasons I said above, and perhaps for other reasons, one thing I have to admit is that
gradle
has been widely used as an emerging tool. P
rojects such as
spring
have switched from
Maven
to
gradle
D
eveloping Android programs also only
gradle
So whether or not you need to switch the project from
maven
to
gradle
now, at least learning
gradle
is a necessity.
(Recommended tutorial: Gradle tutorial)
Here's a look at the benefits of a modern and efficient Java build tool, gradle, and I hope to help you all.
Author: Belle Chuan
Source: www.toutiao.com/i6824937779193971207