# Gradle

---

## Overview

Your private Gradle artifacts and public dependencies from Maven Central, all managed through one registry.

---

## Supported Clients

JFrog Fly supports Gradle artifacts with:

- **Gradle CLI** (`./gradlew publish`, `./gradlew build`) - The Gradle build tool with wrapper

---

## Upload / Publish Artifact

### With Fly App

Activate Gradle in your Fly App to configure Gradle globally with Fly. The Fly App automatically creates an init script at `~/.gradle/init.d/` that configures both dependency resolution and publishing.

Then publish as usual:

```bash
./gradlew publish
```

### Manual Configuration

**1. Generate an access token** in Fly Token Management

**2. Configure Gradle:**

**Option A: Project Configuration**

Edit `build.gradle`:

```groovy
plugins {
    id 'java'
    id 'maven-publish'
}

repositories {
    maven {
        url "https://<your-fly-subdomain>.jfrog.io/artifactory/maven"
        credentials {
            username = "<your-fly-username>"
            password = "<your-fly-token>"
        }
    }
}

publishing {
    publications {
        maven(MavenPublication) {
            from components.java
        }
    }
    repositories {
        maven {
            url "https://<your-fly-subdomain>.jfrog.io/artifactory/maven"
            credentials {
                username = "<your-fly-username>"
                password = "<your-fly-token>"
            }
        }
    }
}
```

**Option B: Global Configuration**

Create an init script at `~/.gradle/init.d/fly.gradle`:

```groovy
def flyUrl = "https://<your-fly-subdomain>.jfrog.io/artifactory/maven"
def flyUsername = "<your-fly-username>"
def flyToken = "<your-fly-token>"

// Configure project repositories for dependency resolution
allprojects { project ->
    project.repositories {
        maven {
            name = "Fly"
            url = uri(flyUrl)
            credentials {
                username = flyUsername
                password = flyToken
            }
        }
    }
    
    // Configure publishing for projects that apply maven-publish plugin
    project.plugins.withId('maven-publish') {
        project.publishing {
            repositories {
                clear()  // Ensure Fly is the only publishing destination
                maven {
                    name = "Fly"
                    url = uri(flyUrl)
                    credentials {
                        username = flyUsername
                        password = flyToken
                    }
                }
            }
        }
    }
}
```

> [!NOTE]
> This init script applies to all Gradle projects. The `plugins.withId` pattern ensures publishing configuration only applies to projects using the `maven-publish` plugin. The `clear()` call ensures Fly is the only publishing destination.

**3. Publish artifact:**

```bash
./gradlew publish
```

---

## Download / Build Dependencies

### With Fly App

Activate Gradle in your Fly App to configure Gradle globally with Fly. The Fly App automatically creates an init script that configures dependency resolution from Fly.

Then build as usual:

```bash
./gradlew build
```

### Manual Configuration

**1. Generate an access token** in Fly Token Management

**2. Configure Gradle** as shown in the Publish section above (Option A or Option B).

**3. Build project:**

```bash
./gradlew build
```

### From Public Registry

When you build a project with dependencies that aren't in your Fly Registry, JFrog Fly automatically fetches them from Maven Central and caches them for future use.

```bash
./gradlew build
```

---

## Upload/Download Artifacts with CI

To publish and build Gradle artifacts with CI, update your GitHub Actions workflow to include the Fly action.

Simply ask your coding agent: **"Configure my workflows with Fly"** and Fly MCP will configure your GitHub Actions workflow yml file, as follows:

**1. Add permissions** (top level, after `on:`):
```yaml
permissions:
  contents: read
  id-token: write
```

**2. Add Fly Action** (after `actions/setup-java`, before Gradle commands):
```yaml
- uses: jfrog/fly-action@v1              # Setup Fly package managers
```

### GitHub Action Example

```yaml
name: Build and Publish Gradle Artifact

on:
  push:
    branches: [main]

permissions:
  contents: read
  id-token: write

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-java@v4
        with:
          java-version: '17'
          distribution: 'temurin'

      - uses: jfrog/fly-action@v1              # Setup Fly package managers

      - run: ./gradlew build                   # Dependencies from Fly registry

      - run: ./gradlew publish                 # Publish to Fly registry
```

---

*Back to [Package Managers →](../)*
