🎓 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
Prerequisites
Before you begin, make sure you have the following installed on your system:
- Java Development Kit (JDK) - Gradle requires JDK 8 or higher. You can download the latest version of the JDK from Oracle's website.
- Gradle - Download and install Gradle from the official website.
Step 1: Install Gradle
To install Gradle, follow these steps:
- Download the latest version of Gradle from the Gradle website.
- Extract the downloaded file to a directory of your choice.
- Set the
GRADLE_HOMEenvironment variable to the path of the Gradle directory. - Add
GRADLE_HOME/binto yourPATHenvironment variable.
To verify the installation, open a command prompt or terminal and run the following command:
gradle -v
You should see an output that includes the Gradle version and JVM information.
Step 2: Create a New Gradle Project
To create a new Gradle project, follow these steps:
- Open a command prompt or terminal.
- Navigate to the directory where you want to create the project.
- Run the following command to generate a new Gradle project:
gradle init
Follow the prompts to configure your project. Gradle will create the necessary files and directories for you.
Step 3: Explore the Project Structure
A typical Gradle project structure looks like this:
my-gradle-project/
├── build.gradle
├── settings.gradle
└── src/
├── main/
│ ├── java/
│ └── resources/
└── test/
├── java/
└── resources/
build.gradle: The main build script for the project.settings.gradle: Contains settings for multi-project builds.src/main/java: Contains the source code.src/main/resources: Contains the resources required by the application.src/test/java: Contains the test source code.src/test/resources: Contains the resources required by the tests.
Step 4: Writing a Simple Build Script
Let's create a simple Java application with Gradle. Open the build.gradle file and add the following content:
plugins {
id 'java'
}
group 'com.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter:5.8.1'
}
test {
useJUnitPlatform()
}
This script does the following:
- Applies the Java plugin: This provides tasks for compiling Java code, running tests, and creating JAR files.
- Sets the group and version of the project: These are used for publishing the project.
- Configures the repositories: This tells Gradle to use Maven Central to resolve dependencies.
- Defines the dependencies: This adds JUnit 5 for testing.
Step 5: Adding Source Code
Create a simple Java class in src/main/java/com/example/App.java:
package com.example;
public class App {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Create a test class in src/test/java/com/example/AppTest.java:
package com.example;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class AppTest {
@Test
public void testApp() {
assertTrue(true);
}
}
Step 6: Running the Build
To build the project, open a command prompt or terminal in the project directory and run:
gradle build
Gradle will compile the source code, run the tests, and create a JAR file in the build/libs directory.
Step 7: Running the Application
To run the application, use the following command:
gradle run
Ensure you have the application plugin applied in your build.gradle:
plugins {
id 'java'
id 'application'
}
mainClassName = 'com.example.App'
Conclusion
Congratulations! You have successfully created and built a simple Java application using Gradle. This guide covered the basics of setting up Gradle, creating a new project, writing a build script, and running the build. Gradle is a powerful and flexible tool that can help you manage your projects more efficiently. To learn more about Gradle, visit the official documentation.
By following this guide and using the latest versions of dependencies, you ensure your project is up-to-date and taking advantage of the latest features and improvements.
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