Skip to main content

How to Run Gradle Build Commands from a Batch Script

Gradle is a powerful build automation tool widely used for Java, Kotlin, Android, and multi-language projects. It combines the dependency management of Maven with the flexibility of a Groovy/Kotlin-based build DSL, offering incremental builds, build caching, and parallel execution out of the box. Running Gradle from a Batch Script enables automated CI/CD pipelines, scheduled builds, and reproducible release processes.

In this guide, we will explore how to execute Gradle build commands from a Batch Script, covering common tasks, wrapper usage, multi-project builds, and production-grade automation.

Understanding Gradle vs. Gradle Wrapper

ToolCommandDescription
GradlegradleSystem-installed Gradle (version may vary)
Gradle Wrappergradlew.batProject-bundled Gradle (ensures consistent version)
tip

Always use the Gradle Wrapper (gradlew.bat) instead of the system-installed gradle. The wrapper ensures every developer and CI server uses exactly the same Gradle version, eliminating "works on my machine" issues.

Method 1: Basic Gradle Build

@echo off
setlocal

echo =============================================
echo GRADLE BUILD
echo =============================================
echo.

:: Use the wrapper if available
if exist "gradlew.bat" (
set "GRADLE=call gradlew.bat"
) else (
set "GRADLE=call gradle"
)

echo Building...
%GRADLE% build

if %errorlevel%==0 (
echo.
echo [SUCCESS] Build complete.
echo.
echo Artifacts:
dir /b build\libs\*.jar 2>nul
) else (
echo.
echo [ERROR] Build failed.
)

pause

Common Gradle Tasks

TaskDescription
cleanDelete build output directory
buildCompile, test, and assemble
testRun unit tests only
assembleCompile and package (skip tests)
jarCreate JAR file
warCreate WAR file
checkRun all verification tasks
dependenciesDisplay dependency tree
tasksList available tasks

Method 2: Clean Build with Specific Tasks

@echo off
setlocal

if exist "gradlew.bat" (set "GW=call gradlew.bat") else (set "GW=call gradle")

echo Clean build...
%GW% clean build --no-daemon

if %errorlevel%==0 (
echo [SUCCESS]
) else (
echo [FAILURE]
)

pause

Useful Gradle Flags

FlagDescription
--no-daemonDo not use the Gradle daemon (better for CI)
--parallelBuild independent modules in parallel
--build-cacheEnable the build cache for faster rebuilds
-qQuiet mode, only errors
--infoInformational logging
--debugDebug logging (verbose)
-x testExclude the test task
-P key=valueSet a project property
-D key=valueSet a system property

Method 3: Full Build Pipeline

@echo off
setlocal enabledelayedexpansion

for /f "tokens=2 delims==" %%T in ('wmic os get LocalDateTime /value') do set "dt=%%T"
set "logfile=build_%dt:~0,8%_%dt:~8,4%.log"

if exist "gradlew.bat" (set "GW=call gradlew.bat") else (set "GW=call gradle")

echo =============================================
echo GRADLE BUILD PIPELINE
echo %date% %time:~0,8%
echo =============================================
echo.

:: Stage 1: Clean
echo [1/4] Cleaning...
%GW% clean -q 2>> "%logfile%"
if !errorlevel! neq 0 (
echo [FAIL] Clean failed. See %logfile%
pause
exit /b 1
)
echo Done.

:: Stage 2: Compile
echo [2/4] Compiling...
%GW% compileJava 2>> "%logfile%"
if !errorlevel! neq 0 (
echo [FAIL] Compilation failed. See %logfile%
pause
exit /b 1
)
echo Done.

:: Stage 3: Test
echo [3/4] Running tests...
%GW% test 2>> "%logfile%"
if !errorlevel! neq 0 (
echo [FAIL] Tests failed.
echo.
echo Test report: build\reports\tests\test\index.html
pause
exit /b 1
)
echo Done.

:: Stage 4: Package
echo [4/4] Packaging...
%GW% assemble -q 2>> "%logfile%"
if !errorlevel! neq 0 (
echo [FAIL] Packaging failed. See %logfile%
pause
exit /b 1
)
echo Done.

:: Show artifacts
echo.
echo Artifacts:
for %%F in (build\libs\*.*) do (
echo %%~nxF (%%~zF bytes^)
)

echo.
echo =============================================
echo BUILD COMPLETE
echo Log: %logfile%
echo =============================================

pause

Method 4: Multi-Project Build

For Gradle projects with multiple subprojects:

@echo off
setlocal

