Groovy DSL Example
Groovy DSL Example
Gradle is a popular build automation tool used mainly for Java projects. It helps developers compile code, run tests, create packages (like JAR files), and deploy applications. Gradle supports two domain-specific languages (DSLs) for writing build scripts: Groovy DSL and Kotlin DSL. Groovy DSL is the default and uses Groovy language syntax, making the scripts concise and flexible.
What is Gradle Groovy DSL?
Groovy DSL is a way of configuring Gradle using the Groovy programming language. It allows you to write build scripts in a human-readable and expressive manner. You define tasks, dependencies, plugins, and project settings using a clear and declarative syntax.
Simple Groovy DSL Example
plugins {
id 'java'
}
group = 'com.example'
version = '1.0.0'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.apache.commons:commons-lang3:3.12.0'
testImplementation 'junit:junit:4.13.2'
}
tasks.register('hello') {
doLast {
println 'Hello from Gradle!'
}
}
Explanation of the Example
- plugins: This block applies the Java plugin, which provides support for building Java projects.
- group and version: These define the project’s group ID and version number, often used when publishing artifacts.
- repositories: This block tells Gradle where to find dependencies. In this case, it’s using Maven Central.
- dependencies: Here, we declare project dependencies. For example, we use Apache Commons Lang for utilities and JUnit for testing.
- tasks.register(‘hello’): This creates a custom task named
hello
. When run, it prints “Hello from Gradle!” to the console.
Gradle Groovy DSL offers a powerful and readable way to define build configurations using the Groovy language. It’s widely used in Java-based projects and helps automate common development tasks efficiently.