ギョームで Java を使うことになったので、 Gradle の復習をしていく。
環境構築
Chocolatey なんて使わせてもらえないから手動構築を行う。 手順は Gradle | Installation 参照。
バイナリの入手
Binary-Only のリンクからファイルをダウンロード。 (2017/3/16 時点では、gradle-3.4.1-bin.zip がダウンロードされる)
展開してパスを通す
- %USERPROFILE%\develop\ディレクトリを作成
- 「1.」で作成したディレクトリで gradle-3.4.1-bin.zipを展開し、できたディレクトリをgradle-3.4.1にリネーム- %USERPROFILE%\develop\gradle-3.4.1というディレクトリができる
 
- %USERPROFILE%\develop\gradle-3.4.1\binにパスを通す
- 環境変数 JAVA_HOMEを設定する
動作確認
> cd %USERPROFILE%\project\
> mkdir GradleTest
> cd GradleTest
> gradle init --type java-applicationこれでプロジェクトのひな型ができたので、早速ビルドする。
> gradlew.bat build本来は gradlew.bat を使う必要はない(すでに環境構築してあるホストだから)けど、配布された側になったつもりで gradlew.bat を使う。
gradle や、依存ライブラリをダウンロードしたうえでビルドしてくれる。
>gradlew.bat run
:compileJava UP-TO-DATE
:processResources NO-SOURCE
:classes UP-TO-DATE
:run
Hello world.大丈夫そう。
設定ファイル作成
- eclipse で実装
- JUnit でテスト
- findbugs 使う(html レポート)
- jacoco 使う(html レポート)
- checkstyle 使う(html レポート)
- build するだけで findbugs, jacoco, checkstyle のレポートを出力する
- lombok 使う
- args4j 使う
あたりの設定を追加。
/*
 * This build file was generated by the Gradle 'init' task.
 *
 * This generated file contains a sample Java project to get you started.
 * For more details take a look at the Java Quickstart chapter in the Gradle
 * user guide available at https://docs.gradle.org/3.4.1/userguide/tutorial_java_projects.html
 */
// Apply the java plugin to add support for Java
apply plugin: 'java'
// Apply the application plugin to add support for building an application
apply plugin: 'application'
apply {
    plugin 'eclipse'
    plugin 'checkstyle'
    plugin 'findbugs'
    plugin 'jacoco'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
version = 1.0
[compileJava, compileTestJava]*.options*.encoding = "UTF-8"
// In this section you declare where to find the dependencies of your project
repositories {
    // Use jcenter for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
}
[checkstyleMain, checkstyleTest, findbugsMain, findbugsTest]*.ignoreFailures = true
configurations {
    provided
}
sourceSets {
    main {
        compileClasspath += configurations.provided
    }
}
eclipse.classpath {
    plusConfigurations += [ configurations.provided ]
    minusConfigurations += [ configurations.provided ]
}
dependencies {
    // This dependency is found on compile classpath of this component and consumers.
    compile 'com.google.guava:guava:20.0'
    // Use JUnit test framework
    testCompile 'junit:junit:4.12'
    testCompile "org.hamcrest:hamcrest-all:1.3"
    compile 'args4j:args4j:2.0.16'
    provided 'org.projectlombok:lombok:1.16.4'
}
// Define the main class for the application
mainClassName = 'App'
test {
    doLast {
        tasks.jacocoTestReport.execute()
    }
}
jacocoTestReport {
    reports {
        xml.enabled = false
        html.enabled = true
        html.destination = "${buildDir}/reports/jacoco"
    }
}
tasks.withType(FindBugs) {
    reports {
        xml.enabled = false
        html.enabled = true
    }
}
tasks.withType(Checkstyle) {
    reports {
        xml.enabled false
        html.enabled true
        html.destination = "${buildDir}/reports/checkstyle/checkstyle.html"
    }
}
jar {
    manifest {
        attributes 'Implementation-Title': 'Test App.'
        attributes 'Implementation-Version': 1.0
        attributes "Main-Class" : "App"
    }
    from configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}動くか試す。
> gradlew.bat buildうん、一通りレポート出力されているっぽい。
とりあえずこんなものでいいか。
