#author("2020-04-30T12:34:25+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){{{ // スクリプトブロック 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'] } }}} - gradleは、ステートメントとスクリプトブロックからなる。 - スクリプトブロックに渡されるクロージャは、そのドメインオブジェクトに委譲されて実行される。~ どのドメインでクロージャが実行されているかを意識すればDocsを見ながら使用できるDSLも分かる。 - プロパティには以下がある。 ~ &label(warn){参考}; https://docs.gradle.org/6.3/userguide/build_environment.html -- ローカル変数(def) -- システムプロパティ(System、systemProp.xxx) -- 拡張プロパティ(ext{}) -- プロジェクトプロパティ(.properties、-P、ORG_GRADLE_PROJECT_XXX)~ 基本的にスクリプトブロックの記述順は影響しない。 * 参考リンク [#w2497b2e] - リリース情報 -- 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};};