LoginSignup
10
9

More than 5 years have passed since last update.

build.gradleの設定で困ったこととかのメモ

Last updated at Posted at 2014-12-07

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

java - How to pass a reference to distribution home directory using Gradle application plugin? - Stack Overflow

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()
    }
}

参考

ソースフォルダのデフォルトの出力先を変更する

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

参考

10
9
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
10
9