Posts Gradle 입문(1)
Post
Cancel

Gradle 입문(1)

Gradle ?

image => https://docs.gradle.org/current/userguide/gradle_basics.html

Gradle 프로젝트 기본 구조

  • The init task generates the new project with the following structure: ``` ├── .gradle (0) │ └── ⋮ ├── gradle (1) │ └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew (2) ├── gradlew.bat (2) ├── settings.gradle (3) └── app ├── build.gradle (4) └── src ├── main │ └── java (5) │ └── demo │ └── App.java └── test └── java (6) └── demo └── AppTest.java

(0) : Project-specific cache directory generated by Gradle. (1) : Generated folder for wrapper files (2) : Gradle wrapper start scripts (3) : Settings file to define build name and subprojects (4) : Build script of app project (5) : Default Java source folder (6) : Default Java test source folder

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

# Gradle Wrapper란 ?
- The Gradle Wrapper is the preferred way of starting a Gradle build.
- The Wrapper downloads (if needed) and then invokes a specific version of Gradle declared in the build.
- In your newly created project, take a look at the files used by the Gradle Wrapper first.
- It consists of a shell script for macOS and Linux and a batch script for Windows.
- **These scripts allow you to run a Gradle build without requiring that Gradle be installed on your system.**
- **It also helps ensure that the same version of Gradle is used for builds by different developers and between local and CI machines.**
- From now on, you will never invoke Gradle directly; instead, you will use the Gradle wrapper.

## Gradle Wrapper 호출하기
- 유닉스 계열 : `$ ./gradlew build`
- 윈도우 : `$ gradlew.bat build`
- The first time you run the wrapper, it downloads and caches the Gradle binaries if they are not already installed on your machine.
- The Gradle Wrapper is now available for building your project.
- Add it to your version control system, and everyone that clones your project can build it just the same.
- It can be used in the exact same way as an installed version of Gradle.
- In this case, we invoked Gradle through the wrapper to build our project, so we can see that the app directory now includes a new `build` folder.
- All the files generated by the build process go into the `build` directory unless otherwise specified.

# Build & Project
- ![image](https://github.com/zz9z9/zz9z9.github.io/assets/64415489/9dd29a37-6617-44ba-99bf-3fcaa0fb3c6f)
=> 출처 : https://docs.gradle.org/current/userguide/part1_gradle_init.html

- A build contains:
  - A top level `settings.gradle(.kts)` file.
  - A root project.
  - One or more subprojects, each with its own `build.gradle(.kts)` file.

- Some builds may contain a `build.gradle.kts` file in the root project but **this is NOT recommended.**
- The `libs.version.toml` file is a version catalog used for dependency management which you will learn about in a subsequent section of the tutorial.

- A build
  - Represents **a bundle of related software** that you can build, test, and/or release together.
  - Can optionally include other builds (i.e. additional software such as libraries, plugins, build-time tools, etc).

- A project
  - Represents **a single piece of your architecture** - a library, an app, a Gradle plugin, etc.
  - Can optionally contain other projects.

# setting.gradle 파일
- The primary purpose of the settings file is to add subprojects to your build.
- Gradle supports single and multi-project builds.
  - For single-project builds, the settings file is optional.
  - For multi-project builds, the settings file is mandatory and declares all subprojects.
- A project is composed of one or more subprojects (sometimes called modules).
- Gradle reads the `settings.gradle` file to figure out **which subprojects comprise a project build.**

```groovy
plugins {
    // Apply the foojay-resolver plugin to allow automatic download of JDKs
    id("org.gradle.toolchains.foojay-resolver-convention") version "0.7.0"
}

rootProject.name = "tutorial"
include("app")
  • The tutorial root project includes the app subproject.
  • The presence of the include call turns the app directory into a subproject.

build.gradle 파일

  • Each subproject contains its own build.gradle.kts file.
  • The build.gradle.kts file is the core component of the build process and defines the tasks necessary to build the project.
  • The build.gradle.kts file is read and executed by Gradle.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
plugins {
    // Apply the application plugin to add support for building a CLI application in Java.
    application
}

repositories {
    // Use Maven Central for resolving dependencies.
    mavenCentral()
}

dependencies {
    // Use JUnit Jupiter for testing.
    testImplementation("org.junit.jupiter:junit-jupiter:5.9.1")

    // This dependency is used by the application.
    implementation("com.google.guava:guava:32.1.2-jre")
}

// Apply a specific Java toolchain to ease working on different environments.
java {
    toolchain {
        languageVersion.set(JavaLanguageVersion.of(17))
    }
}

application {
    // Define the main class for the application.
    mainClass.set("com.gradle.tutorial.App")
}

tasks.named<Test>("test") {
    // Use JUnit Platform for unit tests.
    useJUnitPlatform()
}

Task란 ?

A task is a basic unit of work that can be done by Gradle as part of the build. $ ./gradlew tasks

  • $ ./grad lew build시 수행되는 Task 예시
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    
    > Task :app:compileJava
    > Task :app:processResources
    > Task :app:classes
    > Task :app:jar
    > Task :app:startScripts
    > Task :app:distTar
    > Task :app:distZip
    > Task :app:assemble
    > Task :app:compileTestJava
    > Task :app:processTestResources
    > Task :app:testClasses
    > Task :app:test
    > Task :app:check
    > Task :app:build
    
  • The build task uses the source code and its dependencies to build the app.
  • As seen in the output, the build task compiles, assembles, tests, and checks the code.
  • The tasks are printed in order of execution. The jar tasks is a dependency of the build task.

참고자료


  • https://docs.gradle.org/current/userguide/gradle_basics.html
  • https://docs.gradle.org/current/userguide/settings_file_basics.html
  • https://docs.gradle.org/current/userguide/part1_gradle_init.html
  • https://docs.gradle.org/current/userguide/part2_gradle_tasks.html
This post is licensed under CC BY 4.0 by the author.

키릴문자를 아시나요 ?

Gradle 입문(2)