java-gradle

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Java Gradle Skill

Java Gradle 技能

Master Gradle build tool with Kotlin DSL for Java projects.
掌握适用于Java项目的Gradle构建工具与Kotlin DSL。

Overview

概述

This skill covers Gradle configuration with Kotlin DSL including task configuration, dependency management with catalogs, build cache optimization, and CI/CD integration.
本技能涵盖使用Kotlin DSL进行Gradle配置,包括任务配置、依赖目录管理、构建缓存优化以及CI/CD集成。

When to Use This Skill

适用场景

Use when you need to:
  • Configure Gradle builds (Kotlin DSL)
  • Manage dependencies with catalogs
  • Optimize build performance
  • Set up build cache
  • Create custom tasks
当你需要以下操作时使用本技能:
  • 配置Gradle构建(Kotlin DSL)
  • 通过目录管理依赖
  • 优化构建性能
  • 设置构建缓存
  • 创建自定义任务

Quick Reference

快速参考

kotlin
// build.gradle.kts
plugins {
    java
    id("org.springframework.boot") version "3.2.1"
    id("io.spring.dependency-management") version "1.1.4"
}

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(21)
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-web")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
}

tasks.withType<JavaCompile> {
    options.compilerArgs.addAll(listOf("-parameters", "-Xlint:all"))
    options.isFork = true
    options.isIncremental = true
}

tasks.test {
    useJUnitPlatform()
    maxParallelForks = Runtime.getRuntime().availableProcessors() / 2
}
kotlin
// build.gradle.kts
plugins {
    java
    id("org.springframework.boot") version "3.2.1"
    id("io.spring.dependency-management") version "1.1.4"
}

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(21)
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-web")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
}

tasks.withType<JavaCompile> {
    options.compilerArgs.addAll(listOf("-parameters", "-Xlint:all"))
    options.isFork = true
    options.isIncremental = true
}

tasks.test {
    useJUnitPlatform()
    maxParallelForks = Runtime.getRuntime().availableProcessors() / 2
}

Version Catalog

版本目录

toml
undefined
toml
undefined

gradle/libs.versions.toml

gradle/libs.versions.toml

[versions] spring-boot = "3.2.1"
[libraries] spring-boot-web = { module = "org.springframework.boot:spring-boot-starter-web", version.ref = "spring-boot" }
[plugins] spring-boot = { id = "org.springframework.boot", version.ref = "spring-boot" }
undefined
[versions] spring-boot = "3.2.1"
[libraries] spring-boot-web = { module = "org.springframework.boot:spring-boot-starter-web", version.ref = "spring-boot" }
[plugins] spring-boot = { id = "org.springframework.boot", version.ref = "spring-boot" }
undefined

Useful Commands

实用命令

bash
gradle dependencies              # View dependencies
gradle dependencyInsight --dependency log4j  # Analyze dep
gradle build --scan              # Build scan
gradle build --build-cache       # Use cache
gradle wrapper --gradle-version 8.5  # Update wrapper
bash
gradle dependencies              # 查看依赖
gradle dependencyInsight --dependency log4j  # 分析依赖
gradle build --scan              # 构建扫描
gradle build --build-cache       # 使用缓存
gradle wrapper --gradle-version 8.5  # 更新包装器

Build Optimization

构建优化

kotlin
// settings.gradle.kts
enableFeaturePreview("STABLE_CONFIGURATION_CACHE")

// Enable parallel and caching
org.gradle.parallel=true
org.gradle.caching=true
kotlin
// settings.gradle.kts
enableFeaturePreview("STABLE_CONFIGURATION_CACHE")

// 启用并行与缓存
org.gradle.parallel=true
org.gradle.caching=true

Troubleshooting

故障排除

ProblemSolution
Slow buildsEnable --build-cache
Version conflictUse platform() or constraints
Cache issuesgradle --refresh-dependencies
问题解决方案
构建缓慢启用--build-cache
版本冲突使用platform()或constraints
缓存问题执行gradle --refresh-dependencies

Usage

使用方式

Skill("java-gradle")
Skill("java-gradle")