LoginSignup
4
3

More than 5 years have passed since last update.

Eclipse 4.8 で始める Groovy 2.4 ~準備編~

Last updated at Posted at 2018-07-29

環境構築

Eclipseのダウンロードとインストール

Pleiades All in One Eclipse 4.8 Photon のページから、Java用のFull Editionをダウンロードします。

Windowsの場合は、zip解凍時の注意を必ず参照の事

Groovyプラグインのインストール

Eclipseを起動し、[ヘルプ]->[新規ソフトウエアのインストール...]から

  1. [作業対象] に http://dist.springsource.org/release/GRECLIPSE/e4.8 を入力し、[追加...]します
  2. [Main Package (必須)]を選択してインストールします
    • Groovy 2.5 を使用する場合は、[More Compilers(オプション)] -> [Groovy Compiler 2.5] にもチェックを付けてインストールします
  3. Eclipseの再起動が求められるので、再起動します

※ 2018/08/07 追記 Groovy-Eclipse 3.0.0がリリースされましたので、Update-SiteのURLを変更しました。

Gradleプロジェクトの作成

  1. [ファイル]->[新規]->[Gradleプロジェクト] を選択します
  2. 適当な名前を付けてプロジェクトを作成します

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'
}

ソース・フォルダーの作成

  1. src/mainの下にgroovyフォルダーを作成します。
  2. プロジェクトを右クリックし、[Gradle]->[Gradleプロジェクトのリフレッシュ]を選択します。

初めてのGroovyプログラムを作成する

  1. [ファイル]->[新規]->[その他...]を選択します。
  2. [Groovy]->[Groovy クラス]を選択し、[次へ]をクリックします。
  3. [名前]にFirstGroovy と入力し、[完了]をクリックするとファイルが作成されます。
  4. 次のように編集し、保存します。
package sample

class FirstGroovy {

    static void main(args) {
        println "Hello Groovy!!"
    }
}

Groovyプログラムの実行

作成したプログラムは、次の3つの方法で実行することができます。

Groovy Consoleから実行する

  1. 右クリックで表示されるコンテキストメニューから、[実行]->[Groovy Console]を選択します。
  2. [Ctrl]+[R]キーで実行することができます。

Groovy Scriptから実行する

  1. 右クリックで表示されるコンテキストメニューから、[実行]->[Groovy Script]を選択します。
  2. Eclipseのコンソールに実行結果として、Hello Groovy!!が表示されます。

Java アプリケーションとして実行する

  1. 右クリックで表示されるコンテキストメニューから、[実行]->[Java アプリケーション]を選択します。
  2. Eclipseのコンソールに実行結果として、Hello Groovy!!が表示されます。
4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3