SDKMAN

SDKMANを使うことでGroovyファミリーのソフトウェアのインストールや管理が容易になる。
GradleもSDKMANを使ってインストールすることで様々なバージョンの管理をSDKMANに統一することができる。

$ sdk install gradle 6.3
$ sdk use gradle 6.3
$ gradle init
$ cat <<EOF >> build.gradle
task hello {
    println "hello task"
}
EOF
$ gradle tasks --all
$ gradle hello

URL https://sdkman.io

Gradle Quick Start

Gradleの初期化

initプラグインを使えば、gradleビルド環境を作成できる。

$ gradle init --type java-application --dsl groovy --test-framework junit --project-name myproj --package my

> Task :init
Get more help with your project: https://docs.gradle.org/5.6.2/userguide/tutorial_java_projects.html

BUILD SUCCESSFUL in 636ms
2 actionable tasks: 2 executed
$ tree
.
├── build.gradle
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
└── src
    ├── main
    │   ├── java
    │   │   └── my
    │   │       └── App.java
    │   └── resources
    └── test
        ├── java
        │   └── my
        │       └── AppTest.java
        └── resources

11 directories, 8 files

参考 https://docs.gradle.org/6.3/userguide/build_init_plugin.html#sec:java_application

生成されたbuild.gradleは以下の通り。(ver 6.3)

plugins {
    // Apply the java plugin to add support for Java
    id 'java'

    // Apply the application plugin to add support for building a CLI application.
    id 'application'
}

repositories {
    // Use jcenter for resolving dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
}

dependencies {
    // This dependency is used by the application.
    implementation 'com.google.guava:guava:28.2-jre'

    // Use JUnit test framework
    testImplementation 'junit:junit:4.12'
}

application {
    // Define the main class for the application.
    mainClassName = 'my.App'
}

タスク定義の確認

どのようなタスクが定義されているかは以下のようにして確認することができる。

$ gradle tasks

> Task :tasks

------------------------------------------------------------
Tasks runnable from root project
------------------------------------------------------------

Application tasks
-----------------
run - Runs this project as a JVM application

Build tasks
-----------
assemble - Assembles the outputs of this project.
build - Assembles and tests this project.
buildDependents - Assembles and tests this project and all projects that depend on it.
buildNeeded - Assembles and tests this project and all projects it depends on.
classes - Assembles main classes.
clean - Deletes the build directory.
jar - Assembles a jar archive containing the main classes.
testClasses - Assembles test classes.

Build Setup tasks
-----------------
init - Initializes a new Gradle build.
wrapper - Generates Gradle wrapper files.

Distribution tasks
------------------
assembleDist - Assembles the main distributions
distTar - Bundles the project as a distribution.
distZip - Bundles the project as a distribution.
installDist - Installs the project as a distribution as-is.

Documentation tasks
-------------------
javadoc - Generates Javadoc API documentation for the main source code.

Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'myproj'.
components - Displays the components produced by root project 'myproj'. [incubating]
dependencies - Displays all dependencies declared in root project 'myproj'.
dependencyInsight - Displays the insight into a specific dependency in root project 'myproj'.
dependentComponents - Displays the dependent components of components in root project 'myproj'. [incubating]
help - Displays a help message.
model - Displays the configuration model of root project 'myproj'. [incubating]
outgoingVariants - Displays the outgoing variants of root project 'myproj'.
projects - Displays the sub-projects of root project 'myproj'.
properties - Displays the properties of root project 'myproj'.
tasks - Displays the tasks runnable from root project 'myproj'.

Verification tasks
------------------
check - Runs all checks.
test - Runs the unit tests.

Rules
-----
Pattern: clean<TaskName>: Cleans the output files of a task.
Pattern: build<ConfigurationName>: Assembles the artifacts of a configuration.
Pattern: upload<ConfigurationName>: Assembles and uploads the artifacts belonging to a configuration.

To see all tasks and more detail, run gradle tasks --all

To see more detail about a task, run gradle help --task <task>

BUILD SUCCESSFUL in 827ms
1 actionable task: 1 executed

Javaプロジェクトのビルド

javaプラグインとapplicationプラグインを使用しているので、それらのタスクが使用できることが分かる。
javaソースをコンパイルしてみる。

