0Pricing
Groovy & Gradle: JVM Automation and Build Engineering · درس

إضافات الاصطلاح ومنطق البناء

طوّر إضافات الاصطلاح وطبّقها لفرض إعدادات ومعايير بناء متسقة في مؤسستكم

إضافات الاصطلاح ومنطق البناء درس مجاني في Groovy & Gradle: JVM Automation and Build Engineering على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Groovy & Gradle: JVM Automation and Build Engineering، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Groovy & Gradle: JVM Automation and Build Engineering 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

What are Convention Plugins?

In large Gradle multi-project builds, maintaining consistent configurations across many subprojects can become a challenge. This is where Convention Plugins come in handy.

A convention plugin is essentially a way to package and reuse common build logic and configurations specific to your organization or project. They help enforce standards and reduce boilerplate.

The Problem: Inconsistent Build Logic

Imagine you have several Java subprojects. Each needs to apply the java-library plugin, set a specific Java toolchain, and configure common repositories. Without convention plugins, you might end up with repetitive and slightly different configurations in each build.gradle file.

/* project-a/build.gradle */
plugins {
    id 'java-library'
}
java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(17)
    }
}
repositories {
    mavenCentral()
}

/* project-b/build.gradle */
plugins {
    id 'java-library'
}
java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(17)
    }
}
repositories {
    mavenCentral()
}

Centralizing Build Logic with `buildSrc`

To solve the problem of repetition, Gradle offers a special directory: buildSrc. When present in your project root, Gradle automatically compiles any code (Groovy, Kotlin, Java) found within buildSrc and adds it to the classpath of your build scripts.

This makes buildSrc the perfect place to define internal build logic, custom tasks, and especially, convention plugins.

Crafting Your First Convention Plugin

Let's create a simple convention plugin to standardize our Java library setup. We'll define it in buildSrc/src/main/groovy/my.java-conventions.groovy. This plugin will apply the java-library plugin, set Java 17 as the toolchain, and add mavenCentral().

// buildSrc/src/main/groovy/my.java-conventions.groovy
plugins {
    id 'java-library'
}

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

repositories {
    mavenCentral()
}

Applying Your Convention Plugin

Once you've defined your convention plugin in buildSrc, you can apply it to any subproject's build.gradle file using its ID. Notice how much cleaner and more concise the subproject's build file becomes!

// subproject-a/build.gradle
plugins {
    id 'my.java-conventions'
}

// All Java library, Java 17, and Maven Central settings
// are now applied automatically by the convention plugin!

Expanding Conventions: Dependencies & Tests

Convention plugins can do much more than just basic settings. You can enforce common dependencies, configure testing frameworks, or standardize other build aspects. Here, we add JUnit Jupiter dependencies and a common test logging configuration.

// buildSrc/src/main/groovy/my.java-conventions.groovy (updated)
plugins {
    id 'java-library'
}
// ... (previous Java toolchain and repositories config)

dependencies {
    testImplementation platform('org.junit:junit-bom:5.10.0')
    testImplementation 'org.junit.jupiter:junit-jupiter'
}

test {
    useJUnitPlatform()
    testLogging {
        events "passed", "skipped", "failed"
    }
}

Convention Plugins with Kotlin DSL

Just like regular build scripts, convention plugins can also be written using the Kotlin DSL (.gradle.kts files). This provides compile-time safety and better IDE support, which can be very valuable for complex build logic.

The structure remains similar, just with Kotlin syntax.

// buildSrc/src/main/kotlin/my.kotlin-conventions.gradle.kts
plugins {
    java
}

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

repositories {
    mavenCentral()
}

Applying to the Root Project

While convention plugins are often used for subprojects, they can also be applied to the root project's build.gradle. This is useful for defining build-wide properties or configurations that should apply to the entire project, like common dependency versions.

// build.gradle (root project)
plugins {
    id 'my.root-conventions'
}

// buildSrc/src/main/groovy/my.root-conventions.groovy
ext {
    // Define properties accessible throughout the build
    springBootVersion = '3.2.0'
    kotlinVersion = '1.9.0'
}

Benefits and Best Practices

Convention plugins are a cornerstone of maintainable, large-scale Gradle builds:

  • Consistency: Ensures all projects adhere to organizational standards.
  • Reduced Duplication: Centralizes common build logic, eliminating copy-pasting.
  • Easier Maintenance: Update standards in one place; all projects instantly benefit.
  • Improved Readability: Subproject build files become shorter and more focused on project-specific details.

When creating them, keep plugins focused on a single concern, use clear IDs, and leverage Kotlin DSL for type safety.

Convention Checkpoint

Convention plugins are a powerful way to standardize build logic and enforce organizational standards. Let's test your understanding.

Recap: Mastering Convention Plugins

You've now learned about Gradle Convention Plugins! They are an essential tool for managing complex, multi-project builds.

  • Convention plugins centralize common build logic.
  • They are typically defined in the buildSrc directory.
  • They help enforce consistent configurations (Java versions, dependencies, test setups).
  • They drastically reduce boilerplate and improve maintainability.

By leveraging convention plugins, you can build more robust and scalable Gradle projects.

الأسئلة الشائعة

هل درس «إضافات الاصطلاح ومنطق البناء» مجاني؟

نعم — نص درس «إضافات الاصطلاح ومنطق البناء» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Groovy & Gradle: JVM Automation and Build Engineering، انتقل إلى CoddyKit PRO. تتضمن دورة Groovy & Gradle: JVM Automation and Build Engineering 4 دروس في المجموع.

ماذا ستتعلم في «إضافات الاصطلاح ومنطق البناء»؟

طوّر إضافات الاصطلاح وطبّقها لفرض إعدادات ومعايير بناء متسقة في مؤسستكم تتمرن على Groovy & Gradle: JVM Automation and Build Engineering مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Groovy & Gradle: JVM Automation and Build Engineering؟

لا تُشترط خبرة سابقة. Groovy & Gradle: JVM Automation and Build Engineering على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.

كم من الوقت يستغرق درس «إضافات الاصطلاح ومنطق البناء»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Groovy & Gradle: JVM Automation and Build Engineering هذا؟

نعم. كل درس في Groovy & Gradle: JVM Automation and Build Engineering يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. عمليات فحص Gradle Insights وBuild Scans
  2. الأمان وإدارة بيانات الاعتماد
  3. إضافات الاصطلاح ومنطق البناء
  4. كتالوجات إصدارات التبعيات والمنصات
← العودة إلى Groovy & Gradle: JVM Automation and Build Engineering