環境構築
Eclipseのダウンロードとインストール
Pleiades All in One Eclipse 4.8 Photon のページから、Java用のFull Editionをダウンロードします。
Windowsの場合は、zip解凍時の注意を必ず参照の事
Groovyプラグインのインストール
Eclipseを起動し、[ヘルプ]->[新規ソフトウエアのインストール...]から
- [作業対象] に http://dist.springsource.org/release/GRECLIPSE/e4.8 を入力し、[追加...]します
- [Main Package (必須)]を選択してインストールします
- Groovy 2.5 を使用する場合は、[More Compilers(オプション)] -> [Groovy Compiler 2.5] にもチェックを付けてインストールします
- Eclipseの再起動が求められるので、再起動します
※ 2018/08/07 追記 Groovy-Eclipse 3.0.0がリリースされましたので、Update-SiteのURLを変更しました。
Gradleプロジェクトの作成
- [ファイル]->[新規]->[Gradleプロジェクト] を選択します
- 適当な名前を付けてプロジェクトを作成します
build.gradle ファイルの編集
build.gradleファイルを開き、次のように編集します。
/*
* This build file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Java Library project to get you started.
* For more details take a look at the Java Libraries chapter in the Gradle
* user guide available at https://docs.gradle.org/4.3/userguide/java_library_plugin.html
*/
// Apply the java-library plugin to add support for Java Library
//apply plugin: 'java-library'
apply plugin: 'groovy'
// In this section you declare where to find the dependencies of your project
repositories {
// Use jcenter for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}
dependencies {
// https://mvnrepository.com/artifact/org.codehaus.groovy/groovy-all
compile group: 'org.codehaus.groovy', name: 'groovy-all', version: '2.4.15'
// This dependency is exported to consumers, that is to say found on their compile classpath.
//api 'org.apache.commons:commons-math3:3.6.1'
// This dependency is used internally, and not exposed to consumers on their own compile classpath.
//implementation 'com.google.guava:guava:23.0'
// Use JUnit test framework
//testImplementation 'junit:junit:4.12'
testCompile 'junit:junit:4.12'
}
ソース・フォルダーの作成
- src/mainの下に
groovy
フォルダーを作成します。 - プロジェクトを右クリックし、[Gradle]->[Gradleプロジェクトのリフレッシュ]を選択します。
初めてのGroovyプログラムを作成する
- [ファイル]->[新規]->[その他...]を選択します。
- [Groovy]->[Groovy クラス]を選択し、[次へ]をクリックします。
- [名前]に
FirstGroovy
と入力し、[完了]をクリックするとファイルが作成されます。 - 次のように編集し、保存します。
package sample
class FirstGroovy {
static void main(args) {
println "Hello Groovy!!"
}
}
Groovyプログラムの実行
作成したプログラムは、次の3つの方法で実行することができます。
Groovy Consoleから実行する
- 右クリックで表示されるコンテキストメニューから、[実行]->[Groovy Console]を選択します。
- [Ctrl]+[R]キーで実行することができます。
Groovy Scriptから実行する
- 右クリックで表示されるコンテキストメニューから、[実行]->[Groovy Script]を選択します。
- Eclipseのコンソールに実行結果として、
Hello Groovy!!
が表示されます。
Java アプリケーションとして実行する
- 右クリックで表示されるコンテキストメニューから、[実行]->[Java アプリケーション]を選択します。
- Eclipseのコンソールに実行結果として、
Hello Groovy!!
が表示されます。