$ gradle build
> Task :compileJava
> Task :processResources NO-SOURCE
> Task :classes
> Task :jar
> Task :startScripts
> Task :distTar
> Task :distZip
> Task :assemble
> Task :compileTestJava
> Task :processTestResources NO-SOURCE
> Task :testClasses
> Task :test
> Task :check
> Task :build

BUILD SUCCESSFUL in 11s
7 actionable tasks: 7 executed

参考 https://docs.gradle.org/6.3/userguide/java_plugin.html

distZipを実行すると依存関係も含めてライブラリ一式をアーカイブしてくれる。
アプリケーションのエントリースクリプトも同梱されている。

$ gradle distZip
> Task :compileJava
> Task :processResources NO-SOURCE
> Task :classes
> Task :jar
> Task :startScripts
> Task :distZip

BUILD SUCCESSFUL in 967ms
4 actionable tasks: 4 executed
$ ls build/distributions/
myproj.zip
$ unzip build/distributions/myproj.zip 
Archive:  build/distributions/myproj.zip
   creating: myproj/
   creating: myproj/lib/
  inflating: myproj/lib/myproj.jar   
  inflating: myproj/lib/guava-28.2-jre.jar  
  inflating: myproj/lib/failureaccess-1.0.1.jar  
  inflating: myproj/lib/listenablefuture-9999.0-empty-to-avoid-conflict-with-guava.jar  
  inflating: myproj/lib/jsr305-3.0.2.jar  
  inflating: myproj/lib/checker-qual-2.10.0.jar  
  inflating: myproj/lib/error_prone_annotations-2.3.4.jar  
  inflating: myproj/lib/j2objc-annotations-1.3.jar  
   creating: myproj/bin/
  inflating: myproj/bin/myproj       
  inflating: myproj/bin/myproj.bat   
$ ./myproj/bin/myproj
Hello world.

Javaアプリケーションの実行

applicatioinプラグインで以下のように定義している。

application {
    // Define the main class for the application.
    mainClassName = 'my.App'
}

mainClassNameで指定したプログラムをrunタスクで実行することができるようになっている。

$ gradle run
> Task :compileJava UP-TO-DATE
> Task :processResources NO-SOURCE
> Task :classes UP-TO-DATE

> Task :run
Hello world.

BUILD SUCCESSFUL in 752ms
2 actionable tasks: 1 executed, 1 up-to-date

参考 https://docs.gradle.org/6.3/userguide/application_plugin.html#application_plugin

Gradle基本

// Project
// https://docs.gradle.org/6.3/dsl/org.gradle.api.Project.html

// スクリプトブロック
plugins {
    id 'java'
}

// ステートメント
//     スクリプト全体で参照できる
//     defキーワードは必要
//     ローカル変数
def defaultEndocoding = "UTF-8"

// 委譲オブジェクトはRepositoryHandler
// https://docs.gradle.org/6.3/dsl/org.gradle.api.artifacts.dsl.RepositoryHandler.html
repositories {
    mavenCentral()
}

// 拡張プロパティ
//     ドメインオブジェクトにプロパティとして追加される
ext {
    key = "value"
}

// タスク設定
task hello {
    // ブロック内で有効な変数
    def message = "hello world"
    println message
    // システムプロパティ
    //     systemProp.xx (in gradle.properties)
    println System.properties['message']
}

// タスク実行
hello << {
     println "Run hello task"
}

参考 https://docs.gradle.org/6.3/dsl/org.gradle.api.Project.html

マルチプロジェクト

マルチプロジェクトのビルドができる。

URL https://docs.gradle.org/6.3/userguide/multi_project_builds.html

pluginsブロック

Projectオブジェクトのプロパティやメソッドでpluginsに関するAPIが見つからない。
Projectは、PluginAwareを継承している。

参考

plugnsブロックは、ビルドスクリプトかsettingsスクリプトで使用される。
buildscriptでは、トップレベルでの定義のみが許されている。

参考 https://docs.gradle.org/6.3/userguide/plugins.html#sec:plugins_block

plugins {
    id "org.flywaydb.flyway" version "6.4.1" apply false
}

subprojects {
    repositories {
        mavenCentral()
    }
}

project(":proj1") {
    apply plugin: "org.flywaydb.flyway"
    println project.name
}

project(":proj2") {
    println project.name
}

参考リンク


トップ   差分 バックアップ リロード   一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
目次
ダブルクリックで閉じるTOP | 閉じる
GO TO TOP