MavenユーザのGradle入門
Mavenが動かなくて嫌気がさして始めてしまった。
読み方は「グレイドル」らしい。
しばらく「グラドル」だと思っていたのは秘密。(グラビアアイドルのようだ。。。)
取得
http://www.gradle.org/downloads
↓
今回は、2.1のバイナリのみをゲット。
好きなところに展開してbin配下にパスを通す。
また、必要に応じて環境変数JAVA_OPTS(Java実行時に追加されるオプション)、GRADLE_OPTS(Gradle実行時に追加されるオプション)を設定する。
プロキシ設定は以下のファイルを[ユーザーディレクトリ]/.gradleディレクトリもしくはプロジェクトルートディレクトリに配置する。
■gradle.properties
systemProp.http.proxyHost=myproxy.co.jp systemProp.http.proxyPort=8080 systemProp.http.proxyUser=**** systemProp.http.proxyPassword=**** systemProp.https.proxyHost=myproxy.co.jp systemProp.https.proxyPort=8080 systemProp.https.proxyUser=**** systemProp.https.proxyPassword=****
コマンドプロンプトを開いて「gradle -v」で動確。環境情報も出る。
> gradle -v ------------------------------------------------------------ Gradle 2.1 ------------------------------------------------------------ Build time: 2014-09-08 10:40:39 UTC Build number: none Revision: e6cf70745ac11fa943e19294d19a2c527a669a53 Groovy: 2.3.6 Ant: Apache Ant(TM) version 1.9.3 compiled on December 23 2013 JVM: 1.8.0_20 (Oracle Corporation 25.20-b23) OS: Windows 7 6.1 x86
プロジェクト用のディレクトリ作成。(今回は「GradleSample」にした。)
コマンドプロンプトを開いて、作成したディレクトリまでcdして「gradle init --type [初期タイプ]」。
ここでは「gradle init --type java-library」と実行。
Mavenとほぼ同じディレクトリ構成とGradleラッパーが作成される。
GradleラッパーはGradleがインストールされていない環境でもGradleが実行できるようにするセット。
MavenでいうPOMの代わりが「build.gradle」。
■build.gradle(自動生成されたコメントは削除している)
apply plugin: 'java' repositories { jcenter() } dependencies { compile 'org.slf4j:slf4j-api:1.7.5' testCompile 'junit:junit:4.11' }
「mvn package」に相当するコマンドが「gradle build」。
> gradle build :compileJava Download https://jcenter.bintray.com/org/slf4j/slf4j-api/1.7.5/slf4j-api-1.7.5.pom Download https://jcenter.bintray.com/org/slf4j/slf4j-parent/1.7.5/slf4j-parent-1.7.5.pom Download https://jcenter.bintray.com/org/slf4j/slf4j-api/1.7.5/slf4j-api-1.7.5.jar :processResources UP-TO-DATE :classes :jar :assemble :compileTestJava Download https://jcenter.bintray.com/junit/junit/4.11/junit-4.11.pom Download https://jcenter.bintray.com/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.pom Download https://jcenter.bintray.com/org/hamcrest/hamcrest-parent/1.3/hamcrest-parent-1.3.pom Download https://jcenter.bintray.com/junit/junit/4.11/junit-4.11.jar Download https://jcenter.bintray.com/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar :processTestResources UP-TO-DATE :testClasses :test :check :build BUILD SUCCESSFUL Total time: 43.989 secs
「build/libs」にjarファイルができる。
Mavenでいう「target」がGradleでいう「build」なのかな。
MavenからGradleに引っ越したい場合はpom.xmlをカレントに置いて「gradle init --type pom」と実行すると、pom.xmlと同等のbuild.gradleが作成される。
他のコマンド(gradleではタスクというらしい)は「gradle tasks」で表示。
> gradle tasks :tasks ------------------------------------------------------------ All tasks runnable from root project - GradleSample ------------------------------------------------------------ Build tasks ----------- assemble - Assembles the outputs of this project. build - Assembles and tests this project. buildDependents - Assembles and tests this project and all projects that depend on it. buildNeeded - Assembles and tests this project and all projects it depends on. classes - Assembles classes 'main'. clean - Deletes the build directory. jar - Assembles a jar archive containing the main classes. testClasses - Assembles classes 'test'. Build Setup tasks ----------------- init - Initializes a new Gradle build. [incubating] wrapper - Generates Gradle wrapper files. [incubating] Documentation tasks ------------------- javadoc - Generates Javadoc API documentation for the main source code. Help tasks ---------- components - Displays the components produced by root project 'GradleSample'. dependencies - Displays all dependencies declared in root project 'GradleSample'. dependencyInsight - Displays the insight into a specific dependency in root project 'GradleSample'. help - Displays a help message projects - Displays the sub-projects of root project 'GradleSample'. properties - Displays the properties of root project 'GradleSample'. tasks - Displays the tasks runnable from root project 'GradleSample'. Verification tasks ------------------ check - Runs all checks. test - Runs the unit tests. Other tasks ----------- install - Installs the 'archives' artifacts into the local Maven repository. Rules ----- Pattern: clean<TaskName>: Cleans the output files of a task. Pattern: build<ConfigurationName>: Assembles the artifacts of a configuration. Pattern: upload<ConfigurationName>: Assembles and uploads the artifacts belonging to a configuration. To see all tasks and more detail, run with --all. BUILD SUCCESSFUL Total time: 14.506 secs
表示されるタスクは「apply plugin」で追加しているプラグインによって変わり、今回はJavaプラグインを追加しているため、「Build tasks」「Documentation tasks」「Verification tasks」「Other tasks」がデフォルトよりも増えている。
また、独自でタスクを定義したい場合はbuild.gradleに下記のように定義し、「gradle [定義したタスク名]」で実行することができる。
task hello << { println 'Hello World !'; }
また、「gradle properties」コマンドはGradleおよびプロジェクトの各種設定を表示してくれて便利。
プラグインについて
MavenプラグインはGradleでは使えない。
ここでは、「Eclipse対応」と「アーカイブタイプの変更」をするプラグインを使用してみる。
「apply plugin」というキーワードで「apply plugin: 'eclipse'」と「apply plugin: 'war'」をbuild.gradleに追加する。
■build.gradle
apply plugin: 'java' apply plugin: 'eclipse' // 追加行 apply plugin: 'war' // 追加行 [compileJava, compileTestJava]*.options*.encoding = 'UTF-8' // 追加行 repositories { jcenter() } dependencies { compile 'org.slf4j:slf4j-api:1.7.5' testCompile 'junit:junit:4.11' }
「gradle eclipse」コマンドをたたくと「.classpath」と「.project」が作成されてEclipseプロジェクトになって、既存のEclipseプロジェクトとしてインポート可能になる。
「gradle build」コマンドをたたくと「build/libs」にwarファイルができる。
リポジトリについて
デフォルトだと「Bintray」というソフトウェア配布サービスの「JCenter」というリポジトリを使用している。詳しくはここ。
MavenCentralを使用したい場合はbuild.gradleを下記のように修正する。
■build.gradle
apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'war' repositories { // jcenter() // コメントアウトした mavenCentral() // 追加行 } dependencies { compile 'org.slf4j:slf4j-api:1.7.5' testCompile 'junit:junit:4.11' }
独自のMavenリポジトリを設定したい場合は下記。
■build.gradle
apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'war' repositories { // jcenter() // コメントアウトした // mavenCentral() // コメントアウトした maven { // url "http://repo.sample.jp/maven" // 追記行 } // } dependencies { compile 'org.slf4j:slf4j-api:1.7.5' testCompile 'junit:junit:4.11' }
依存ライブラリ
依存ライブラリは「dependencies」に記載する。
記載する際は以下の4つのキーワードを使い分ける。
- compile
- プロジェクトのコードをコンパイルするのに必要な依存関係。
- runtime
- クラスを実行する際に必要な依存関係。デフォルトで「compile」で定義したものが含まれる。
- testCompile
- プロジェクトのテストコードをコンパイルするのに必要な依存関係。
- testRuntime
- テストを実行する際に必要な依存関係。デフォルトで「compile」、「runtime」、「testCompile」で定義したものが含まれる。
依存ライブラリの指定方法は下記のいずれか。
- '[GroupId]:[ArtifactID]:[Version]'
- 例)compile 'org.slf4j:slf4j-api:1.7.5'
- group: '[GroupID]', name: '[ArtifactId]', version: '[Version]'
- 例)compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.5'
参考:
http://gradle.monochromeroad.com/docs/index.html