• 追加された行はこの色です。
  • 削除された行はこの色です。
---
keywords: [Gradle]
description: 汎用ビルドツールであるGradleの使い方についてメモしています。
author: aterai
pubdate: 2016-12-13T13:37:28+09:00
---
#contents

* 概要 [#about]

* インストール [#install]
- `Windows` + `Cygwin`の環境で`SDKMAN`から`Gradle`をインストール
- `Cygwin`
-- `setup-x86_64.exe`などから、`crul`、`unzip`、`zip`をインストール
- `SDKMAN`: `Cygwin terminal`などから、以下を実行してインストールし、ターミナルを再起動
 curl -s "https://get.sdkman.io" | bash
- `Gradle`: 
 sdk install gradle

* サンプル [#example]
- `build.gradle`

#code{{
description 'Java Swing Tips'

allprojects {
  apply {
    plugin 'java'
    plugin 'application'
  }

  def defaultEncoding = 'UTF-8'
  [compileJava, compileTestJava]*.options*.encoding = defaultEncoding

  repositories {
    jcenter()
  }
}

subprojects {
  sourceSets {
    main {
      java { srcDir 'src/java' }
      resources { srcDir 'src/java' }
    }
    test {
      java { srcDir 'src/test' }
    }
  }

  def config = new ConfigSlurper().parse(new File("${project.projectDir}/config.gradle").toURL())

  mainClassName = config.main.class

  // In this section you declare where to find the dependencies of your project
  repositories {
    flatDir { dirs "${project.projectDir}/lib" }
  }

  // In this section you declare the dependencies for your production and test code
  dependencies {
    compile fileTree(dir: "${project.projectDir}/lib", include: '*.jar')
  }

  version = config.version
  jar {
    manifest {
      attributes 'Implementation-Version': config.version,
                 'Implementation-Vendor': config.vendor,
                 'Main-Class': config.main.class,
                 'Class-Path': config.runtime.classpath
    }
  }

  apply {
    plugin 'pmd'
    plugin 'checkstyle'
    plugin 'findbugs'
  }
  pmd {
    ignoreFailures = true
    consoleOutput = true
    ruleSetFiles = files("${rootProject.projectDir}/ruleset.xml")
    ruleSets = []
  }
  checkstyleMain {
    ignoreFailures = true
    configFile = file("${rootProject.projectDir}/checkstyle.xml")
  }
  findbugs {
    ignoreFailures = true
    excludeFilter = file("${rootProject.projectDir}/excludefilter.xml")
  }
}
}}

- `settings.gradle`

#code{{
//サブプロジェクトとしてAccordionPanel~ZoomingAndPanningを追加
//700ほど追加すると重い...
file('.').eachDirMatch(~/[a-zA-Z]+/) {
    include it.name
}
}}

- `AccordionPanel`~`ZoomingAndPanning`以下に`config.gradle`

#code{{
vendor  = 'Java Swing Tips - http://ateraimemo.com/'
version = '1.0.0'
year    = '2003-2016'

main.class = 'example.MainPanel'
runtime.classpath = '.'
}}

* 実行方法 [#excute]
- すべてのサブプロジェクトを実行

 $ gradle clean
 :clean UP-TO-DATE
 :AccordionPanel:clean
 :ActionMap:clean
 ... 中略 ...
 :ZoomAndPanPanel:clean UP-TO-DATE
 :ZoomingAndPanning:clean UP-TO-DATE
 
 BUILD SUCCESSFUL
 
 Total time: 8.747 secs

`Ant`で同様に`clean`タスクを実行すると`5`秒程度

 $ ant -f all.xml -Dall=clean
 Buildfile: C:\tmp\jst\all.xml
 all:
 clean:
 clean:
    [delete] Deleting directory C:\tmp\jst\AccordionPanel\target
 ... 中略 ...
 clean:
    [delete] Deleting directory C:\tmp\jst\ZoomingAndPanning\target
 
 BUILD SUCCESSFUL
 Total time: 5 seconds

- サブプロジェクトを指定して実行

 $ gradle :ZoomingAndPanning:check
 :ZoomingAndPanning:compileJava UP-TO-DATE
 :ZoomingAndPanning:processResources UP-TO-DATE
 :ZoomingAndPanning:classes UP-TO-DATE
 :ZoomingAndPanning:checkstyleMain
 :ZoomingAndPanning:compileTestJava UP-TO-DATE
 :ZoomingAndPanning:processTestResources UP-TO-DATE
 :ZoomingAndPanning:testClasses UP-TO-DATE
 :ZoomingAndPanning:checkstyleTest UP-TO-DATE
 :ZoomingAndPanning:findbugsMain
 :ZoomingAndPanning:findbugsTest UP-TO-DATE
 :ZoomingAndPanning:pmdMain
 :ZoomingAndPanning:pmdTest UP-TO-DATE
 :ZoomingAndPanning:test UP-TO-DATE
 :ZoomingAndPanning:check
 
 BUILD SUCCESSFUL
 
 Total time: 17.056 secs

* 参考リンク [#reference]
- [http://gradle.monochromeroad.com/docs/userguide/userguide.html Gradle User Guide]
- [http://d.hatena.ne.jp/masanobuimai/20120528/1338213342 Gradleなんとなくわかってきた(その2) - marsのメモ]
- [http://www.ne.jp/asahi/hishidama/home/tech/groovy/gradle/index.html Gradleメモ(Hishidama's Gradle Memo)]

* コメント [#comment]
#comment
#comment