> For the complete documentation index, see [llms.txt](https://codewithmeiy.gitbook.io/core-java/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://codewithmeiy.gitbook.io/core-java/build-architectures/gradle.md).

# Gradle

Gradle is an open-source build automation tool that is used for building, testing, and deploying software projects. It uses a Groovy-based domain-specific language (DSL) for build scripts, but it also supports the Kotlin DSL. Gradle is highly customizable and provides a powerful and flexible build system for various types of projects, including Java applications.

### Gradle Installation

Before setting up a Gradle project, you need to install Gradle on your system.

#### Installation on Windows

1. **Download Gradle:**
   * Visit the official Gradle website at <https://gradle.org/install/>.
   * Download the latest binary distribution for Windows.
2. **Extract Gradle:**
   * Create a directory for Gradle (e.g., `C:\Gradle`).
   * Extract the contents of the downloaded Gradle distribution zip file to this directory.
3. **Set Environment Variables:**
   * Add the Gradle `bin` directory to your system's PATH environment variable.
4. **Verify Installation:**
   * Open a command prompt and run `gradle -v` to ensure Gradle is installed correctly.

#### Installation on macOS and Linux

1. **Install SDKMAN! (Optional):**
   * On macOS and Linux, you can use [SDKMAN!](https://sdkman.io/) to manage Gradle installations easily.
2. **Install Gradle using SDKMAN!:**

   * Open your terminal and run the following command to install Gradle using SDKMAN!:

   ```bash
   sdk install gradle
   ```
3. **Verify Installation:**
   * Run `gradle -v` to verify the installation.

### Gradle Project Structure

A typical Gradle project structure consists of the following directories and files:

* `src/` - This directory contains your project's source code.
* `build/` - Gradle generates build outputs and intermediate files in this directory.
* `build.gradle` - The build script for your project, written in Groovy or Kotlin DSL.
* `settings.gradle` - This file configures the project's settings, such as its name and dependencies.

### Example Gradle Project

Let's create a simple Java project using Gradle to illustrate the project structure and build script.

#### Creating a New Gradle Project

1. **Create a Project Directory:**
   * Create a new directory for your Gradle project. For example, `MyJavaProject`.
2. **Initialize Gradle Project:**

   * Open a terminal and navigate to the project directory.
   * Run the following command to initialize a new Gradle project:

   ```bash
   gradle init --type java-application
   ```

   This command will generate the basic project structure and build files.

#### Project Structure

After running the initialization command, your project structure should look like this:

```
MyJavaProject/
├── build/
├── gradle/
├── src/
│   ├── main/
│   │   └── java/
│   │       └── com/
│   │           └── myproject/
│   │               └── App.java
│   └── test/
│       └── java/
│           └── com/
│               └── myproject/
│                   └── AppTest.java
├── build.gradle
└── settings.gradle
```

#### Build Script

Open the `build.gradle` file in your project directory. This is where you define the project's dependencies and configurations. Below is a basic `build.gradle` example:

```groovy
plugins {
    id 'java'
}

repositories {
    jcenter()
}

dependencies {
    implementation 'org.slf4j:slf4j-api:1.7.32'
    implementation 'org.slf4j:slf4j-simple:1.7.32'
}

application {
    mainClassName = 'com.myproject.App'
}

test {
    useJUnitPlatform()
}
```

This script does the following:

* Applies the Java plugin.
* Configures repositories for resolving dependencies.
* Defines project dependencies (in this case, SLF4J).
* Specifies the main class for the application.
* Configures JUnit for testing.

### Building and Running the Project

Now that your Gradle project is set up, you can build and run it:

1. **Building the Project:**

   * Open a terminal and navigate to your project directory.
   * Run the following command to build the project:

   ```bash
   gradle build
   ```

   This command compiles your Java code and creates a JAR file in the `build/libs/` directory.
2. **Running the Application:**

   * Run your Java application with the following command:

   ```bash
   java -jar build/libs/MyJavaProject.jar
   ```

   Replace `MyJavaProject.jar` with the actual JAR file name.

That's it! You've successfully created a Gradle project, defined a build script, and built and executed your Java application using Gradle. Gradle's flexibility allows for more complex project configurations and builds, making it a powerful choice for Java development.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://codewithmeiy.gitbook.io/core-java/build-architectures/gradle.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
