Gradle を使ってて、 build.gradle の設定で困ったこととか、色々調べたこととかがあれば、随時メモしていく。 と思う。
プログラムのインストールディレクトリを参照する
build.gradle
apply plugin: 'application'
mainClassName = 'sample.Main'
startScripts.with {
doLast {
unixScript.text = unixScript.text.replaceFirst('(?<=DEFAULT_JVM_OPTS=)((\'|\")(.*)(\'|"))(?=\n)',
'\'$3 "-Dapp.home=\\$APP_HOME"\'')
windowsScript.text = windowsScript.text.replaceFirst('(?<=DEFAULT_JVM_OPTS=)(.*)(?=\r\n)',
'$1 "-Dapp.home=%~dp0.."')
}
}
Main.java
package sample;
public class Main {
public static void main(String... args) {
System.out.println("os = " + System.getProperty("os.name"));
System.out.println("app.home = " + System.getProperty("app.home"));
}
}
実行結果(Windows)
os = Windows 7
app.home = f:\tmp\gradle\test\build\install\test\bin\..
実行結果(Linux)
os = Linux
app.home = /vagrant/gradle
Eclipse まわり
プロジェクトの文字コード設定
eclipseJdt << {
File f = file('.settings/org.eclipse.core.resources.prefs')
f.write('eclipse.preferences.version=1\n')
f.append('encoding/<project>=utf-8')
}
making eclipse wtp project with gradle - Stack Overflow
外部ライブラリを追加する
eclipse {
classpath {
containers 'GROOVY_DSL_SUPPORT', 'GROOVY_SUPPORT'
}
}
例は、 Groovy を追加した場合。
参考
TypeScript プラグインをプロジェクト設定で有効にする
これ をプロジェクト設定で有効になるようにする。
eclipse {
project {
buildCommand 'com.palantir.typescript.typeScriptBuilder'
natures 'com.palantir.typescript.typeScriptNature'
}
}
tasks.eclipse << {
println file('.settings/com.palantir.typescript.prefs').withWriter {writer ->
writer << """\
|build.path.sourceFolder=src/main/ts
|compiler.outputDirOption=bin/ts
|compiler.outputFileOption=
|eclipse.preferences.version=1
|""".stripMargin()
}
}
参考
- Slim3+JPP+Eclipse 用のbuild.gradleを頑張って書いた…。今んとここれで多分いいに違いない。誰か詳しい人、もっとかっこよくしてくりー〜〜〜
- gradle-user - Best Practices on extending Eclipse file generation.
ソースフォルダのデフォルトの出力先を変更する
eclipse {
classpath {
defaultOutputDir = file('path/to/output')
}
}
参考
ソースフォルダごとに、コンパイル結果の出力先を変更する
import org.gradle.plugins.ide.eclipse.model.SourceFolder
eclipse {
classpath {
file.whenMerged {cp ->
cp.entries.findAll { it instanceof SourceFolder && it.path.startsWith("src/test/") }*.output = "path/to/output"
}
}
}
参考