ä»åã¯ãJavaåãã®ãã«ããã¼ã«ã§ãã Gradle ã使ã£ã¦ã¿ã¾ãã
Javaç¨ããããã¯ãJavaã«ä½¿ãããã«ããã¼ã«ã¨ãã¦ãMakeãAntãMavenãªã©ãããã¾ãããæè¿ã¯ Gradle ããã使ããã¦ãã¾ãã
ååã Gradleï¼ã°ã¬ã¼ãã«ï¼ãä¸çªãã£ãããã§ãããèªã¿æ¹ãæããã§ããã§ãããMavenã¯ãã¡ã¤ãã³ã¨èªãã¨æã£ã¦ããã§ããã©ãæ¥æ¬ã®æ¹ã¯ã¡ã¤ãã³ã¨ãã¡ã¤ãã³ã¨ãå¼ãã§ã¾ãããæµ·å¤ã®æ¹ã®YouTubeè¦ãã¨ãã¡ã¤ãã³ã¨ã¡ã¤ãã³ã®ä¸éãããã«èããã¾ãã
ãã¨ãGradle ã®ãã´ãç¹å¾´çã§è¯ããã¶ã¤ã³ã ã¨æãã¾ãï¼ï¼ã¢ã¤ãã£ããç»åã«ä½¿ããã¦ãããã¾ããï¼
ä»åã¯ãGradle ã使ã£ãç°¡åãªããã¸ã§ã¯ãã使ã£ã¦ç解ãã¦ããã¾ãã
åèæç®
ã¯ããã«
ãJavaã§ãã¶ã¤ã³ãã¿ã¼ã³ãå¦ã¶ãã®è¨äºä¸è¦§ã§ããè¯ãã£ããåèã«ãã¦ãã ããã
Javaã§ãã¶ã¤ã³ãã¿ã¼ã³ã®è¨äºä¸è¦§
ã¨ã³ã¸ãã¢ã°ã«ã¼ãã®ã©ã³ãã³ã°ã«åå ä¸ã§ãã
æ°æ¥½ã«ãããã¨ãããããé¡ããããã¾ãð
ããã§ã¯ãã£ã¦ããã¾ãï¼
Ubuntu 22.04 ã« Gradle ãã¤ã³ã¹ãã¼ã«
Gradleã®å
¬å¼ãµã¤ãã§ãã
gradle.org
å
¬å¼ãµã¤ãã®ã¤ã³ã¹ãã¼ã«æé ã§ããææ°ãã¼ã¸ã§ã³ã¯8.7ã®ããã§ãã
Gradle | Installation
å
¬å¼ã®æé ã§ã¯ãSDKMANï¼ã使ããããã¤ããªãæåã§å
¥ããæ¹æ³ããããã¾ããã§ããã
å
¬å¼ã®æé ã¨ã¯éãã¾ãããæ®éã«aptã§ãGradleãã¤ã³ã¹ãã¼ã«ãã¦ã¿ã¾ãã
$ sudo apt install gradle
$ gradle -v
------------------------------------------------------------
Gradle 4.4.1
------------------------------------------------------------
Build time: 2012-12-21 00:00:00 UTC
Revision: none
Groovy: 2.4.21
Ant: Apache Ant(TM) version 1.10.12 compiled on January 17 1970
JVM: 17.0.10 (Private Build 17.0.10+7-Ubuntu-122.04.1)
OS: Linux 6.5.0-28-generic amd64
æ®éã«å
¥ããã¨ãããªãå¤ããã¼ã¸ã§ã³ï¼2012å¹´ï¼ãã¤ã³ã¹ãã¼ã«ããã¾ããã
ãªãã¸ããªã追å ãã¦ã¿ã¾ãã
$ sudo add-apt-repository ppa:cwchien/gradle
$ sudo apt update
$ sudo apt-get upgrade gradle
$ gradle -v
Welcome to Gradle 8.3!
Here are the highlights of this release:
- Faster Java compilation
- Reduced memory usage
- Support for running on Java 20
For more details see https://docs.gradle.org/8.3/release-notes.html
------------------------------------------------------------
Gradle 8.3
------------------------------------------------------------
Build time: 2023-08-17 07:06:47 UTC
Revision: 8afbf24b469158b714b36e84c6f4d4976c86fcd5
Kotlin: 1.9.0
Groovy: 3.0.17
Ant: Apache Ant(TM) version 1.10.13 compiled on January 4 2023
JVM: 17.0.10 (Private Build 17.0.10+7-Ubuntu-122.04.1)
OS: Linux 6.5.0-28-generic amd64
8.3ãå
¥ãã¾ãããã¨ãããããããã§ãã£ã¦ããã¾ãã
å
¬å¼ãµã¤ãã® Getting Started ããã£ã¦ã¿ã
å
¬å¼ãµã¤ãã® Getting Started ã§ãã
Gradle | Gradle Guides
ãã®ä¸ã®ãBuilding Java Applicationsãããã£ã¦ã¿ã¾ãã
ããã¸ã§ã¯ãã®ãã£ã¬ã¯ããªãä½æ
ã¾ããããã¸ã§ã¯ããæ ¼ç´ãããã£ã¬ã¯ããªãä½ãã¾ããdemoãã£ã¬ã¯ããªãä½ãã¨æ¸ããã¦ã¾ããããå°ãå¤ãã¾ãã
$ mkdir gradle-demo
$ cd gradle-demo/
åæåã¿ã¹ã¯ãå®è¡ãã
ç¶ãã¦ãinit task ãå®è¡ãã¾ãã横ã«å¤ãæ¸ãã¦ãã®ã¯å
¥åãããã®ã§ãä½ãæ¸ããã¦ãªãã®ã¯ããã©ã«ããé¸æãã¾ããã
$ gradle init
Starting a Gradle Daemon (subsequent builds will be faster)
Select type of project to generate:
1: basic
2: application
3: library
4: Gradle plugin
Enter selection (default: basic) [1..4] 2
Select implementation language:
1: C++
2: Groovy
3: Java
4: Kotlin
5: Scala
6: Swift
Enter selection (default: Java) [1..6] 3
Generate multiple subprojects for application? (default: no) [yes, no]
Select build script DSL:
1: Kotlin
2: Groovy
Enter selection (default: Kotlin) [1..2]
Select test framework:
1: JUnit 4
2: TestNG
3: Spock
4: JUnit Jupiter
Enter selection (default: JUnit Jupiter) [1..4]
Project name (default: gradle-demo):
Source package (default: gradle.demo):
Enter target version of Java (min. 7) (default: 17):
Generate build using new APIs and behavior (some features may change in the next minor release)? (default: no) [yes, no]
> Task :init
To learn more about Gradle by exploring our Samples at https://docs.gradle.org/8.3/samples/sample_building_java_applications.html
BUILD SUCCESSFUL in 1m 51s
2 actionable tasks: 2 executed
éä¸ã®ãGenerate multiple subprojects for application?ããã¡ãã£ã¨åãããªãã¦ãããã©ã«ã㧠no ã§ããã
yes ã«ããå ´åãæ°ã«ãªã£ãã®ã§ãyesã«ãã¦ä½ã£ãããã¸ã§ã¯ãã¨æ¯è¼ãã¦ã¿ã¾ãã
$ colordiff -u gradle-demo gradle-demo-multi/
å
±éã®ãµããã£ã¬ã¯ããªã¼: gradle-demo/.gradle 㨠gradle-demo-multi/.gradle
å
±éã®ãµããã£ã¬ã¯ããªã¼: gradle-demo/app 㨠gradle-demo-multi/app
gradle-demo-multi/ ã®ã¿ã«åå¨: buildSrc
å
±éã®ãµããã£ã¬ã¯ããªã¼: gradle-demo/gradle 㨠gradle-demo-multi/gradle
gradle-demo-multi/ ã®ã¿ã«åå¨: list
diff -u gradle-demo/settings.gradle.kts gradle-demo-multi/settings.gradle.kts
--- gradle-demo/settings.gradle.kts 2024-04-30 00:24:47.943000000 +0900
+++ gradle-demo-multi/settings.gradle.kts 2024-04-30 00:27:22.604000000 +0900
@@ -10,5 +10,5 @@
id("org.gradle.toolchains.foojay-resolver-convention") version "0.4.0"
}
-rootProject.name = "gradle-demo"
-include("app")
+rootProject.name = "gradle-demo-multi"
+include("app", "list", "utilities")
gradle-demo-multi/ ã®ã¿ã«åå¨: utilities
multiãé¸æããå ´åã¯ãBuildSrcã¨ããç¹å¥ãªãã£ã¬ã¯ããªããã£ã¦ãã³ã¼ãã®åå©ç¨ãããã«ãããã»ã¹ã®ã«ã¹ã¿ãã¤ãºãããã¸ã§ã¯ãæ§é ãæ´çã§ããããã§ãï¼from ChatGPTï¼ã
ããã§ã¯ãmultiã¯é¸æããªãæ¹ã§ãç¶ãããã£ã¦ããã¾ãã
èªåçæããããã£ã¬ã¯ããªããã¡ã¤ã«ã確èªãã¾ãã
$ tree
.
|-- app
| |-- build.gradle.kts
| `-- src
| |-- main
| | |-- java
| | | `-- gradle
| | | `-- demo
| | | `-- App.java
| | `-- resources
| `-- test
| |-- java
| | `-- gradle
| | `-- demo
| | `-- AppTest.java
| `-- resources
|-- gradle
| `-- wrapper
| |-- gradle-wrapper.jar
| `-- gradle-wrapper.properties
|-- gradlew
|-- gradlew.bat
`-- settings.gradle.kts
14 directories, 8 files
ããã¸ã§ã¯ãåã demo
ãã gradle-demo
ã«å¤æ´ãããã¨ã§ãApp.java ã¾ã§ã®é層ã1ã¤æ·±ããªãã¾ããï¼ããã¸ã§ã¯ããdemoã«ããã¨ãgradleãã£ã¬ã¯ããªã¯ä½ãããªãããã§ãï¼ã
éè¦ãªãã¡ã¤ã«ã¯ã以ä¸ã®3ãã¡ã¤ã«ã§ãã
- settings.gradle.ktsï¼gradle-demoããã¸ã§ã¯ãã®è¨å®ãã¡ã¤ã«
- build.gradle.ktsï¼appããã¸ã§ã¯ãã®ãã«ãã¹ã¯ãªãã
- App.javaï¼appããã¸ã§ã¯ãã®Javaã½ã¼ã¹ãã¡ã¤ã«
settings.gradle.ktsã§ãããinclude("xxx")ãã追å ããã¨ããappãã¨åãããã«xxxããã¸ã§ã¯ãã追å ã§ãã¾ãã
plugins {
id("org.gradle.toolchains.foojay-resolver-convention") version "0.4.0"
}
rootProject.name = "gradle-demo"
include("app")
build.gradle.ktsã§ããåãããã¨ã¯ä»¥ä¸ã§ãã
- Javaã®CLIã¢ããªã±ã¼ã·ã§ã³ã§ã
- Maven Centralãªãã¸ããªã使ã£ã¦ãã¾ã
- Google製ã®Javaã©ã¤ãã©ãªãGuavaãã使ã£ã¦ãã¾ã
- Javaã¯ãã¼ã¸ã§ã³17ã使ããã¦ãã¾ã
- ã¨ã³ããªãã¤ã³ãã¯ãgradle.demo.Appãã§ã
plugins {
application
}
repositories {
mavenCentral()
}
dependencies {
testImplementation("org.junit.jupiter:junit-jupiter:5.9.3")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
implementation("com.google.guava:guava:32.1.1-jre")
}
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(17))
}
}
application {
mainClass.set("gradle.demo.App")
}
tasks.named<Test>("test") {
useJUnitPlatform()
}
App.javaã§ãããHello World!ãã表示ããã¢ããªã±ã¼ã·ã§ã³ã®ããã§ãã
package gradle.demo;
public class App {
public String getGreeting() {
return "Hello World!";
}
public static void main(String[] args) {
System.out.println(new App().getGreeting());
}
}
ã¢ããªã±ã¼ã·ã§ã³ãå®è¡ãã
ã¢ããªã±ã¼ã·ã§ã³ãå®è¡ãã¦ã¿ã¾ãã
$ ./gradlew run
Downloading https://services.gradle.org/distributions/gradle-8.3-bin.zip
............10%............20%.............30%............40%.............50%............60%.............70%............80%.............90%............100%
Path for java installation '/usr/lib/jvm/openjdk-17' (Common Linux Locations) does not contain a java executable
> Task :app:run
Hello World!
BUILD SUCCESSFUL in 48s
2 actionable tasks: 2 executed
ã¢ããªã±ã¼ã·ã§ã³ãå®è¡ããã¦ããHello World!ãã表示ããã¾ããã
追å ããããã£ã¬ã¯ããªããã¡ã¤ã«ã確èªãã¾ãã
$ tree
.
|-- app
| |-- build
| | |-- classes
| | | `-- java
| | | `-- main
| | | `-- gradle
| | | `-- demo
| | | `-- App.class
| | |-- generated
| | | `-- sources
| | | |-- annotationProcessor
| | | | `-- java
| | | | `-- main
| | | `-- headers
| | | `-- java
| | | `-- main
| | `-- tmp
| | `-- compileJava
| | `-- previous-compilation-data.bin
| |-- build.gradle.kts
| `-- src
| |-- main
| | |-- java
| | | `-- gradle
| | | `-- demo
| | | `-- App.java
| | `-- resources
| `-- test
| |-- java
| | `-- gradle
| | `-- demo
| | `-- AppTest.java
| `-- resources
|-- gradle
| `-- wrapper
| |-- gradle-wrapper.jar
| `-- gradle-wrapper.properties
|-- gradlew
|-- gradlew.bat
`-- settings.gradle.kts
30 directories, 10 files
ãã£ã¬ã¯ããªã¯ããããå¢ãã¦ã¾ããããã¡ã¤ã«ã¯2ã¤è¿½å ããã¾ãããApp.class ã¨ãprevious-compilation-data.bin ã§ãã
ã¢ããªã±ã¼ã·ã§ã³ããã«ããã
é
å¸å¯è½ãªã¢ããªã±ã¼ã·ã§ã³ãæ§ç¯ãã¾ãã
$ ./gradlew build
Path for java installation '/usr/lib/jvm/openjdk-17' (Common Linux Locations) does not contain a java executable
BUILD SUCCESSFUL in 15s
7 actionable tasks: 6 executed, 1 up-to-date
1è¡ç®ã«è¦åãåºã¦ãã¾ãããstack overflowã«ããã¨ãåé¡ãªãããã§ãã
å¢ãããã¡ã¤ã«ã確èªãã¾ãã
$ tree
.
|-- app
| |-- build
| | |-- classes
| | | `-- java
| | | |-- main
| | | | `-- gradle
| | | | `-- demo
| | | | `-- App.class
| | | `-- test
| | | `-- gradle
| | | `-- demo
| | | `-- AppTest.class
| | |-- distributions
| | | |-- app.tar
| | | `-- app.zip
| | |-- generated
| | | `-- sources
| | | |-- annotationProcessor
| | | | `-- java
| | | | |-- main
| | | | `-- test
| | | `-- headers
| | | `-- java
| | | |-- main
| | | `-- test
| | |-- libs
| | | `-- app.jar
| | |-- reports
| | | `-- tests
| | | `-- test
| | | |-- classes
| | | | `-- gradle.demo.AppTest.html
| | | |-- css
| | | | |-- base-style.css
| | | | `-- style.css
| | | |-- index.html
| | | |-- js
| | | | `-- report.js
| | | `-- packages
| | | `-- gradle.demo.html
| | |-- scripts
| | | |-- app
| | | `-- app.bat
| | |-- test-results
| | | `-- test
| | | |-- TEST-gradle.demo.AppTest.xml
| | | `-- binary
| | | |-- output.bin
| | | |-- output.bin.idx
| | | `-- results.bin
| | `-- tmp
| | |-- compileJava
| | | `-- previous-compilation-data.bin
| | |-- compileTestJava
| | | `-- previous-compilation-data.bin
| | |-- jar
| | | `-- MANIFEST.MF
| | `-- test
| |-- build.gradle.kts
| `-- src
| |-- main
| | |-- java
| | | `-- gradle
| | | `-- demo
| | | `-- App.java
| | `-- resources
| `-- test
| |-- java
| | `-- gradle
| | `-- demo
| | `-- AppTest.java
| `-- resources
|-- gradle
| `-- wrapper
| |-- gradle-wrapper.jar
| `-- gradle-wrapper.properties
|-- gradlew
|-- gradlew.bat
`-- settings.gradle.kts
51 directories, 28 files
ããããå¢ãã¾ãããéè¦ãªã®ã¯ãapp/build/distributions/app.zipï¼app.tarï¼ã§ããããã
app.zipã解åãã¦å®è¡ãã¦ã¿ã¾ãã
$ cd app/build/distributions/
$ unzip app.zip
Archive: app.zip
creating: app/
creating: app/lib/
inflating: app/lib/app.jar
inflating: app/lib/guava-32.1.1-jre.jar
inflating: app/lib/failureaccess-1.0.1.jar
inflating: app/lib/jsr305-3.0.2.jar
inflating: app/lib/checker-qual-3.33.0.jar
inflating: app/lib/error_prone_annotations-2.18.0.jar
creating: app/bin/
inflating: app/bin/app
inflating: app/bin/app.bat
$ cd app/bin/
$ ./app
Hello World!
CLIï¼ã³ãã³ãã©ã¤ã³ã¤ã³ã¿ãã§ã¼ã¹ï¼ã¢ããªã±ã¼ã·ã§ã³ãä½ããã¦ãã¦ãå®è¡ãã§ãã¾ããã
ã¢ããªã±ã¼ã·ã§ã³ãã¹ãã£ã³ãã
ã¹ãã£ã³ããã¨ããã«ãã®è©³ç´°ãæãã¦ãããããã§ãã
$ ./gradlew build --scan
Path for java installation '/usr/lib/jvm/openjdk-17' (Common Linux Locations) does not contain a java executable
BUILD SUCCESSFUL in 1s
7 actionable tasks: 7 up-to-date
Publishing a build scan to scans.gradle.com requires accepting the Gradle Terms of Service defined at https://gradle.com/terms-of-service. Do you accept these terms? [yes, no] yes
Gradle Terms of Service accepted.
Publishing build scan...
https://gradle.com/s/mtyxq5dwgsw76
æ示ãããURLã«ã¢ã¯ã»ã¹ããã¨ãã¡ã¼ã«ã¢ãã¬ã¹ãå
¥åãããã©ã¼ã ããããå
¥åãã¦Goãã¯ãªãã¯ããã¨ãã¡ã¼ã«ãå±ãã¾ãï¼ã¹ã¯ã·ã§å¿ãã¾ããï¼ããã®ã¡ã¼ã«ã®ãªã³ã¯ãã¯ãªãã¯ããã¨ä»¥ä¸ã®ãããªãã«ãçµæã®ã¬ãã¼ããã¼ã¸ãä½æããã¦ã¾ããã
å
¨é¨è¦ãã¦ãªãã§ããã豪è¯ã§è©³ç´°ãªã¬ãã¼ãã§ããã
ãããã«
ä»å㯠Gradle ã使ã£ã¦ãJava ã®ã³ãã³ãã©ã¤ã³ã¢ããªã±ã¼ã·ã§ã³ãä½ã£ã¦ã¿ã¾ããã
次åã¯ãä»åä½ã£ã Gradle ããã¸ã§ã¯ãã IntelliJï¼ç·åéçºç°å¢ï¼ã§ãããã°ãããã¨æãã¾ãã
ä»åã¯ä»¥ä¸ã§ãï¼
æå¾ã¾ã§ãèªã¿ããã ãããããã¨ããããã¾ããã