if exist "gradlew.bat" (set "GW=call gradlew.bat") else (set "GW=call gradle")

echo =============================================
echo MULTI-PROJECT BUILD
echo =============================================
echo.

:: List all subprojects
echo Available subprojects:
%GW% projects -q 2>nul
echo.

:: Build all projects in parallel
echo Building all projects...
%GW% clean build --parallel --no-daemon

if %errorlevel%==0 (
echo.
echo [SUCCESS] All subprojects built.
) else (
echo.
echo [ERROR] Build failed.
)

echo.

:: Build a specific subproject
echo Building only the API subproject...
%GW% :api:build -x test -q

pause

Method 5: Release and Publish Workflow

@echo off
setlocal enabledelayedexpansion

if exist "gradlew.bat" (set "GW=call gradlew.bat") else (set "GW=call gradle")

echo =============================================
echo GRADLE RELEASE WORKFLOW
echo =============================================
echo.

:: Get current version
set "version=unknown"
for /f "delims=" %%V in ('%GW% properties -q 2^>nul ^| findstr /b /c:"version:"') do (
set "ver_line=%%V"
)
if defined ver_line (
for /f "tokens=1,* delims= " %%A in ("!ver_line!") do set "version=%%B"
)
echo Current version: !version!
echo.

:: Full verification build
echo [1/3] Full verification build...
%GW% clean check --no-daemon
if !errorlevel! neq 0 (
echo [ABORT] Verification failed.
pause
exit /b 1
)
echo Passed.

:: Create distribution
echo [2/3] Creating distribution...
%GW% assemble -q
if !errorlevel! neq 0 (
echo [ABORT] Assembly failed.
pause
exit /b 1
)
echo Done.

:: Publish (if configured)
echo [3/3] Publishing...
%GW% publish 2>nul
if !errorlevel!==0 (
echo Published.
) else (
echo [SKIP] Publish task failed or not configured.
)

echo.
echo =============================================
echo RELEASE v!version! COMPLETE
echo =============================================

pause

Dependency Management

Viewing the Dependency Tree

@echo off
if exist "gradlew.bat" (set "GW=call gradlew.bat") else (set "GW=call gradle")

echo Dependency tree:
echo ================
%GW% dependencies --configuration runtimeClasspath
pause

Refreshing Dependencies

@echo off
if exist "gradlew.bat" (set "GW=call gradlew.bat") else (set "GW=call gradle")

echo Refreshing all dependencies...
%GW% build --refresh-dependencies
pause

Common Mistakes

The Wrong Way: Using System Gradle Instead of Wrapper

:: WRONG - System Gradle version may not match project requirements
gradle build
:: Could fail if system has Gradle 7 but project needs Gradle 8

Output Concern: Different Gradle versions can have incompatible build file syntax and plugin APIs. The Gradle Wrapper (gradlew.bat) ensures the correct version is used automatically. If the wrapper is not present, generate it with gradle wrapper --gradle-version 8.5.

The Wrong Way: Not Using call with gradlew.bat

:: WRONG - Script terminates after Gradle finishes
gradlew.bat clean build
echo This line never executes

gradlew.bat is a Batch file. Without call, the current script terminates when Gradle completes. Always use call gradlew.bat.

The Wrong Way: Running the Daemon in CI

:: PROBLEMATIC in CI - Daemon persists between builds, potentially using stale state
gradlew.bat build

In CI environments (Jenkins, GitHub Actions), use --no-daemon to ensure clean, isolated builds. The daemon is beneficial for local development but can cause issues in disposable CI containers.

Best Practices

  1. Use the Gradle Wrapper: Always prefer gradlew.bat over system-installed gradle.
  2. Use call in Batch scripts: Prevents script termination after Gradle runs.
  3. Use --no-daemon in CI: Avoids stale daemon state in ephemeral build environments.
  4. Enable parallel builds: Use --parallel for multi-project builds to reduce build time.
  5. Check test reports on failure: Gradle generates HTML test reports at build/reports/tests/test/index.html.

Conclusion

Running Gradle build commands from a Batch Script is accomplished by calling the Gradle Wrapper (gradlew.bat) with the desired tasks and flags. The critical details are using call to prevent script termination, preferring the wrapper for version consistency, and using --no-daemon in CI environments. By structuring builds as staged pipelines with clean, compile, test, and package phases, teams achieve reproducible, automated builds that work identically on developer machines and CI servers. Gradle's incremental build and caching capabilities make repeated builds significantly faster than full rebuilds.