Switch to Gradle: Installation, Wrapper, Dependencies, and Performance Tips
This article explains why Gradle is a superior Java build tool, covering installation methods, the Gradle wrapper, concise dependency management, task and plugin flexibility, mirror and proxy configuration, and the performance advantages over Maven.
1. Installing Gradle
The traditional method is to download the binary package from the Gradle website, unzip it, and add the directory to the PATH. Because new versions are released frequently, using a package manager is recommended: Linux users can rely on their distro’s package manager, while Windows users can use the Scoop manager, which also handles environment variables automatically. Gradle also provides a wrapper that can download the appropriate version on demand.
2. Using the Gradle Wrapper
IDEA creates projects with the Gradle wrapper, so a local Gradle installation is not required. The project structure mirrors Maven’s: a gradle folder and gradlew scripts replace the Maven pom.xml file.
The wrapper lets you specify the Gradle version per project, ensuring all team members use the same version. IDEA often suggests downloading the “all” distribution, which includes source code for better IDE support.
3. Dependency Management
Gradle uses a concise DSL instead of Maven’s verbose XML. Example:
dependencies {
testImplementation 'junit:junit:4.13'
implementation 'com.google.code.gson:gson:2.8.6'
}JetBrains Package Search is a handy site for finding Maven and Gradle artifacts.
Gradle offers several dependency configurations, each with distinct visibility rules:
implementation : default scope, available at compile and runtime but not exposed to consumers.
api : like implementation but also exposed to consumers.
compileOnly and runtimeOnly : visible only at compile time or runtime respectively.
testImplementation : visible during test compile and runtime.
testCompileOnly and testRuntimeOnly : analogous to the non‑test variants for test code.
These scopes provide finer‑grained control than Maven’s four scopes.
4. Tasks and Plugins
Gradle build files are Groovy scripts, allowing you to define custom tasks directly in code, which offers far more flexibility than Maven’s XML plugins. For example, you can write a few lines to print the size of a JAR during packaging. Gradle also has a rich plugin ecosystem; the gretty plugin (originally community‑maintained, now official) can run web projects on Tomcat or Jetty and is more powerful than comparable Maven plugins.
5. Configuring Mirrors
To speed up artifact downloads, you can configure Maven‑compatible mirrors in an init.gradle script placed in the .gradle directory. Example script:
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" }
}
}Gradle stores downloaded wrapper distributions in .gradle/wrapper/dists and caches artifacts under .gradle/caches/modules-2/files-2.1, similar to Maven’s local repository but with a different directory layout.
6. Proxy Settings
If a corporate proxy is required, create a gradle.properties file in the .gradle folder with the following content:
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=all7. Why Use Gradle?
Gradle outperforms Maven in build speed thanks to caching and the daemon, offers far greater flexibility because its scripts are written in Groovy, and reduces configuration size, making builds more concise. It is widely adopted in Spring projects and is mandatory for Android development, so learning Gradle is essential for modern Java developers.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
