Gradle 0.9.x から Idea プラグインがそれなりに動くようになっていじってみてるんですけど、依存性 (dependencies) にライブラリを追加して IDEA の設定ファイルを生成し直す(「gradle idea」コマンド実行)と IDEA の設定があれこれリセットされてしまうようなので、頑張ってビルドファイル (build.gradle) から設定を行うようにしてみました(一覧)。
ついでにソースコードのエンコーディングの指定や実行可能 Jar ファイルを生成するための設定などもしてます。 もっと簡単な設定の仕方があったり、そのうち必要になくなる設定もあるかも知れませんがご了承を。
参考 URL
- Gradle User Guide「Chapter 28. The Idea Plugin」
- 「Gradleで文字エンコーディングを指定する方法」
- 「Object Design Blog ― desktop publishing hacks and groovy. ― 」
- 「Creating a fat jar」
初めに
以下で「Java プロジェクト」バージョンと「Groovy プロジェクト」バージョンの build.gradle を載せてますが、どちらも
gradle
で実行可能 Jar ファイルを生成できます(もしくは assemble タスクを実行してネ)。 生成される場所は
- $PROJECT_HOME/build/libs
フォルダです。 また、実行の起点となるメインクラスを変更したい場合は、jar.manifest.attributes ノードの「org.sample.hello.Main」の部分を変更してください。
Java プロジェクト
まずは Java プロジェクトの build.gradle:
apply plugin:'java' apply plugin:'idea' defaultTasks 'clean', 'assemble' version = '1.0' def jdkVersion = 1.6 def enc = 'UTF-8' sourceCompatibility = jdkVersion targetCompatibility = jdkVersion tasks.withType(Compile){ options.encoding = enc } // ***** Settings for dependencies ***** repositories { mavenCentral() } dependencies { // 依存性を書く // compile 'org.apache.commons:commons-math:2.2' } // compile スコープのライブラリを runtime スコープにも展開(いらないみたい) //configurations{ // runtime { extendsFrom compile } //} // ***** Settings for Executable Jar ***** jar { manifest.attributes 'Main-Class':'org.sample.hello.Main' from configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) } } // ***** Settings for IntelliJ IDEA ***** ideaModule{ outputDir = compileJava.destinationDir testOutputDir = compileTestJava.destinationDir downloadSources = false } ideaProject{ javaVersion = jdkVersion }
- 「ideaModule.downloadSources = false」でライブラリのソースコードをダウンロードするのを抑制しています。 ソースコードも必要なら削除して下さい。
- Gradle の Idea プラグインのドキュメントを見る限り「ideaProject.javaVersion」の設定はデフォルトで 1.6 になってるはずなんですが、どうもそのようには設定されないようなので明示的に設定しています。 そのうち必要なくなるかと。
Groovy プロジェクト
次は Groovy プロジェクトの build.gradle。 Java プロジェクトの build.gradle に
- 「apply plugin:'groovy'」
- エンコーディングの設定
- dependncies.groovy ノード
を追加しています。 エンコーディングの設定以外は問題なしかと。
apply plugin:'groovy' apply plugin:'idea' defaultTasks 'clean', 'assemble' version = '1.0' def jdkVersion = 1.6 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:1.7.8' // compile 'org.apache.commons:commons-math:2.2' } // ***** Settings for Executable Jar ***** jar { manifest.attributes 'Main-Class':'org.sample.hello.Main' from configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) } } // ***** Settings for IntelliJ IDEA ***** ideaModule{ outputDir = compileJava.destinationDir testOutputDir = compileTestJava.destinationDir downloadSources = false } ideaProject{ javaVersion = jdkVersion }
エンコーディングの設定は
compileJava.options.encoding | src/main/java/**.java |
compileGroovy.options.encoding | src/main/groovy/**.java |
compileGroovy.groovyOptions.encoding | src/main/groovy/**.groovy |
compileTestJava.options.encoding | src/test/java/**.java |
compileTestGroovy.options.encoding | src/test/groovy/**.java |
compileTestGroovy.groovyOptions.encoding | src/test/groovy/**.groovy |
だそうです ^ ^;)
追記
「gradle idea」コマンドで IntelliJ IDEA の「Gradle home」がリセットされてしまうのは修正できませんでした・・・ はてさてどこから設定すればいいものか。
- 作者: Duane K. Fields,Stephen Saunders,Eugene Belyaev
- 出版社/メーカー: Manning Pubns Co
- 発売日: 2006/04/30
- メディア: ペーパーバック
- 購入: 1人 クリック: 16回
- この商品を含むブログ (10件) を見る