gradleの初期化 †
initプラグインを使えば、gradleビルド環境を作成できる。
$ 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
参考 https://docs.gradle.org/6.3/userguide/build_init_plugin.html#sec:java_application
生成されたbuild.gradleは以下の通り。(ver 6.3)
$ 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
タスク定義の確認 †
どのようなタスクが定義されているかは以下のようにして確認することができる。
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' }
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 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アプリケーションの実行 †
applicatioinプラグインで以下のように定義している。
$ 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.
mainClassName
で指定したプログラムをrun
タスクで実行することができるようになっている。
application { // Define the main class for the application. mainClassName = 'my.App' }
参考 https://docs.gradle.org/6.3/userguide/application_plugin.html#application_plugin
SDKMAN †
SDKMANを使うことでGroovyファミリーのソフトウェアのインストールや管理が容易になる。
GradleもSDKMANを使ってインストールすることで様々なバージョンの管理をSDKMANに統一することができる。
$ 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
参考リンク †
- リリース情報
- マニュアル
- SDKMAN