#author("2018-07-21T03:43:16+00:00","default:haikikyou","haikikyou")
[[HaikikyouのIT関連技術メモ]]
#author("2020-05-01T06:55:43+00:00","default:haikikyou","haikikyou")
#contents

* SDKMAN [#x06ca6f4]

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

#geshi(bash){{{
$ 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
}}}

&label(link){URL}; https://sdkman.io

* Gradle Quick Start [#mca61ea8]
** Gradleの初期化 [#ad99df83]

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

#geshi(bash){{{
$ 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
}}}
&label(warn){参考}; https://docs.gradle.org/6.3/userguide/build_init_plugin.html#sec:java_application

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

#geshi(groovy){{{
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'
}
}}}


** タスク定義の確認 [#k86dda82]

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

#geshi(bash){{{
$ 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プロジェクトのビルド [#s1bf9916]

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

#geshi{{{
$ 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
}}}

&label(warn){参考}; https://docs.gradle.org/6.3/userguide/java_plugin.html

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

#geshi(bash){{{
$ 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アプリケーションの実行 [#gc595518]

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

#geshi(groovy){{{
application {
    // Define the main class for the application.
    mainClassName = 'my.App'
}
}}}

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

#geshi(bash){{{
$ 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
}}}

&label(warn){参考};  https://docs.gradle.org/6.3/userguide/application_plugin.html#application_plugin

* Gradle基本 [#o6f1257a]


#geshi(groovy){{{
// 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"
}
}}}
&label(warn){参考}; https://docs.gradle.org/6.3/dsl/org.gradle.api.Project.html

- gradleは、ステートメントとスクリプトブロックからなる。
- スクリプトブロックに渡されるクロージャは、そのドメインオブジェクトに委譲されて実行される。~
どのドメインでクロージャが実行されているかを意識すればDocsを見ながら使用できるDSLも分かる。~
&label(link){URL}; https://docs.gradle.org/6.3/dsl/index.html
- プロパティには以下がある。 ~
&label(warn){参考}; https://docs.gradle.org/6.3/userguide/build_environment.html
-- ローカル変数(def)
-- システムプロパティ(System、systemProp.xxx)
-- 拡張プロパティ(ext{})
-- プロジェクトプロパティ(.properties、-P、ORG_GRADLE_PROJECT_XXX)~
- &code(){task <task名> { }};はタスク設定、&code(){task << };はタスク実行時の定義であることに注意。~
設定時と実行時で実行されるタイミングが異なる。
- ProjectプロパティのaddRuleでタスクルール定義を追加できる。~
(&code(){clean<TaskName>};のようなタスクルールのこと)

**  マルチプロジェクト [#s3df540b]
マルチプロジェクトのビルドができる。

&label(link){URL}; https://docs.gradle.org/6.3/userguide/multi_project_builds.html

** pluginsブロック [#yb260b6c]

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

&label(warn){参考};

- https://stackoverflow.com/questions/51511666/gradles-new-plugins-dsl-syntax
- https://docs.gradle.org/6.3/dsl/org.gradle.api.Project.html#N14F6A
- https://docs.gradle.org/6.3/javadoc/org/gradle/api/plugins/PluginAware.html

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

&label(warn){参考}; https://docs.gradle.org/6.3/userguide/plugins.html#sec:plugins_block

#geshi(groovy){{{
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
}
}}}
* 参考リンク [#w2497b2e]
- [[Gradle User Manual>https://docs.gradle.org/current/userguide/userguide.html]] - &size(11){&color(gray){on https://docs.gradle.org/current/userguide/userguide.html};};
- [[Gradle User Guide (Version 2.2-20140924021627+0000)>http://gradle.monochromeroad.com/docs/userguide/userguide.html]] - &size(11){&color(gray){on http://gradle.monochromeroad.com/docs/userguide/userguide.html};};
- リリース情報
-- https://gradle.org/releases/
- マニュアル
-- [[Gradle User Manual>https://docs.gradle.org/current/userguide/userguide.html]] - &size(11){&color(gray){on https://docs.gradle.org/current/userguide/userguide.html};};
-- [[Gradle User Guide (Version 2.2-20140924021627+0000)>http://gradle.monochromeroad.com/docs/userguide/userguide.html]] - &size(11){&color(gray){on http://gradle.monochromeroad.com/docs/userguide/userguide.html};};
- SDKMAN
-- [[The Software Development Kit Manager>https://sdkman.io]] - &size(11){&color(gray){on https://sdkman.io};};


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