Why Switch from Maven to Gradle: Installation, Wrapper, Dependency Management, and Advantages
This article explains the drawbacks of Maven, introduces Gradle as a modern Java build tool, and provides step‑by‑step guidance on installing Gradle, using the Gradle wrapper, managing dependencies, configuring mirrors and proxies, and highlights Gradle’s speed, flexibility, and concise DSL compared to Maven.
Many Java developers are familiar with Maven, but they often encounter issues such as overly long XML configuration files, inflexible custom logic, and limited support for newer Java versions; the article suggests trying Gradle to address these pain points.
# Switch to Gradle
Gradle has become widely adopted, with projects like Spring migrating from Maven, and Android development now requiring Gradle, making it essential to learn this newer build tool.
# Install Gradle
The traditional method is to download the binary package from the Gradle website, extract it, and add it to the system PATH, but because Gradle releases new versions frequently, using a package manager (e.g., Scoop on Windows) is recommended for easier updates.
Gradle also provides a wrapper that can download and install the required Gradle version automatically, eliminating the need for a global installation.
# Use Gradle Wrapper
When creating a Gradle project in IntelliJ IDEA, the IDE automatically uses the Gradle wrapper, so no separate Gradle installation is required. The project structure mirrors Maven’s layout, with a gradle folder and gradlew scripts, while .gradle files replace Maven’s pom.xml .
# Dependency Management
Gradle simplifies dependency declarations compared to Maven’s verbose XML. Example:
dependencies { testImplementation 'junit:junit:4.13' implementation 'com.google.code.gson:gson:2.8.6'}Gradle offers several dependency configurations: implementation (default, not exposed to consumers), api (exposed), compileOnly and runtimeOnly , as well as their test‑specific counterparts testImplementation , testCompileOnly , and testRuntimeOnly .
# Gradle Tasks and Plugins
Gradle build files are Groovy scripts, allowing custom tasks to be written in a few lines, offering far more flexibility than Maven’s XML plugins. Popular plugins such as gretty enable running web projects on Tomcat or Jetty with richer features than comparable Maven plugins.
# Configure Mirrors
To speed up downloads in China, you can create an init.gradle script in the .gradle directory with Maven mirror definitions:
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 } }}If you have a proxy, setting a global proxy in gradle.properties is recommended:
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# Why Use Gradle?
Gradle outperforms Maven in build speed thanks to caching and the Gradle daemon, offers greater flexibility through Groovy‑based scripts, and provides a concise DSL that reduces configuration length, making it a compelling alternative for modern Java and Android projects.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
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.