Doma 2をEclipse 4.4.1+Gradle 2.2で使うための環境設定・WTPプロジェクト作成・Doma利用設定まで
このブログについて
Java Advent Calendar 2014 - Qiitaで12月10日にDomaの記事を書くのですが、環境設定などが長くなったため別記事としたのが、この記事です。
Doma 2でWebアプリを作るための下準備の方法を紹介します。
DomaおよびGradleに関しては初心者ですので、何か間違いがあったらすいません。
環境
JDK 8u25
Eclipse for Java EE Developers 4.4.1(Pleiadesで日本語化済み)
Gradle 2.2
GlassFish 4.1(Tomcatなど、別のAPサーバーでも大丈夫です)
Doma 2.0.1
Doma-Gen 2.0.1
PostgreSQL 8.4.17(古くてすいません(^^;)
JDK 8u25+Eclipse 4.4.1のインストール
省略します。
ちなみに、NetBeansラブな自分が今回Eclipseを使った理由は、NetBeansでのGradleの使い方がよく分からなかったことと、DomaプラグインがNetBeansには無かったことです。
Gradle 2.2のインストール
下記のサイトからgradle-2.2-all.zipをダウンロードします。
Download Gradle l Open Source Enterprise Build Automation
僕が使ったのは2.2ですが、2.2.1でも大丈夫だと思います。
このZIPファイルをCドライブ直下に展開すると、「C:\gradle-2.2」というフォルダが出来ます。
環境変数に
GRADLE_HOME→C:\gradle-2.2
PATH→%GRADLE_HOME%\bin
を追加します。
そして、コマンドプロンプトを起動して「gradle -v」と実行し、下記のようになればOKです。
EclipseのGradleプラグインのインストール
Eclipseを起動し、[ヘルプ]-[Eclipseマーケットプレイス]と選択します。
検索バーに「gradle」と入力して、[Gradle IDE Pack]をインストールします。
インストール後、Eclipseは再起動します。
EclipseのDomaプラグインのインストール
[ヘルプ]-[新規ソフトウェアのインストール]と選択します。
[作業対象]に「http://dl.bintray.com/domaframework/eclipse/」と入力し、出てきたものをインストールします。
ちなみに、IntelliJ IDEAにもDomaプラグインがあります。
Gradleプロジェクトの作成
[ファイル]-[新規]-[プロジェクト]と選択し、[Gradle]-[Gradleプロジェクト]を作成します。
プロジェクト名に任意の名前を入力し、[Java Quickstart]を選択して完了します。
Gradleプロジェクトが作成されました。
プロジェクトの設定変更
デフォルトでは、プロジェクトのコンパイラ・レベルが1.5になっているので、変更します。
プロジェクトを右クリックして[プロパティー]→[Javaコンパイラー]の[コンパイラー準拠レベル]を[1.8]に変更します。
[OK]すると「再ビルドが必要です」と聞かれるので、OKしてください。
WTPプロジェクトへの変換
src/main直下に、webappフォルダを作成します。
プロジェクト直下にあるbuild.gradleを、下記のように編集します。
apply plugin: 'java' apply plugin: 'eclipse-wtp' apply plugin: 'war' sourceCompatibility = 1.8 version = '1.0' jar { manifest { attributes 'Implementation-Title': 'Gradle Quickstart', 'Implementation-Version': version } } processResources.destinationDir = compileJava.destinationDir compileJava.dependsOn processResources compileJava { options.encoding = 'UTF-8' } compileTestJava { options.encoding = 'UTF-8' } task wrapper(type: Wrapper) { gradleVersion = '2.2' } eclipse { // Dynamic Web Project wtp { component { // eclipse wtp context_path contextPath = '/Sample' } facet { facet name: 'java', version: '1.8' facet name: 'jst.web', version: '3.1' } } } configurations { domaGenRuntime } repositories { mavenCentral() } dependencies { domaGenRuntime 'org.seasar.doma:doma-gen:2.0.1' domaGenRuntime 'org.postgresql:postgresql:9.3-1102-jdbc41' compile 'org.seasar.doma:doma:2.0.1' compile 'org.seasar.doma:doma-gen:2.0.1' compile 'org.postgresql:postgresql:9.3-1102-jdbc41' } test { systemProperties 'property': 'value' } task gen << { ant.taskdef(resource: 'domagentask.properties', classpath: configurations.domaGenRuntime.asPath) ant.gen(url: 'jdbc:postgresql://localhost/postgres', user: 'postgres', password: 'P@ssw0rd') { entityConfig(packageName: 'com.tada.doma2.entity') daoConfig(packageName: 'com.tada.doma2.dao', configClassName: 'com.tada.doma2.dao.AppConfig') sqlConfig() } } apply from:'eclipse.gradle'
プロジェクト直下にeclipse.gradleを作成し、下記のように記述します。
apply plugin: 'eclipse-wtp' ext { aptDir='.apt_generated' } eclipse { jdt.file.withProperties { it['org.eclipse.jdt.core.compiler.processAnnotations'] = 'enabled' } classpath { containers = ['org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8'] } } eclipseJdt { def clos = [] def fp = file('.factorypath') outputs.file fp clos += { fp.withWriter { def domaJar = configurations.compile.find { it.name.startsWith('doma-2') } new groovy.xml.MarkupBuilder(it).factorypath() { factorypathentry(kind:'EXTJAR', id:domaJar, enabled:true, runInBatchMode:false) } } } def prefs = { name, contents -> def f = file(".settings/$name") clos += { f.text = contents.stripMargin() } outputs.file f } prefs 'org.eclipse.jdt.apt.core.prefs', """\ |eclipse.preferences.version=1 |org.eclipse.jdt.apt.aptEnabled=true |org.eclipse.jdt.apt.genSrcDir=${aptDir} |org.eclipse.jdt.apt.reconcileEnabled=true |""" doLast { clos*.run() } } task eclipseSettings(type: Copy) { from rootProject.files("eclipse/org.eclipse.jdt.ui.prefs") into project.file('.settings/') outputs.upToDateWhen { false } } task eclipseJdtPrepare(type: Copy) { from rootProject.file("eclipse/org.eclipse.jdt.core.prefs") into project.file(".settings/") outputs.upToDateWhen { false } } task cleanEclipseJdtUi(type: Delete) { delete project.file(".settings/org.eclipse.jdt.ui.prefs") delete project.file(".settings/org.eclipse.jdt.core.prefs") } tasks["eclipseJdt"].dependsOn(eclipseJdtPrepare) tasks["cleanEclipse"].dependsOn(cleanEclipseJdtUi) tasks["eclipse"].dependsOn(eclipseSettings)
では、プロジェクトを変換します。
build.gradleを右クリック→[Gradle]-[すべてリフレッシュ]を選択します。
すると処理が実行され、プロジェクトがWTPプロジェクトに変換されます。
src/main/java、src/main/resources、src/test/java、src/test/resourcesの各フォルダには、自動生成されたorg.gradleパッケージがあるので、削除しておきます。
プロジェクトのアイコンを見ると、WTPプロジェクトになっていることが分かります。(地球のアイコンが左上に付いています)
また、[参照ライブラリー]の中に、DomaなどのJARファイルが追加されています。
はじめての実行の際は、依存ライブラリのダウンロードが走るので、けっこう時間がかかります。
APサーバーの選択
EclipseにはGlassFishプラグインを追加しておいてください。
GlassFish 4さわる - kagamihogeの日記
プロジェクトのプロパティー→[サーバー]および[ターゲット・ランタイム]でGlassFish 4.1を選択します。