ビルドツールを使ったことないよわよわJavaエンジニアがGradleを使ってビルド生活をしてみる
タイトル:ビルドツールを使ったことないよわよわJavaエンジニアがGradleを使ってビルド生活をしてみる
0時間目
Gradleについて調べてみました
下記リンク先を参考にしました
【Java】Gradleのインストールと基本的な使い方(画像付きで解説)
1時間目
なんとなくわかったので使い始めてみます。
バージョン確認
gradle -v ------------------------------------------------------------ Gradle 5.2.1 ------------------------------------------------------------ Build time: 2019-02-08 19:00:10 UTC Revision: f02764e074c32ee8851a4e1877dd1fea8ffb7183 Kotlin DSL: 1.1.3 Kotlin: 1.3.20 Groovy: 2.5.4 Ant: Apache Ant(TM) version 1.9.13 compiled on July 10 2018 JVM: 1.8.0_131 (Oracle Corporation 25.131-b11) OS: Mac OS X 10.14 x86_64
プロジェクトの作成
gradle init --type java-application Select build script DSL: 1: groovy 2: kotlin Enter selection (default: groovy) [1..2] 1 Select test framework: 1: junit 2: testng 3: spock Enter selection (default: junit) [1..3] 1 Project name (default: GradleTest): gradleSrc Source package (default: gradleSrc): BUILD SUCCESSFUL in 18s 2 actionable tasks: 2 executed
中身を確認してみると、
tree . ├── build.gradle ├── gradle │ └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src ├── main │ ├── java │ │ └── gradleSrc │ │ └── App.java │ └── resources └── test ├── java │ └── gradleSrc │ └── AppTest.java └── resources 11 directories, 8 files
App.javaはのソースはこんな感じ
App.java
package gradleSrc; public class App { public String getGreeting() { return "Hello world."; } public static void main(String[] args) { System.out.println(new App().getGreeting()); } }
build.gradleのソースはこんな感じ
build.gradle
/* * This file was generated by the Gradle 'init' task. * * This generated file contains a sample Java project to get you started. * For more details take a look at the Java Quickstart chapter in the Gradle * User Manual available at https://docs.gradle.org/5.2.1/userguide/tutorial_java_projects.html */ plugins { // Apply the java plugin to add support for Java id 'java' // Apply the application plugin to add support for building an application id 'application' } repositories { // Use jcenter for resolving your dependencies. // You can declare any Maven/Ivy/file repository here. jcenter() } dependencies { // This dependency is found on compile classpath of this component and consumers. implementation 'com.google.guava:guava:27.0.1-jre' // Use JUnit test framework testImplementation 'junit:junit:4.12' } // Define the main class for the application mainClassName = 'gradleSrc.App'
mainClassNameにビルドするメインクラスを書くらしい
実際にビルドをしてみると
gradle build BUILD SUCCESSFUL in 16s 7 actionable tasks: 7 executed
ビルド完了。動かしてみる。
radle run > Task :run Hello world. BUILD SUCCESSFUL in 0s 2 actionable tasks: 1 executed, 1 up-to-date
デフォルトのソースはビルド→実行できたので次は自作のソースコードを作成して動かしてみます。
GradleTest.java
package GradleTest.src.main.java.gradleSrc; public class GradleTest { public static void main(String[] args) { System.out.println("Hello, Gradle"); } }
built.gradle
/* * This file was generated by the Gradle 'init' task. * * This generated file contains a sample Java project to get you started. * For more details take a look at the Java Quickstart chapter in the Gradle * User Manual available at https://docs.gradle.org/5.2.1/userguide/tutorial_java_projects.html */ plugins { // Apply the java plugin to add support for Java id 'java' // Apply the application plugin to add support for building an application id 'application' } repositories { // Use jcenter for resolving your dependencies. // You can declare any Maven/Ivy/file repository here. jcenter() } dependencies { // This dependency is found on compile classpath of this component and consumers. implementation 'com.google.guava:guava:27.0.1-jre' // Use JUnit test framework testImplementation 'junit:junit:4.12' } // Define the main class for the application mainClassName = 'gradleSrc.GradleTest'
buit.gradleは「mainClassName」のところだけ変えました。
実際にビルド→実行できるか試してみます。
ビルド
gradle build BUILD SUCCESSFUL in 1s 7 actionable tasks: 6 executed, 1 up-to-date
実行
> Task :run FAILED エラー: メイン・クラスgradleSrc.GradleTestが見つからなかったかロードできませんでした FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':run'. > Process 'command '/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/bin/java'' finished with non-zero exit value 1 * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights. * Get more help at https://help.gradle.org BUILD FAILED in 0s 2 actionable tasks: 1 executed, 1 up-to-date
あれ?実行が失敗した。エラー: メイン・クラスgradleSrc.GradleTestが見つからなかったかロードできませんでしたと出てくるからもしや。
built.gradle
/* * This file was generated by the Gradle 'init' task. * * This generated file contains a sample Java project to get you started. * For more details take a look at the Java Quickstart chapter in the Gradle * User Manual available at https://docs.gradle.org/5.2.1/userguide/tutorial_java_projects.html */ plugins { // Apply the java plugin to add support for Java id 'java' // Apply the application plugin to add support for building an application id 'application' } repositories { // Use jcenter for resolving your dependencies. // You can declare any Maven/Ivy/file repository here. jcenter() } dependencies { // This dependency is found on compile classpath of this component and consumers. implementation 'com.google.guava:guava:27.0.1-jre' // Use JUnit test framework testImplementation 'junit:junit:4.12' } // Define the main class for the application mainClassName = 'GradleTest.src.main.java.gradleSrc.GradleTest'
「mainClassName」の値をフルネームで記載しました。
実行
gradle run > Task :run Hello, Gradle BUILD SUCCESSFUL in 0s 2 actionable tasks: 1 executed, 1 up-to-date
できたぁ!ある程度感覚が掴めてきたので2時間目からはフルで生かそうと思います。
2時間目
私が今読んでいるJava言語プログラミングレッスン 第3版(下) オブジェクト指向を始めようのソースを使ってビルドしてみます。
ShoFile1.java
package GradleTest.src.main.java.gradleSrc; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class ShowFile1 { public static void main(String[] args) { if (args.length != 1) { System.out.println("使用方法:java ShowFile1 ファイル"); System.out.println("例:kjava ShowFile1 ShowFile1.java"); System.exit(0); } String filename = args[0]; try { BufferedReader reader = new BufferedReader(new FileReader(filename)); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } reader.close(); } catch (FileNotFoundException e) { System.out.println(filename + "がみつかりません。"); } catch (IOException e) { System.out.println(e); } } }
今度は引数が必要です。
下記参考にbuilt.gradleを作りました。
built.gradle
mainClassName = 'GradleTest.src.main.java.gradleSrc.ShowFile1' run { if (project.hasProperty('args')) { args project.args.split('\\s+') } }
実行
gradle run -Pargs="test.txt" > Task :run test1 test2 test3 Hello, Gradle. BUILD SUCCESSFUL in 0s 2 actionable tasks: 1 executed, 1 up-to-date
実行できました!これで引数も対応できますね!