Gradle のバックアップ(No.8)
- バックアップ一覧
 - 差分 を表示
 - 現在との差分 を表示
 - 現在との差分 - Visual を表示
 - ソース を表示
 - Gradle へ行く。
  
- 1 (2016-12-13 (火) 13:49:59)
 - 2 (2016-12-13 (火) 16:17:16)
 - 3 (2016-12-13 (火) 21:05:30)
 - 4 (2017-10-27 (金) 16:26:13)
 - 5 (2019-01-18 (金) 17:10:36)
 - 6 (2019-04-19 (金) 13:43:27)
 - 7 (2019-11-14 (木) 16:41:08)
 - 8 (2023-03-21 (火) 16:51:50)
 - 9 (2025-01-03 (金) 08:57:02)
 - 10 (2025-01-03 (金) 09:03:21)
 - 11 (2025-01-03 (金) 09:04:02)
 
 
- keywords: [Gradle] description: 汎用ビルドツールであるGradleの使い方についてメモしています。 author: aterai pubdate: 2016-12-13T13:37:28+09:00
 
概要
インストール
Windows+Cygwinの環境でSDKMANからGradleをインストールCygwinsetup-x86_64.exeなどから、curl、unzip、zipをインストール
SDKMAN:Cygwin terminalなどから、以下を実行してインストールし、ターミナルを再起動curl -s "https://get.sdkman.io" | bash
Gradle:sdk install gradle
サンプル
aterai/java-swing-tipsのような、大量(AccordionPanel~ZoomingAndPanningで700ほど)のサブプロジェクトが存在する場合のサンプルです。
build.gradle
description 'Java Swing Tips'
allprojects {
  apply {
    plugin 'java'
    plugin 'application'
    plugin 'pmd'
    plugin 'checkstyle'
  }
  repositories {
    mavenCentral()
    jcenter()
  }
  def defaultEncoding = 'UTF-8'
  [compileJava, compileTestJava]*.options*.encoding = defaultEncoding
  pmd {
    consoleOutput = true
    ignoreFailures = true
    incrementalAnalysis = true
    ruleSetFiles = files("${rootProject.projectDir}/ruleset.xml")
    ruleSets = [] // ruleSetFiles のみを使用する場合は ruleSets を空にする必要がある
  }
  checkstyle {
    ignoreFailures = true
    configFile = file("${rootProject.projectDir}/checks.xml")
  }
}
subprojects {
  sourceSets {
    main {
      //デフォルトプロジェクトレイアウトでは src/main/java
      java { srcDir 'src/java' }
      //デフォルトプロジェクトレイアウトでは src/main/resources
      resources { srcDir 'src/java' }
    }
  }
  ext.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 {
    implementation 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
    }
  }
}
settings.gradle
// サブプロジェクトとしてAccordionPanel~ZoomingAndPanningを追加
// 700ほど追加すると重い...
file('.').eachDirMatch(~/[a-zA-Z]+/) {
  include it.name
}
AccordionPanel~ZoomingAndPanning以下にconfig.gradle
vendor = 'Java Swing Tips - https://ateraimemo.com/'
version '1.0-SNAPSHOT'
year = '2003-2019'
main.class = 'example.MainPanel'
runtime.classpath = '.'
実行方法
- すべてのサブプロジェクトを実行
 
$ 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
- サブプロジェクトディレクトリを指定して実行
 
$ gradle clean run -q --project-dir ZoomingAndPanning/