Skip to content

Gradle

참고 자료

  • https://learn.tomgregory.com/courses/get-going-with-gradle

What is Gradle?

  • build automation tool
    • takes your code and packages it into deployable unit
    • applies to small or large project
  • written in the Groovy language
  • highly configurable

Why Use Gradle?

  • makes building & running easy
  • no need to install
    • Gradle wrapper comes bundled with project
    • just ./gradlew build
  • very concise

Key Concepts

1. build.gradle

  • build.gradle is the Bradle build script file
    • written in a Groovy DSL
    • = Maven's pom.xml
    • nomarlly lives in top level of the project
super simple build.gradle
puglins { // (1)!
    id 'java' // (2)!
}

group 'org.example'  // (3)!
version '1.0-SNAPSHOT'

repositories { // (4)!
    mavenCentral()
}

dependencies { // (5)!
    testCompile group: 'junit', name: 'junit', version: '4.12'
}
  1. plugins - additional build functionality
  2. plugin java - 자바 코드를 compile, test 할 수 있도록 한다.
  3. build metadata - information about your build
  4. repositories - where to find dependencies
  5. dependencies - requirements for building your code

2. Tasks

  • defines a unit of work
  • invoked from the command line
    • ./gradlew build
  • see all available tasks - ./gradlew tasks
  • can create your own custom tasks
  • tasks have dependencies on other tasks
  • creates a tasks graph

3. Wrapper

  • script used to invoke Gradle and run tasks
    • 🍎./gradlew build
    • Win gradlew.bat build
  • git 등의 version control 에 항상 포함해야한다.

Gradle 설치 확인

C:\Users\1>gradle --version

Welcome to Gradle 8.0.2!

Here are the highlights of this release:
 - Improvements to the Kotlin DSL
 - Fine-grained parallelism from the first build with configuration cache
 - Configurable Gradle user home cache cleanup

For more details see https://docs.gradle.org/8.0.2/release-notes.html


------------------------------------------------------------
Gradle 8.0.2
------------------------------------------------------------

Build time:   2023-03-03 16:41:37 UTC
Revision:     7d6581558e226a580d91d399f7dfb9e3095c2b1d

Kotlin:       1.8.10
Groovy:       3.0.13
Ant:          Apache Ant(TM) version 1.10.11 compiled on July 10 2021
JVM:          17.0.5 (Oracle Corporation 17.0.5+9-LTS-191)
OS:           Windows 10 10.0 amd64

Simple Gradle Project Structure

.
├── build.gradle
├── gradle
│   └─wrapper
|      └──gradle-wrapper.jar
|      └──gradle-wrapper.properties
|── gradlew
|── gradlew.bat
└── settings.gradle

위의 모든 파일은 git 에 포함되어야 한다. .gradle 은 포함되면 안된다.

Gradle 프로젝트 시작하기

  • gradle init
C:\Users\1\Desktop\gradle-basic>gradle init

Select type of project to generate:
  1: basic
  2: application
  3: library
  4: Gradle plugin
Select build script DSL:
  1: Groovy
  2: Kotlin---> 0% EXECUTING [7s]
Enter selection (default: Groovy) [1..2] 1

Generate build using new APIs and behavior (some features may change in the next minor release)? (default: no) [yes, no]

Project name (default: gradle-basic):

> Task :init
Get more help with your project: Learn more about Gradle by exploring our samples at https://docs.gradle.org/8.0.2/samples

BUILD SUCCESSFUL in 50s
2 actionable tasks: 2 executed
기본 build.gradle
/*
 * This file was generated by the Gradle 'init' task.
 *
 * This is a general purpose Gradle build.
 * Learn more about Gradle by exploring our samples at https://docs.gradle.org/8.0.2/samples
 */
기본 settings.gradle
/*
 * This file was generated by the Gradle 'init' task.
 *
 * The settings file is used to specify which projects to include in your build.
 *
 * Detailed information about configuring a multi-project build in Gradle can be found
 * in the user manual at https://docs.gradle.org/8.0.2/userguide/multi_project_builds.html
 */

rootProject.name = 'gradle-basic'

Last update: March 8, 2023
Created: March 8, 2023