Automating Java-Gradle Project - Simplifying: Cloning, Building, and Running with Bash Scripts

Development Workflow
Jan 2025
GitBashGradleShell Scripting

"A comprehensive guide to creating bash scripts for cloning, running, and managing multiple Git repositories efficiently."

Table of Contents

Introduction

Managing multiple Git repositories can be challenging. This guide provides a set of bash scripts to automate the process of cloning, running, and managing multiple Gradle projects efficiently.

Prerequisites

  • Git installed
  • Bash shell (Linux/macOS/Windows)
  • Gradle
  • SSH access to GitHub repositories

Project Structure and Scripts

1. Repository Configuration

Create a projects.txt file to define the repositories you want to clone:

projects.txt
gradle git@github.com:profile/repo1 repo1-name
gradle git@github.com:profile/repo2 repo2-name
other git@github.com:profile/utility-repo utility-repo-name

File Format

  • First column: Project type (gradle or other)
  • Second column: Git repository SSH URL
  • Third column: Local directory name

2. Cloning Repositories

fetch-all.sh

#!/bin/bash

# Usage: ./fetch-all.sh <branch_name>
# Example: ./fetch-all.sh master

# Check if branch name is provided
if [ "$#" -ne 1 ]; then
    echo "Usage: $0 <branch_name>"
    exit 1
fi

branch="$1"

# Validate projects.txt exists
if [ ! -f projects.txt ]; then
    echo "Error: projects.txt file not found."
    exit 1
fi

# Initialize settings.gradle
echo "rootProject.name = 'myproject'" > settings.gradle
echo "" >> settings.gradle

# Process each repository
while IFS= read -r line || [[ -n "$line" ]]; do
    type=$(echo "$line" | awk '{print $1}')
    repo_url=$(echo "$line" | awk '{print $2}')
    project_name=$(echo "$line" | awk '{print $3}')

    # Validate input
    if [ -z "$type" ] || [ -z "$repo_url" ] || [ -z "$project_name" ]; then
        echo "Skipping invalid entry: $line"
        continue
    fi

    # Clone repository
    echo "Cloning $repo_url into $project_name"
    git clone "$repo_url" "$project_name"

    # Check clone success
    if [ $? -eq 0 ]; then
        (cd "$project_name" &&
            echo "Checking out branch $branch" &&
            if ! git checkout "$branch"; then
                echo "Branch $branch not found in $project_name"
            fi
        )

        # Include in settings.gradle for Gradle projects
        if [ "$type" == "gradle" ]; then
            echo "include ':$project_name'" >> settings.gradle
            echo "project(':$project_name').projectDir = file('$project_name')" >> settings.gradle
        fi
    else
        echo "Failed to clone $repo_url"
    fi

done < projects.txt

3. Running All Projects

boot-run.sh
#!/bin/bash

# Dynamically read projects from settings.gradle
projects=($(grep "include" settings.gradle | cut -d "'" -f 2))

# Open terminal tabs and run each Gradle project
for project in "${projects[@]}"; do
    gnome-terminal --tab --title="$project" -- bash -c "cd $project; ./gradlew clean bootRun; exec bash"
done

4. Stopping All Projects

stop-all.sh
#!/bin/bash

# Find all running `./gradlew bootRun` processes
pids=$(pgrep -f "./gradlew clean bootRun")

if [ -z "$pids" ]; then
    echo "No bootRun processes are currently running."
else
    echo "Stopping all running bootRun processes..."
    kill -SIGTERM $pids

    # Wait and verify
    sleep 5
    remaining_pids=$(pgrep -f "./gradlew clean bootRun")
    if [ -z "$remaining_pids" ]; then
        echo "All bootRun processes stopped successfully."
    else
        echo "Forcibly stopping remaining processes..."
        kill -SIGKILL $remaining_pids
    fi
fi

CMD Usage

  1. Prepare Repositories:

    # Edit projects.txt with your repositories
    vim projects.txt
    
  2. Clone Repositories:

    # Clone all repositories on the master branch
    ./fetch-all.sh master
    
  1. Run All Projects:

    # Start all Gradle projects in separate terminal tabs
    ./boot-run.sh
    
  2. Stop All Projects:

    # Stop all running Gradle projects
    ./stop-all.sh
    

Best Practices

  • Use SSH keys for seamless GitHub authentication
  • Regularly update projects.txt
  • Ensure consistent Gradle project structure
  • Handle different project types flexibly

Troubleshooting

  • Verify SSH access to repositories
  • Check Gradle and Git installations
  • Ensure proper permissions for bash scripts
    chmod +x fetch-all.sh boot-run.sh stop-all.sh