Java 7 がリリースされて数ヶ月が経ってるので今更ですが、Java 7 を使って開発を行うプロジェクトの環境設定をしてみます。 プロジェクトは Gradle (バージョンは 1.0-milestone-0.6) でビルドでき、IntelliJ IDEA で開発できるようにします*1。 また、Groovy も使えるようにしましょう。 Java 7 を使うなら、まだベータ版しかリリースされてませんが Groovy のバージョンは2.0以降にした方がよさそうなので、そうします*2。
という前置きのもと、Gradle の build.gradle ファイルを見ていこう!ってだけです。 ちなみに、この記事は「IntelliJ IDEA で実行可能 Jar ファイルを作成するプロジェクトの Gradle ビルドファイル 〜エンコーディングも指定するヨ〜 feat. JDK 1.6」の Java 7 版ってだけです。 この1.6版の build.gradle の設定では(Gradle のバージョンによって) IntelliJ IDEA の設定が上手く反映されないので記事を書き直した次第です。
では build.gradle の内容を見てみましょう:
apply plugin:'groovy' apply plugin:'idea' defaultTasks 'cleanIdea', 'idea' // IDEA の設定ファイルを生成 //defaultTasks 'test' // テストを実行 //defaultTasks 'clean', 'build' // クリーン&ビルド group = '《グループ名》' version = '《成果物バージョン》' def jdkVersion = 1.7 // Java 7 を使おう! def enc = 'UTF-8' // エンコーディング指定 sourceCompatibility = jdkVersion targetCompatibility = jdkVersion tasks.withType(Compile){ options.encoding = enc } // ***** Settings for dependencies ***** repositories { mavenCentral() } dependencies { groovy 'org.codehaus.groovy:groovy-all:2.0.0-beta-1' compile 'org.codehaus.gpars:gpars:0.11' testCompile 'junit:junit:4.10' } // ***** Settings for Executable Jar ***** jar { manifest.attributes 'Main-Class':'《Jar 実行の際のメインクラス》', 'Implementation-Version': version //from configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) } // 依存しているライブラリをすべてまとめて1つの Jar ファイルにするならコメントはずす } // ***** Settings for IntelliJ IDEA ***** idea{ project{ // デフォルトは Java 6 (1.6) なので設定が必要 jdkName = jdkVersion languageLevel = jdkVersion } // こちらはお好みに合わせて。 module{ outputDir = compileJava.destinationDir testOutputDir = compileTestJava.destinationDir downloadJavadoc = true downloadSources = false } }
- idea ノード以外は大して変わってません。 dependencies ノードに、テストに必要な JUnit ライブラリを加えたくらいでしょうか*3。
- idea ノードの project ノードでは JDK のバージョンを設定しています。 (Gradle の) idea プラグインではデフォルトで JDK のバージョンが1.6に設定されるので、Java 7 を使うなら設定必須です。
- idea ノードの module ノードの設定はお好みに合わせて。
ちなみに、ビルドで生成した成果物をどこかの Maven リポジトリにアップロードしたい場合は以下のようにします:
apply plugin:'groovy' apply plugin:'idea' apply plugin:'maven' // 成果物をリポジトリにアップロードするなら必要 defaultTasks 'cleanIdea', 'idea' //defaultTasks 'test' //defaultTasks 'clean', 'build', 'uploadArchives' // 成果物を生成してアップロード group = '《グループ名》' version = '《成果物バージョン》' def jdkVersion = 1.7 def enc = 'UTF-8' sourceCompatibility = jdkVersion targetCompatibility = jdkVersion tasks.withType(Compile){ options.encoding = enc } // ***** Settings for dependencies ***** repositories { mavenCentral() flatDir(name: 'local', dirs: "$projectDir/repo") // ローカル・ディレクトリをリポジトリに設定 } dependencies { groovy 'org.codehaus.groovy:groovy-all:2.0.0-beta-1' compile 'org.codehaus.gpars:gpars:0.11' testCompile 'junit:junit:4.10' } // ***** Settings for Executable Jar ***** jar { manifest.attributes 'Main-Class':'《Jar 実行の際のメインクラス》', 'Implementation-Version': version //from configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) } } uploadArchives { repositories{ add project.repositories.local // repositories ノードに定義されているリポジトリにアップロード mavenDeployer { repository(url: '《リポジトリの URL》') // 指定した URL のリポジトリにアップロード } } } // ***** Settings for IntelliJ IDEA ***** idea{ project{ jdkName = jdkVersion languageLevel = jdkVersion } module{ outputDir = compileJava.destinationDir testOutputDir = compileTestJava.destinationDir downloadJavadoc = true downloadSources = false } }
- Maven リポジトリにアップロードするには maven プラグインを使います。
- リポジトリの設定は repositries ノードと uploadAchives ノードで行います(uploadAchives ノードだけでも可)。
まぁ、スタンダードなプロジェクトの設定はこんな感じでいいんじゃないでしょうか。

- 作者: 関谷和愛,上原潤二,須江信洋,中野靖治
- 出版社/メーカー: 技術評論社
- 発売日: 2011/07/06
- メディア: 単行本(ソフトカバー)
- 購入: 6人 クリック: 392回
- この商品を含むブログ (152件) を見る

Building and Testing With Gradle
- 作者: Tim Berglund,Matthew Mccullough,Hans Dockter
- 出版社/メーカー: Oreilly & Associates Inc
- 発売日: 2011/07/13
- メディア: ペーパーバック
- クリック: 7回
- この商品を含むブログ (7件) を見る