Gradle Wrapperの使い方

Gradle Wrapperの使用方法

このチュートリアルでは、プロジェクトのGradleラッパーの作成方法と使用方法を示します。

What is Gradle wrapper?
Gradleラッパーを使用すると、システムにGradleがインストールされていなくても、Gradleタスクを実行できます。

1. Gradleラッパーを作成する

1.1 Declares a wrapper task.

build.gradle

task wrapper(type: Wrapper) {
    gradleVersion = '2.10' //we want gradle 2.10 to run this project
}

1.2 Run the wrapper task with gradle wrapper

$ gradle wrapper

:wrapper

BUILD SUCCESSFUL

1.3 The following files will be created, remember to add these files and folders to your version control system (e.g GitHub or Bitbucket).

|-gradle
  |--- wrapper
      |--- gradle-wrapper.jar
      |--- gradle-wrapper.properties
|-gradlew
|-gradlew.bat

Gradleタスクを実行するためのUNIXシェルスクリプトであるgradlewファイルを確認します。

2. Gradle Wrapperの使用方法

2.1 Assume your Gradle project along with the Gradle wrapper files are added to GitHub, and someone wants to clone your project and try to run it without installing Gradle on their system.

2.2 They can download the project and run it with gradlew, read comments for self-explanatory :

$ git clone https://github.com/example/spring4-mvc-gradle-annotation-hello-world.git
$ cd spring4-mvc-gradle-annotation-hello-world

# wait, build? we don't have Gradle install yet? No problem, gradlew will handle it.
$ ./gradlew build

# first time, download the gradle 2.10..
# download project dependencies
# run the gradle build task
# ...wow, we run a Gradle task without installing Gradle!

# try run jettyRun task
$ ./gradlew jettyRun
16:16:21 INFO  Jetty 9.2.10.v20150310 started and listening on port 8080
16:16:21 INFO  spring4 runs at:
16:16:21 INFO    http://localhost:8080/spring4
Press any key to stop the server.

$ ./gradlew task

完了しました。