乐闻世界logo
搜索文章和话题

What is Gradle Wrapper? How to generate and use it?

2月21日 18:10

Gradle Wrapper is an important feature of Gradle that allows projects to be built with a specific version of Gradle without requiring Gradle to be pre-installed on developers' machines. Here's a detailed explanation of Gradle Wrapper:

Gradle Wrapper Introduction

Gradle Wrapper is a script and a set of JAR files used to download and run a specific version of Gradle. It ensures that all developers and CI/CD systems use the same version of Gradle for builds.

Wrapper File Structure

shell
project/ ├── gradle/ │ └── wrapper/ │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── build.gradle

File Description

  • gradlew: Executable script on Unix/Linux/macOS
  • gradlew.bat: Batch script on Windows
  • gradle/wrapper/gradle-wrapper.jar: Wrapper JAR file
  • gradle/wrapper/gradle-wrapper.properties: Wrapper configuration file

Generating Wrapper

Generate Using Gradle Command

bash
# Generate Wrapper (using current Gradle version) gradle wrapper # Specify Gradle version gradle wrapper --gradle-version 8.0 # Use release version gradle wrapper --gradle-distribution-url https://services.gradle.org/distributions/gradle-8.0-bin.zip

Configure in build.gradle

groovy
// build.gradle wrapper { gradleVersion = '8.0' distributionType = Wrapper.DistributionType.BIN distributionPath = wrapperPath archivePath = wrapperPath }

Wrapper Configuration

gradle-wrapper.properties

properties
# Gradle distribution download URL distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip # Distribution base URL (optional) distributionBase=GRADLE_USER_HOME # Distribution path (optional) distributionPath=wrapper/dists # Zip cache base URL (optional) zipStoreBase=GRADLE_USER_HOME # Zip cache path (optional) zipStorePath=wrapper/dists

Distribution Types

Gradle Wrapper provides three distribution types:

  1. bin: Contains only binary files, small size, fast download

    properties
    distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip
  2. all: Contains binary files and source code, larger size, suitable for viewing source code

    properties
    distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-all.zip
  3. only: Contains only source code (rarely used)

    properties
    distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-src.zip

Using Wrapper

Basic Usage

bash
# Execute Gradle tasks using Wrapper ./gradlew build # On Windows gradlew.bat build # View available tasks ./gradlew tasks # Clean project ./gradlew clean

Specify JVM Options

bash
# Set JVM memory ./gradlew build -Dorg.gradle.jvmargs="-Xmx2048m -XX:MaxMetaspaceSize=512m" # Use specific Java version JAVA_HOME=/path/to/java17 ./gradlew build

Pass Project Properties

bash
# Pass project properties ./gradlew build -Pprofile=production # Pass system properties ./gradlew build -Dspring.profiles.active=production

Customizing Wrapper

Custom Distribution Location

groovy
// build.gradle wrapper { gradleVersion = '8.0' distributionType = Wrapper.DistributionType.BIN // Use custom repository distributionUrl = 'https://my-company.com/gradle/gradle-8.0-bin.zip' }

Use Local Gradle Distribution

groovy
// gradle-wrapper.properties distributionUrl=file\:///path/to/local/gradle-8.0-bin.zip

Configure Network Proxy

bash
# Set proxy ./gradlew build -Dhttp.proxyHost=proxy.example.com -Dhttp.proxyPort=8080 # Configure in gradle.properties # gradle.properties systemProp.http.proxyHost=proxy.example.com systemProp.http.proxyPort=8080 systemProp.https.proxyHost=proxy.example.com systemProp.https.proxyPort=8080

Version Management

Update Wrapper Version

bash
# Update to latest version ./gradlew wrapper --gradle-version=8.1 # Update to specific version ./gradlew wrapper --gradle-version=8.0.2 # Update to release candidate version ./gradlew wrapper --gradle-version=8.2-rc-1

Check Current Version

bash
# View Gradle version used by Wrapper ./gradlew --version # View Wrapper configuration cat gradle/wrapper/gradle-wrapper.properties

Best Practices

1. Commit Wrapper Files to Version Control

bash
# Commit these files to Git git add gradlew gradlew.bat git add gradle/wrapper/gradle-wrapper.jar git add gradle/wrapper/gradle-wrapper.properties # Don't commit downloaded Gradle distributions echo "gradle/wrapper/dists/" >> .gitignore

2. Use Fixed Version

groovy
// build.gradle wrapper { gradleVersion = '8.0' // Use fixed version distributionType = Wrapper.DistributionType.BIN }

3. Use Wrapper in CI/CD

yaml
# GitHub Actions example - name: Build with Gradle run: ./gradlew build # Jenkins Pipeline example sh './gradlew build' # GitLab CI example script: - ./gradlew build

4. Optimize Download Speed

groovy
// Use domestic mirror wrapper { gradleVersion = '8.0' distributionUrl = 'https://mirrors.cloud.tencent.com/gradle/gradle-8.0-bin.zip' }

Troubleshooting

1. Wrapper Script Has No Execute Permission

bash
# Add execute permission chmod +x gradlew

2. Download Failed

bash
# Manually download Gradle distribution wget https://services.gradle.org/distributions/gradle-8.0-bin.zip # Place in correct directory mkdir -p ~/.gradle/wrapper/dists/gradle-8.0-bin cp gradle-8.0-bin.zip ~/.gradle/wrapper/dists/gradle-8.0-bin/

3. Version Conflict

bash
# Clear local cache rm -rf ~/.gradle/wrapper/dists/ # Re-download ./gradlew build

4. Network Issues

bash
# Use offline mode ./gradlew build --offline # Configure proxy ./gradlew build -Dhttp.proxyHost=proxy.example.com -Dhttp.proxyPort=8080

Security Considerations

1. Verify Wrapper JAR

bash
# Verify Wrapper JAR checksum shasum gradle/wrapper/gradle-wrapper.jar

2. Use HTTPS

properties
# gradle-wrapper.properties distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip

3. Restrict Network Access

groovy
// In enterprise environments, restrict Wrapper to only download from internal repositories wrapper { distributionUrl = 'https://internal-repo.example.com/gradle/gradle-8.0-bin.zip' }

Advanced Usage

1. Wrapper in Multi-Project

bash
# Generate Wrapper in root project ./gradlew wrapper # All subprojects can use the same Wrapper ./gradlew :module1:build ./gradlew :module2:build

2. Custom Wrapper Script

bash
# Modify gradlew script to add custom logic # For example: set environment variables, check Java version, etc.

3. Use Gradle Version Catalog

groovy
// gradle/libs.versions.toml [versions] gradle = "8.0" // Use in build.gradle wrapper { gradleVersion = libs.versions.gradle.get() }
标签:Gradle