1. 프로젝트 디렉토리 생성

$ mkdir -p gradle-test

2. 소스 코드 작성

$ cd gradle-test
$ mkdir -p src/main/java/test

따라서 gradle-test/src/main/java/test 구조가 디렉토리가 있는 것이다.

2-1. Hello.java

package test;

public class Hello {
  public static void main(String[] args) {
     You you = new You();
     System.out.println(you.say());
  }
}

2-2. You.java

package test;

public class You {
  public String say() {
    return "Hello world!";
  }
}

3. build.gradle 파일

이 파일은 gradle-test 하위에 있으면 된다.

apply plugin: 'java'

4. Gradle Build

# gradle build

BUILD SUCCESSFUL in 1s
2 actionable tasks: 2 executed

5. 확인

build 디렉토리 생성되고 그 안에 class 파일들이 생성된다.


6. Dependency 설정

6-1. You.java 수정

package test;

import org.joda.time.LocalTime;

public class You {
  public String say() {
    LocalTime currentTime = new LocalTime();
    return "Hello world! " + currentTime;
  }

6-2. 빌드

# gradle build

> Task :compileJava FAILED
/src/gradle-test/src/main/java/test/You.java:3: error: package org.joda.time does not exist
import org.joda.time.LocalTime;
                    ^
/src/gradle-test/src/main/java/test/You.java:7: error: cannot find symbol
    LocalTime currentTime = new LocalTime();
    ^
  symbol:   class LocalTime
  location: class You
/src/gradle-test/src/main/java/test/You.java:7: error: cannot find symbol
    LocalTime currentTime = new LocalTime();
                                ^
  symbol:   class LocalTime
  location: class You
3 errors

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileJava'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 1s
1 actionable task: 1 executed

6-3. build.gradle 수정

apply plugin: 'java'

repositories { mavenCentral() }
dependencies {
  compile "joda-time:joda-time:2.2"
}

6.4 재빌드

# gradle build

BUILD SUCCESSFUL in 6s
2 actionable tasks: 2 executed