🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Step 1: Setting Up Your Project
First, create a simple Gradle project. You can do this by running the following command in your terminal:
gradle init --type java-application
This command will generate a basic Java application project with a build.gradle file.
Step 2: Defining a Custom Task
Open the build.gradle file and add a custom task definition. In Gradle, you can define tasks using the task keyword.
Example 1: Simple Custom Task
Let's create a simple custom task that prints "Hello, Gradle!" to the console.
task helloGradle {
doLast {
println 'Hello, Gradle!'
}
}
Explanation
task helloGradle: Defines a new task namedhelloGradle.doLast { ... }: Specifies an action to be executed when the task runs. In this case, it prints a message to the console.
Running the Custom Task
To run the helloGradle task, use the following command:
gradle helloGradle
Step 3: Creating a Task with Parameters
You can also create tasks that accept parameters. This can be useful for tasks that need to perform operations based on user input or configuration.
Example 2: Task with Parameters
Let's create a custom task that accepts a greeting message and prints it to the console.
task greet {
doLast {
def greeting = project.hasProperty('greeting') ? project.greeting : 'Hello, World!'
println greeting
}
}
Explanation
project.hasProperty('greeting'): Checks if a property namedgreetingis provided.project.greeting: Retrieves the value of thegreetingproperty.? ... : ...: Conditional operator to use a default value if the property is not provided.
Running the Task with Parameters
To run the greet task with a custom greeting, use the following command:
gradle greet -Pgreeting="Hello, Gradle Enthusiasts!"
Step 4: Creating a Complex Task
Sometimes, tasks need to perform more complex operations, such as file manipulation or invoking external commands.
Example 3: File Manipulation Task
Let's create a custom task that reads a text file and prints its contents to the console.
- Create a sample text file in the
src/main/resourcesdirectory namedsample.txtwith some content.
mkdir -p src/main/resources
echo "This is a sample file." > src/main/resources/sample.txt
- Define the custom task in
build.gradle.
task readFile {
doLast {
def file = file('src/main/resources/sample.txt')
if (file.exists()) {
println file.text
} else {
println 'File not found!'
}
}
}
Explanation
file('src/main/resources/sample.txt'): Gets a reference to the file.file.exists(): Checks if the file exists.file.text: Reads the contents of the file.
Running the Complex Task
To run the readFile task, use the following command:
gradle readFile
Step 5: Creating a Task that Depends on Another Task
Gradle allows you to define task dependencies, ensuring that certain tasks run before others.
Example 4: Task Dependency
Let's create two tasks: one that prepares a directory and another that writes a file in that directory.
task prepareDir {
doLast {
mkdir 'build/output'
println 'Directory prepared.'
}
}
task writeFile(dependsOn: prepareDir) {
doLast {
def file = file('build/output/hello.txt')
file.text = 'Hello, Gradle!'
println 'File written.'
}
}
Explanation
dependsOn: prepareDir: Specifies that thewriteFiletask depends on theprepareDirtask.mkdir 'build/output': Creates a directory.file.text = ...: Writes text to the file.
Running the Task with Dependencies
To run the writeFile task, use the following command:
gradle writeFile
This command will automatically run the prepareDir task before executing the writeFile task.
Conclusion
Creating custom tasks in Gradle allows you to extend and tailor the build process to meet the specific needs of your project. By following this tutorial, you should now have a good understanding of how to define, configure, and execute custom tasks in Gradle. Whether you need simple tasks or complex operations with dependencies, Gradle provides the flexibility to handle it all.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment