0Pricing
Groovy & Gradle: JVM Automation and Build Engineering · 강의

바이너리 플러그인 구축

Groovy 또는 Java를 사용해 독립형 바이너리 플러그인을 개발하고 배포를 위해 패키징합니다.

바이너리 플러그인 구축은(는) CoddyKit의 무료 Groovy & Gradle: JVM Automation and Build Engineering 강의입니다. 이것은 4개 중 2번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Groovy & Gradle: JVM Automation and Build Engineering 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Groovy & Gradle: JVM Automation and Build Engineering 강의에는 총 4개의 강의가 포함되어 있습니다.

이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.

Why Binary Plugins?

Welcome! In this lesson, we'll dive into building binary plugins for Gradle. Unlike simpler script plugins, binary plugins are compiled code (Java or Groovy) that offer superior reusability and structure.

  • Reusability: Easily share across many projects.
  • Encapsulation: Keep your build logic clean and organized.
  • Testability: Easier to unit test complex logic.

Setting Up Your Plugin Project

A binary plugin is typically developed in its own Gradle project. For local development, a common approach is to place your plugin source code within the buildSrc directory of your main project.

The standard structure looks like this:

  • rootProject/
  • buildSrc/
  • src/main/groovy/com/coddykit/plugins/
  • MyPlugin.groovy
  • build.gradle
  • build.gradle (for the consumer project)

The Plugin<Project> Interface

Every binary Gradle plugin must implement the org.gradle.api.Plugin interface, specifically Plugin<Project>. This interface defines a single method: apply(Project project).

The Project object passed to apply is the project the plugin is being applied to. This is your entry point to configure that project.

package com.coddykit.plugins

import org.gradle.api.Plugin
import org.gradle.api.Project

class MyFirstPlugin implements Plugin<Project> {
    @Override
    void apply(Project project) {
        // Plugin logic goes here
        // You can add tasks, extensions, etc.
    }
}

Plugin's Core: apply Method

The apply method is where your plugin's magic happens. Inside this method, you can perform various actions to configure the target project, such as:

  • Creating custom tasks.
  • Adding new configurations.
  • Registering extensions for user configuration.
  • Applying other plugins.

This method runs when Gradle applies your plugin to a project.

Adding a Simple Task

Let's create a basic plugin that adds a custom task named helloGradle to any project it's applied to. This task will simply print a greeting message.

We'll put this class in buildSrc/src/main/groovy/com/coddykit/plugins/GreetingPlugin.groovy.

package com.coddykit.plugins

import org.gradle.api.Plugin
import org.gradle.api.Project

class GreetingPlugin implements Plugin<Project> {
    @Override
    void apply(Project project) {
        project.tasks.create('helloGradle') {
            doLast {
                println "Hello from CoddyKit's Greeting Plugin!"
            }
        }
    }
}

Declaring Your Plugin ID

To make your plugin discoverable, you need to declare a unique ID for it in the plugin project's build.gradle file. This maps your plugin class to an ID that consumer projects will use.

This example build.gradle would be placed in buildSrc/build.gradle.

plugins {
    id 'java-gradle-plugin' // Enables Gradle plugin development features
    id 'groovy' // If your plugin is written in Groovy
}

repositories {
    mavenCentral()
}

dependencies {
    implementation gradleApi() // Provides Gradle API classes
    implementation localGroovy() // Provides Groovy classes
}

gradlePlugin {
    plugins {
        greetingPlugin {
            id = 'com.coddykit.greeting'
            implementationClass = 'com.coddykit.plugins.GreetingPlugin'
        }
    }
}

// To build this plugin: gradle build (from buildSrc dir)

Using Your Custom Plugin

Now that our GreetingPlugin is defined and has an ID, we can apply it to a consumer project. If the plugin is in buildSrc, it's automatically available to the root project.

Add this to your root project's build.gradle file:

plugins {
    id 'com.coddykit.greeting' // Apply our custom plugin by ID
}

// To run the task:
// 1. Ensure GreetingPlugin is in buildSrc
// 2. Open terminal in your project's root
// 3. Run: gradle helloGradle

Plugin Extensions for Configuration

For powerful and flexible plugins, you'll want to allow users to configure them. Gradle extensions are objects that plugins add to a project, providing a DSL (Domain Specific Language) for configuration.

You define an extension class with properties, then register it in your plugin's apply method. Users can then configure these properties in their build.gradle.

Plugin with Configuration

Here's an example of a plugin that uses an extension. First, the extension class (CoddyKitExtension.groovy) defines configurable properties. Then, the plugin (ConfigurablePlugin.groovy) creates an instance of this extension and registers it with the project, also creating a task that uses the configured message.

// src/main/groovy/com/coddykit/plugins/CoddyKitExtension.groovy
package com.coddykit.plugins

class CoddyKitExtension {
    String message = "Default CoddyKit message"
}

// src/main/groovy/com/coddykit/plugins/ConfigurablePlugin.groovy
package com.coddykit.plugins

import org.gradle.api.Plugin
import org.gradle.api.Project

class ConfigurablePlugin implements Plugin<Project> {
    @Override
    void apply(Project project) {
        // Create and register the extension
        def extension = project.extensions.create('coddyKit', CoddyKitExtension)

        project.tasks.create('displayConfigMessage') {
            doLast {
                println "CoddyKit Plugin says: ${extension.message}"
            }
        }
    }
}

Binary Plugin Check

Which of the following are key benefits of using a binary Gradle plugin compared to a simple script plugin?

Recap: Building Binary Plugins

Great job! You've learned the fundamentals of building binary Gradle plugins:

  • Binary plugins offer reusability, encapsulation, and testability for your build logic.
  • They implement the Plugin<Project> interface, with core logic in the apply method.
  • You declare a unique plugin ID in your plugin project's build.gradle.
  • Extensions provide a way for users to configure your plugin.

Next, we'll explore how to apply and publish these powerful plugins!

자주 묻는 질문

“바이너리 플러그인 구축” 강의는 무료인가요?

네 — “바이너리 플러그인 구축” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Groovy & Gradle: JVM Automation and Build Engineering 강의 전체를 잠금 해제할 수 있습니다. Groovy & Gradle: JVM Automation and Build Engineering 강의에는 총 4개의 강의가 포함되어 있습니다.

“바이너리 플러그인 구축”에서 뭘 배우나요?

Groovy 또는 Java를 사용해 독립형 바이너리 플러그인을 개발하고 배포를 위해 패키징합니다. 브라우저에서 직접 실행하는 실습 코드로 Groovy & Gradle: JVM Automation and Build Engineering을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Groovy & Gradle: JVM Automation and Build Engineering을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 Groovy & Gradle: JVM Automation and Build Engineering은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 2번째 강의입니다.

“바이너리 플러그인 구축” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 Groovy & Gradle: JVM Automation and Build Engineering 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 Groovy & Gradle: JVM Automation and Build Engineering 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. Gradle 플러그인 이해하기
  2. 바이너리 플러그인 구축
  3. 플러그인 적용과 게시
  4. 플러그인 확장과 구성 가능한 DSL
← Groovy & Gradle: JVM Automation and Build Engineering(으)로 돌아가기