Gradle init 7.0¶
JDKとGradleにPATHを通した状態で gradle init を実行します。
対話的にいくつかの設定を指示すると、プロジェクトのディレクトリ・ファイルが生成されます。
以下はGradle 7.0で実行したときの例です。
D:\work\HelloApp> gradle init Welcome to Gradle 7.0! Here are the highlights of this release: - File system watching enabled by default - Support for running with and building Java 16 projects - Native support for Apple Silicon processors - Dependency catalog feature preview For more details see https://docs.gradle.org/7.0/release-notes.html Starting a Gradle Daemon (subsequent builds will be faster) Select type of project to generate: 1: basic 2: application 3: library 4: Gradle plugin Enter selection (default: basic) [1..4] 2 <--- Java実行可能プロジェクト Select implementation language: 1: C++ 2: Groovy 3: Java 4: Kotlin 5: Scala 6: Swift Enter selection (default: Java) [1..5] 3 <--- Java Select build script DSL: 1: Groovy 2: Kotlin Enter selection (default: Groovy) [1..2] 1 <--- build.gradleの記述はGroovyで Split functionality across multiple subprojects?: 1: no - only one application project 2: yes - application and library projects Enter selection (default: no - only one application project) [1..2] 1 <-- 単一のプロジェクトとする Select build script DSL: 1: Groovy 2: Kotlin Enter selection (default: Groovy) [1..2] 1 <-- 今回はGroovyで Select test framework: 1: JUnit 4 2: TestNG 3: Spock 4: JUnit Jupiter Enter selection (default: JUnit 4) [1..4] 4 <--- JUnit5 を使う Project name (default: HelloApp): <--- ディレクトリ名と同じでよければ[Enter]キー入力 Source package (default: HelloApp): com.torutk.hello <--- 作成するプログラムのパッケージ名を入力 > Task :init Get more help with your project: https://docs.gradle.org/6.4/userguide/tutorial_java_projects.html BUILD SUCCESSFUL in 7m 47s 2 actionable tasks: 2 executed D:\work\HelloApp>
Gradleのinitタスクを実行すると、対話的にプロジェクトの種類や設定を入力し、それに従ったプロジェクトディレクトリ・ファイルが生成されます。このディレクトリ構造は次となります。
D:\work\HelloApp
│ .gitattributes
│ .gitignore
│ gradlew
│ gradlew.bat
│ settings.gradle
│
├─.gradle
│ ├─7.0
│ │ │ gc.properties
│ │ │
│ │ ├─dependencies-accessors
│ │ │ dependencies-accessors.lock
│ │ │ gc.properties
│ │ │
│ │ ├─executionHistory
│ │ │ executionHistory.bin
│ │ │ executionHistory.lock
│ │ │
│ │ ├─fileChanges
│ │ │ last-build.bin
│ │ │
│ │ ├─fileHashes
│ │ │ fileHashes.bin
│ │ │ fileHashes.lock
│ │ │
│ │ └─vcsMetadata-1
│ ├─buildOutputCleanup
│ │ buildOutputCleanup.lock
│ │ cache.properties
│ │ outputFiles.bin
│ │
│ ├─checksums
│ │ checksums.lock
│ │
│ ├─configuration-cache
│ │ gc.properties
│ │
│ └─vcs-1
│ gc.properties
│
├─gradle
│ └─wrapper
│ gradle-wrapper.jar
│ gradle-wrapper.properties
│
└─app
└─src
│ build.gradle
│
├─main
│ ├─java
│ │ └─com
│ │ └─torutk
│ │ └─hello
│ │ App.java
│ │
│ └─resources
└─test
├─java
│ └─com
│ └─torutk
│ └─hello
│ AppTest.java
│
└─resources
build.gradle¶
ビルドの定義はbuild.gradleファイルに記述します。initタスク(プロジェクト種類:application、言語:Java)で生成されたbuild.gradleの内容(コメント行を除く)は次です。
plugins {
id 'application'
}
repositories {
mavenCentral()
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
implementation 'com.google.guava:guava:30.0-jre'
}
application {
mainClass = 'com.torutk.hello.App'
}
tasks.named('test') {
useJUnitPlatform()
}
App.java¶
initタスクでは、指定したパッケージにmainメソッドを持つ雛形クラスのソースファイルが1つ生成されます。
- src/main/java/com/torutk/hello/App.java
package com.torutk.hello; public class App { public String getGreeting() { return "Hello world!"; } public static void main(String[] args) { System.out.println(new App().getGreeting()); } }
.gitignore¶
gitリポジトリに登録しないgradleプロジェクトのディレクトリを設定したgit無視ファイルが生成されます。
- .gitignore
.gradle build
.gitattributes¶
gitリポジトリに登録する際のファイル属性を設定したgit属性ファイルが生成されます。
- .gitattributes
*.bat text eol=crlf
これは、gradlew.bat に対応するものです。