ææ°ã®ãã«ãããŒã«Gradleã®äžçãžããããïŒ
ã¯ããã«ïŒGradleãšã¯ïŒãªãä»ãGradleãªã®ãïŒ ð€
ãœãããŠã§ã¢éçºã®äžçã§ã¯ãã³ãŒããæžãã ãã§ãªãããããã³ã³ãã€ã«ãããã¹ãããããã±ãŒãžåããæã«ã¯ãããã€ãããšããäžé£ã®äœæ¥ãäžå¯æ¬ ã§ãããããã®å埩çãªäœæ¥ãæäœæ¥ã§è¡ãã®ã¯éå¹ççã§ããããã¹ãèµ·ããããããªããŸããããã§ç»å Žããã®ãããã«ãèªååããŒã«ãã§ãã
Gradle (ã°ã¬ã€ãã«) ã¯ãçŸä»£çãªãœãããŠã§ã¢éçºã«ãããŠåºãå©çšãããŠãããªãŒãã³ãœãŒã¹ã®ãã«ãèªååããŒã«ã§ããç¹ã«Javaãäžå¿ãšãããããžã§ã¯ãã§åŒ·åãªæ¯æãåŸãŠããŸãããKotlinãScalaãGroovyãšãã£ãJVMèšèªã¯ãã¡ãããC/C++ãJavaScriptãªã©ãå€èšèªéçºã«ã察å¿ããæè»æ§ãæã£ãŠããŸãã
Gradleã¯ã2008幎é ã«Hans Dockteræ°ãã«ãã£ãŠéçºãå§ãŸãã2012幎ã«ããŒãžã§ã³1.0ããªãªãŒã¹ãããŸãããApache AntãApache Mavenãšãã£ãå è¡ãããã«ãããŒã«ã®è¯ãç¹ãåãå ¥ãã€ã€ããã®èª²é¡ïŒXMLã«ããèšè¿°ã®åé·æ§ãæè»æ§ã®äœããªã©ïŒã解決ããããšãç®æããŠèšèšãããŸããã
ãªãä»ãGradleãåŠã¶ããšãéèŠãªã®ã§ããããïŒ
- ã¢ãã³ãªéçºç°å¢ã§ã®æšæºå: Androidã¢ããªéçºã§ã¯ã2013幎é ããAndroid Studioã®æšæºãã«ãããŒã«ãšããŠæ¡çšãããŠãããå¿ é ã®ç¥èãšãªã£ãŠããŸãããŸããSpring Bootãªã©ã®äººæ°Javaãã¬ãŒã ã¯ãŒã¯ã§ãåºã䜿ãããŠããŸãã
- é«ãæè»æ§ãšæ¡åŒµæ§: ãã«ãã¹ã¯ãªãããGroovyãKotlinãšãã£ãããã°ã©ãã³ã°èšèªã§èšè¿°ã§ãããããXMLããŒã¹ã®èšå®ãã¡ã€ã«ãããã¯ããã«æè»ã§åŒ·åãªãã«ãããžãã¯ãå®è£ ã§ããŸããç¬èªã®ã¿ã¹ã¯ããã©ã°ã€ã³ã®äœæã容æã§ãã
- åªããããã©ãŒãã³ã¹: ãã«ãæéã®ççž®ã¯çç£æ§ã«çŽçµããŸããGradleã¯ãå€æŽãããéšåã ãããã«ããããã€ã³ã¯ãªã¡ã³ã¿ã«ãã«ããããã«ãçµæãåå©çšããããã«ããã£ãã·ã¥ãããã«ãããã»ã¹ãã¡ã¢ãªäžã«ä¿æãããGradleããŒã¢ã³ããªã©ã®æ©èœã«ãããç¹ã«å€§èŠæš¡ãããžã§ã¯ãã«ãããŠMavenãAntãããé«éãªãã«ããå®çŸããŸãã
- 掻çºãªã³ãã¥ããã£ãšãšã³ã·ã¹ãã : Gradleæ¬äœã®éçºã¯æŽ»çºã§ãããç¶ç¶çã«æ°æ©èœã®è¿œå ãããã©ãŒãã³ã¹æ¹åãè¡ãããŠããŸãããŸããGradle Plugin Portalã«ã¯æ§ã ãªçšéã«å¯Ÿå¿ããè±å¯ãªãã©ã°ã€ã³ãå ¬éãããŠãããå¿ èŠãªæ©èœãç°¡åã«è¿œå ã§ããŸãã
ãã®ããã°ã§ã¯ãGradleã®åºæ¬çãªæŠå¿µãããã€ã³ã¹ããŒã«æ¹æ³ãåºæ¬çãªäœ¿ãæ¹ããã«ãã¹ã¯ãªããã®æžãæ¹ãäž»èŠãªæ©èœããããŠAntãMavenãšã®æ¯èŒãŸã§ãç¶²çŸ çã«è§£èª¬ããŠãããŸããGradleãåããŠäœ¿ãæ¹ã§ãããã®èšäºãèªãã°Gradleã®äžçãžã®ç¬¬äžæ©ãèžã¿åºããã¯ãã§ãããããäžç·ã«GradleãåŠãã§ãããŸãããïŒð
Gradleã®æŽå²ãšé²å ð
Gradleã¯ãJavaãšã³ã·ã¹ãã ã«ããããã«ãããŒã«ã®é²åã®æµãã®äžã§çãŸããŸããããã®æŽå²ãçŽè§£ãããšã§ãGradleãã©ã®ãããªèæ¯ãšææ³ã®ããšã«èšèšãããã®ããç解ããããšãã§ããŸãã
- Makeã®æ代: åæã®ãœãããŠã§ã¢éçºã§ã¯ãUnixç³»OSã§åºã䜿ãããŠããMakeããã«ãèªååã®äž»æµã§ããããããããã©ãããã©ãŒã äŸåæ§ãè€éãªãã«ãã«å¯Ÿå¿ãã«ãããšãã課é¡ããããŸããã
- Apache Antã®ç»å Ž (2000幎é ): Javaã®æ®åãšãšãã«ããã©ãããã©ãŒã éäŸåã®ãã«ãããŒã«ãšããŠApache Antãç»å ŽããŸãããXML圢åŒã®
build.xml
ãã¡ã€ã«ã«ãå®è¡ãããã¿ã¹ã¯ïŒã³ã³ãã€ã«ãã³ããŒãåé€ãªã©ïŒãåœä»€çã«èšè¿°ããŠããã¹ã¿ã€ã«ã§ããéåžžã«æè»æ§ãé«ããæ§ã ãªåŠçãèšè¿°ã§ããŸãããããããžã§ã¯ãã®æ§é ãäŸåé¢ä¿ç®¡çã«é¢ããæšæºçãªèŠçŽããªãã£ãããããããžã§ã¯ãããšã«ãã«ãã¹ã¯ãªããã®æžãæ¹ãç°ãªãã倧èŠæš¡ã«ãªããšéåžžã«è€éã§ã¡ã³ããã³ã¹ãå°é£ã«ãªããšããåé¡ããããŸãããäŸåé¢ä¿ç®¡çã«ã¯ãå¥éApache Ivyãªã©ã®ããŒã«ãçµã¿åãããå¿ èŠããããŸããã - Apache Mavenã®ç»å Ž (2004幎é ): Antã®èª²é¡ã§ãã£ãæšæºåã®æ¬ åŠã解決ããããã«ãApache Mavenãç»å ŽããŸããããèšå®ããèŠçŽ (Convention over Configuration)ããšããææ³ã«åºã¥ããæšæºçãªãããžã§ã¯ããã£ã¬ã¯ããªæ§é ãšã
compile
,test
,package
ãšãã£ãå®çŸ©æžã¿ã®ãã«ãã©ã€ããµã€ã¯ã«ãå°å ¥ããŸãããXML圢åŒã®pom.xml
(Project Object Model) ãã¡ã€ã«ã«ããããžã§ã¯ãæ å ±ãäŸåã©ã€ãã©ãªã䜿çšãããã©ã°ã€ã³ãªã©ã宣èšçã«èšè¿°ããŸããäŸåé¢ä¿ç®¡çæ©èœãçµã¿èŸŒãŸããŠãããMaven Centralãªããžããªãªã©ããã©ã€ãã©ãªãèªåçã«ããŠã³ããŒãã»ç®¡çã§ããŸããããã«ãããå€ãã®ãããžã§ã¯ãã§ãã«ãæ§æãçµ±äžãããç解ãããããªããŸãããããããèŠçŽããå€ããç¹æ®ãªãã«ããè¡ãããå Žåã«ã¯ãæè»æ§ã«æ¬ ããèšå®ãè€éã«ãªããšãããã¡ãªããããããŸããã - Gradleã®èªçãšçºå± (2008幎é éçºéå§ã2012幎v1.0ãªãªãŒã¹): Gradleã¯ãAntã®ãæè»æ§ããšMavenã®ãèŠçŽãšäŸåé¢ä¿ç®¡çãã®åæ¹ã®å©ç¹ãçµã¿åãããããšãç®æããŠéçºãããŸãããMavenã®ããã«èŠçŽã«åºã¥ãããã«ããåºæ¬ãšããªããããXMLã§ã¯ãªãGroovyïŒJVMäžã§åäœããåçããã°ã©ãã³ã°èšèªïŒãããŒã¹ãšããDSL (Domain Specific Language) ãæ¡çšããããšã§ããã«ãããžãã¯ãããç°¡æœãã€åŒ·åã«èšè¿°ã§ããããã«ããŸãããããã«ãããAntã®ãããªæè»ãªã«ã¹ã¿ãã€ãºãå¯èœã«ãªããŸããããŸããMavenãªããžããªãšã®äºææ§ãæã¡ã匷åãªäŸåé¢ä¿ç®¡çæ©èœãæäŸããŸãã
- 2013幎: GoogleãAndroid Studioã®æšæºãã«ãããŒã«ãšããŠGradleãæ¡çšãããã«ããGradleã®å©çšè ãççºçã«å¢å ããŸããã
- 2016幎 (v3.0): KotlinïŒåããJVMèšèªïŒããã«ãã¹ã¯ãªããèšèªãšããŠãµããŒãéå§ (Kotlin DSL)ãéçåä»ãã«ããIDEãµããŒãïŒè£å®ããšã©ãŒãã§ãã¯ïŒã®åäžãªã©ãã¡ãªããã§ãã
- 2017幎: ãã«ããã£ãã·ã¥ããã«ãåææ©èœãæäŸããæåãµãŒãã¹ Gradle Enterprise (çŸ Develocity) ããªãªãŒã¹ã
- 2018幎 (v5.0): Kotlin DSLãæ¬çªå©çšå¯èœ (production-ready) ãšãªããJava 11ããµããŒãã
- 2019幎 (v6.0): äŸåé¢ä¿ç®¡çæ©èœã®åŒ·åïŒããŒãžã§ã³ã¢ã©ã€ã³ã¡ã³ããªã©ïŒã
- ç¶ç¶çãªé²å: ãã®åŸããããã©ãŒãã³ã¹åäžïŒã€ã³ã¯ãªã¡ã³ã¿ã«ãã«ããèšå®ãã£ãã·ã¥ãªã©ïŒããã«ããããžã§ã¯ããã«ãã®æ¹åãæ°ããJavaããŒãžã§ã³ã®ãµããŒãããã©ã°ã€ã³APIã®æ¹è¯ãªã©ãç¶ããããŠããŸãã
ãã®ããã«ãGradleã¯å è¡ããŒã«ã®èª²é¡ãå æããçŸä»£çãªãœãããŠã§ã¢éçºã®èŠæ±ã«å¿ããããã«é²åãç¶ããŠããŸããããã®æè»æ§ãããã©ãŒãã³ã¹ã匷åãªæ©èœã«ãããå€ãã®ãããžã§ã¯ãã§æšæºçãªãã«ãããŒã«ãšããŠã®å°äœã確ç«ããŠããŸãã
Gradleã®äž»ãªç¹åŸŽ âš
Gradleãå€ãã®éçºè ã«æ¯æãããçç±ã¯ããã®ãŠããŒã¯ã§åŒ·åãªç¹åŸŽã«ãããŸããããã§ã¯ãç¹ã«éèŠãªç¹åŸŽã詳ããèŠãŠãããŸãããã
1. æè»ãªãã«ãã¹ã¯ãªãã (Groovy / Kotlin DSL)
Gradleã®æãéç«ã£ãç¹åŸŽã¯ããã«ãã®ããžãã¯ãå®éã®ããã°ã©ãã³ã°èšèªã§èšè¿°ã§ããç¹ã§ããããã«ãããéçãªXMLèšå®ãã¡ã€ã«ã§ã¯é£ããã£ããåçãªåŠçãè€éãªæ¡ä»¶åå²ãå«ããã«ãããã»ã¹ã容æã«æ§ç¯ã§ããŸãã
- Groovy DSL (
build.gradle
): Gradleã®åæãããµããŒããããŠããDSLã§ããGroovyã¯Javaãã©ãããã©ãŒã äžã§åäœããåçèšèªã§ãJavaã«äŒŒãæ§æãæã¡ãªãããããç°¡æœãªèšè¿°ãå¯èœã§ããJavaéçºè ã«ãšã£ãŠã¯æ¯èŒçç¿åŸãããããæè»ã§è¡šçŸåè±ããªã¹ã¯ãªãããæžãããšãã§ããŸãã - Kotlin DSL (
build.gradle.kts
): 2016幎ãããµããŒããããè¿å¹Žæšå¥šãããåŸåã«ããDSLã§ããKotlinã¯éçåä»ãèšèªã§ããã以äžã®ãããªã¡ãªããããããŸãã- IDEã«ãã匷åãªãµããŒãïŒã³ãŒãè£å®ããªãã¡ã¯ã¿ãªã³ã°ããšã©ãŒæ€åºïŒ
- åå®å šæ§ã«ãããã«ãã¹ã¯ãªããã®å ç¢æ§åäž
- Kotlinèšèªèªäœã®ã¢ãã³ãªæ©èœïŒNullå®å šãæ¡åŒµé¢æ°ãªã©ïŒã®æŽ»çš
ã©ã¡ãã®DSLãéžæããŠããGradleã®ã³ã¢APIããã©ã°ã€ã³ãæäŸããæ©èœãå©çšã§ããŸãããããžã§ã¯ãã®ç¹æ§ãããŒã ã®ã¹ãã«ã»ããã«åãããŠéžæããããšãå¯èœã§ãã
// build.gradle (Groovy DSL äŸ)
task helloGroovy {
doLast {
println "Hello from Groovy DSL! ${System.getProperty('java.version')}"
}
}
// build.gradle.kts (Kotlin DSL äŸ)
tasks.register("helloKotlin") {
doLast {
println("Hello from Kotlin DSL! ${System.getProperty("java.version")}")
}
}
2. 宣èšçãªãã«ããšåœä»€çãªããžãã¯ã®èå
Gradleã¯ãMavenã®ãããªãäœã (What)ããã«ããããã®å®£èšçãªã¢ãããŒããšãAntã®ãããªãã©ã®ããã« (How)ããã«ããããã®åœä»€çãªã¢ãããŒãããã©ã³ã¹è¯ãåãå ¥ããŠããŸãã
- 宣èšçãªåŽé¢: Javaãã©ã°ã€ã³ãé©çšããã°ããœãŒã¹ã³ãŒãã®å Žæãåºåãã£ã¬ã¯ããªãªã©ã®èŠçŽãèªåçã«èšå®ããã
build
ã¿ã¹ã¯ãå®è¡ããã ãã§ã³ã³ãã€ã«ããã¹ããJARäœæãšãã£ãäžé£ã®åŠçãè¡ãããŸããäŸåé¢ä¿ãdependencies
ãããã¯ã«å®£èšçã«èšè¿°ããŸãã - åœä»€çãªåŽé¢: ãã«ãã¹ã¯ãªããå ã§ããã°ã©ãã³ã°ããžãã¯ã䜿ã£ãŠãæ¢åã®ã¿ã¹ã¯ã®åäœãã«ã¹ã¿ãã€ãºããããå šãæ°ããã«ã¹ã¿ã ã¿ã¹ã¯ãå®çŸ©ãããããããšã容æã§ããäŸãã°ããç¹å®ã®æ¡ä»¶äžã§ã®ã¿ãã¡ã€ã«ãã³ããŒãããããã«ãåã«å€éšAPIããããŒã¿ãååŸããããšãã£ãåŠçãèªç±ã«èšè¿°ã§ããŸãã
ãã®çµã¿åããã«ãããäžè¬çãªãããžã§ã¯ãã§ã¯èŠçŽã«åŸã£ãŠã·ã³ãã«ã«ãã«ããå®çŸ©ã§ããè€éãªèŠä»¶ãæã€ãããžã§ã¯ãã§ã¯æè»ã«ã«ã¹ã¿ãã€ãºã§ãããšãããäž¡æ¹ã®äžçã®å©ç¹ã享åã§ããŸãã
3. ããã©ãŒãã³ã¹ãžã®åŸ¹åºçãªãã ãã ð
éçºãµã€ã¯ã«ã«ãããŠãã«ãæéã¯ç¡èŠã§ããªãèŠçŽ ã§ããGradleã¯ãã«ãããã©ãŒãã³ã¹ãéåžžã«éèŠããŠãããæ§ã ãªæé©åæè¡ãå°å ¥ããŠããŸãã
- ã€ã³ã¯ãªã¡ã³ã¿ã«ãã«ã (Incremental Build): Gradleã¯åã¿ã¹ã¯ã®å
¥åãšåºåã远跡ããŸãããã«ããå®è¡ããéãå
¥åãååã®ãã«ãããå€æŽãããŠããªãã¿ã¹ã¯ã¯å®è¡ãã¹ãããããŸãïŒ
UP-TO-DATE
ãšè¡šç€ºãããŸãïŒãããã«ãããã³ãŒãã®äžéšã ããå€æŽããå Žåãªã©ãäžèŠãªåãã«ããé¿ããæéãå€§å¹ ã«ç¯çŽããŸãã - ãã«ããã£ãã·ã¥ (Build Cache): ã¿ã¹ã¯ã®åºåããã£ãã·ã¥ããåãå ¥åïŒãœãŒã¹ã³ãŒããäŸåé¢ä¿ãèšå®ãªã©ïŒãæã€ã¿ã¹ã¯ãå床å®è¡ãããéã«ããã£ãã·ã¥ãããåºåãåå©çšããä»çµã¿ã§ããããŒã«ã«ãã·ã³äžã®ãã£ãã·ã¥ïŒããŒã«ã«ãã«ããã£ãã·ã¥ïŒã ãã§ãªãããããã¯ãŒã¯çµç±ã§ããŒã ãCIãµãŒããŒéã§ãã£ãã·ã¥ãå ±æãããªã¢ãŒããã«ããã£ãã·ã¥ãå©çšå¯èœã§ããããã«ãããåãã³ãããããã«ãããå Žåãªã©ã«CIã®ãã«ãæéãªã©ãåçã«ççž®ã§ããå¯èœæ§ããããŸãã
- GradleããŒã¢ã³ (Gradle Daemon): ååã®ãã«ãæã«Gradleããã»ã¹ïŒããŒã¢ã³ïŒãããã¯ã°ã©ãŠã³ãã§èµ·åããã¡ã¢ãªäžã«ãã«ãæ å ±ïŒã¯ã©ã¹ããŒããŒããããžã§ã¯ãæ§é ãã¿ã¹ã¯ã°ã©ããªã©ïŒããã£ãã·ã¥ããŸãã2åç®ä»¥éã®ãã«ãã§ã¯ããã®ããŒã¢ã³ãåå©çšããããããJVMã®èµ·åãã¯ã©ã¹èªã¿èŸŒã¿ã®ãªãŒããŒãããããªããªãããã«ãéå§ãŸã§ã®æéãå€§å¹ ã«ççž®ãããŸãã
- èšå®ãã£ãã·ã¥ (Configuration Cache): 倧èŠæš¡ãããžã§ã¯ãã§ã¯ããã«ãã¹ã¯ãªããã解æããã¿ã¹ã¯ã°ã©ããæ§ç¯ãããèšå®ãã§ãŒãºãã«ãæéããããããšããããŸããèšå®ãã£ãã·ã¥ã¯ããã®èšå®ãã§ãŒãºã®çµæããã£ãã·ã¥ãããã«ãã¹ã¯ãªãããèšå®ãã¡ã€ã«ã«å€æŽããªãéããèšå®ãã§ãŒãºèªäœãã¹ãããããŸãã
- 䞊åå®è¡ (Parallel Execution): ãã«ãã³ã¢CPUã掻çšããäŸåé¢ä¿ã®ãªãã¿ã¹ã¯ãåæã«å®è¡ããŸãã
--parallel
ãªãã·ã§ã³ãä»ããããgradle.properties
ãã¡ã€ã«ã§èšå®ããããšã§æå¹ã«ãªããŸãã
ãããã®æ©èœã«ãããGradleã¯ç¹ã«å埩çãªéçºãµã€ã¯ã«ã«ãããŠé«éãªãã£ãŒãããã¯ã«ãŒããå®çŸããŸãã
4. 匷åãã€æè»ãªäŸåé¢ä¿ç®¡ç ð
çŸä»£ã®ãœãããŠã§ã¢éçºã§ã¯ãå€éšã©ã€ãã©ãªã®å©çšãäžå¯æ¬ ã§ããGradleã¯ãApache IvyãããŒã¹ãšããé«åºŠãªäŸåé¢ä¿ç®¡çæ©èœãæäŸããŸãã
- ãªããžããªãµããŒã: Maven Central, Google’s Maven Repository (GMAVEN), JCenter (èªã¿åãå°çš), Ivyãªããžããªãªã©ãæšæºçãªãªããžããªãç°¡åã«å©çšã§ããŸãã瀟å ã®ãã©ã€ããŒããªããžããªãããŒã«ã«ãã¡ã€ã«ã·ã¹ãã äžã®ãªããžããªãèšå®å¯èœã§ãã
- æšç§»çäŸåé¢ä¿: ãããžã§ã¯ããäŸåããã©ã€ãã©ãªããããã«å¥ã®ã©ã€ãã©ãªã«äŸåããŠããå Žåããããã®ã©ã€ãã©ãªãèªåçã«è§£æ±ºããŠãããŸããããŒãžã§ã³ç«¶åãçºçããå Žåã¯ãããã©ã«ãã§ã¯ææ°ããŒãžã§ã³ãéžæããããªã©ã®è§£æ±ºæŠç¥ããããŸãã
- æè»ãªããŒãžã§ã³æå®:
'1.+'
ïŒææ°ã®1.xç³»ïŒã'[1.0, 2.0)'
ïŒ1.0以äž2.0æªæºïŒãšãã£ãããŒãžã§ã³ç¯å²æå®ããåžžã«ææ°ã®ã¹ãããã·ã§ããããªãªãŒã¹ãååŸããåçããŒãžã§ã³ãå©çšã§ããŸãïŒãã ãããã«ãã®åçŸæ§ãæãªãããå¯èœæ§ããããã泚æãå¿ èŠã§ãïŒã - äŸåé¢ä¿ã¹ã³ãŒã (Configuration): äŸåé¢ä¿ãã©ã®ã¯ã©ã¹ãã¹ïŒã³ã³ãã€ã«æãå®è¡æããã¹ãæãªã©ïŒã§å¿
èŠããæå®ãããã¹ã³ãŒããïŒGradleã§ã¯ConfigurationãšåŒã°ããïŒãå®çŸ©ã§ããŸããJavaãã©ã°ã€ã³ã§ã¯
implementation
,api
,compileOnly
,runtimeOnly
,testImplementation
ãªã©ãæäŸãããŸããapi
ãšimplementation
ã䜿ãåããããšã§ãã©ã€ãã©ãªã®APIå€æŽãåãŒã圱é¿ç¯å²ãå¶åŸ¡ããäžèŠãªåã³ã³ãã€ã«ãé²ãããšãã§ããŸãïŒç¹ã«ã©ã€ãã©ãªéçºã§éèŠïŒã - äŸåé¢ä¿ã®é€å€ãšåŒ·å¶: ç¹å®ã®æšç§»çäŸåé¢ä¿ãé€å€ããããç¹å®ã®ããŒãžã§ã³ã匷å¶çã«äœ¿çšãããããæ©èœããããŸãã
- BOM (Bill of Materials) ãµããŒã: è€æ°ã®ã©ã€ãã©ãªã®äºææ§ã®ããããŒãžã§ã³ã»ãããå®çŸ©ããBOMãã¡ã€ã«ãã€ã³ããŒãããããšã§ãé¢é£ã©ã€ãã©ãªã®ããŒãžã§ã³ç®¡çãç°¡çŽ åã§ããŸãïŒSpring Bootãªã©ã§ããå©çšãããŸãïŒã
5. è±å¯ãªãã©ã°ã€ã³ãšã³ã·ã¹ãã ð§©
Gradleã®ã³ã¢æ©èœã¯æ±çšçã§ãããç¹å®ã®çš®é¡ã®ãããžã§ã¯ãïŒJavaãAndroidãWebã¢ããªãªã©ïŒãç¹å®ã®ã¿ã¹ã¯ïŒã³ãŒãçæãéç解æããããã€ãªã©ïŒã«å¿ èŠãªæ©èœã¯ãäž»ã«ãã©ã°ã€ã³ã«ãã£ãŠæäŸãããŸãã
- ã³ã¢ãã©ã°ã€ã³: Gradleãã£ã¹ããªãã¥ãŒã·ã§ã³ã«å梱ãããŠããã
id 'java'
ã®ããã«IDãæå®ããã ãã§å©çšã§ããŸããJava, Groovy, Scala, War, Application, Baseãªã©ãå«ãŸããŸãã - ã³ãã¥ããã£ãã©ã°ã€ã³: Gradle Plugin Portal (https://plugins.gradle.org/) ã§å
¬éãããŠãããæ§ã
ãªãµãŒãããŒãã£è£œã®ãã©ã°ã€ã³ãèŠã€ãããŸããAndroid, Spring Boot, Micronaut, Quarkus, Docker, JaCoCo (ã«ãã¬ããž), Checkstyle, PMD ãªã©ãå€çš®å€æ§ãªãã©ã°ã€ã³ãååšããŸããå©çšããã«ã¯ã
plugins
ãããã¯ã«IDãšããŒãžã§ã³ãæå®ããŸãã - ã«ã¹ã¿ã ãã©ã°ã€ã³: ãããžã§ã¯ãåºæã®ãã«ãããžãã¯ãèŠçŽãè€æ°ã®ãããžã§ã¯ãã§åå©çšãããå Žåãç¬èªã®ãã©ã°ã€ã³ãäœæããããšãã§ããŸãã
buildSrc
ãã£ã¬ã¯ããªãç¬ç«ãããããžã§ã¯ããšããŠäœæããå ¬éããããšãå¯èœã§ãã
ãã©ã°ã€ã³ã¯ãèŠçŽã®èšå®ãã¿ã¹ã¯ã®è¿œå ãDSLã®æ¡åŒµãªã©ãè¡ããç¹å®ã®æè¡ããã¬ãŒã ã¯ãŒã¯åãã®ãã«ãèšå®ãåçã«ç°¡çŽ åããŸãã
6. æŽç·Žããããã«ããããžã§ã¯ããã«ããµããŒã ðïž
çŸä»£çãªãœãããŠã§ã¢ã¯ãè€æ°ã®ç¬ç«ããã¢ãžã¥ãŒã«ïŒãµããããžã§ã¯ãïŒããæ§æãããããšãäžè¬çã§ããGradleã¯ããã®ãããªãã«ããããžã§ã¯ãæ§æã®ãã«ããéåžžã«åŸæãšããŠããŸãã
- éå±€æ§é : ãããžã§ã¯ããã«ãŒããããžã§ã¯ããšè€æ°ã®ãµããããžã§ã¯ããšããéå±€æ§é ã§ç®¡çã§ããŸãã
- èšå®ã®ç¶æ¿ãšå
±æ: ã«ãŒããããžã§ã¯ãã®
build.gradle(.kts)
ã§å®çŸ©ããèšå®ïŒãã©ã°ã€ã³ããªããžããªãäŸåé¢ä¿ããŒãžã§ã³ãªã©ïŒããallprojects { ... }
ãsubprojects { ... }
ãããã¯ã䜿ã£ãŠå šãµããããžã§ã¯ããŸãã¯ç¹å®ã®ãµããããžã§ã¯ã矀ã«é©çšã§ããŸããããã«ãããèšå®ã®éè€ãé¿ããäžè²«æ§ãä¿ã€ããšãã§ããŸãã - ãããžã§ã¯ãéäŸå:
dependencies { implementation project(':other-subproject') }
ã®ããã«ããµããããžã§ã¯ãéã§ç°¡åã«äŸåé¢ä¿ãå®çŸ©ã§ããŸããGradleã¯äŸåé¢ä¿ã解決ããæ£ããé åºã§ãã«ããå®è¡ããŸãã - ã¿ã¹ã¯ã®éçŽãšéžæå®è¡: ã«ãŒããããžã§ã¯ããã
./gradlew build
ãå®è¡ããã°ãäŸåé¢ä¿ã«åºã¥ããŠå šãµããããžã§ã¯ãããã«ããããŸãããŸãã./gradlew :my-subproject:test
ã®ããã«ãç¹å®ã®ãµããããžã§ã¯ãã®ç¹å®ã®ã¿ã¹ã¯ã ããå®è¡ããããšãå¯èœã§ãã
ãããã®æ©èœã«ããã倧èŠæš¡ã§è€éãªã³ãŒãããŒã¹ãå¹ççã«ç®¡çã»ãã«ãããããšãã§ããŸãã
Gradle vs Ant/Maven æ¯èŒè¡š ð€
GradleãAntãMavenã¯ãããããJavaãšã³ã·ã¹ãã ã§åºã䜿ãããŠãããã«ãããŒã«ã§ãããããããã«ç¹åŸŽãšèšèšææ³ã®éãããããŸããã©ã®ããŒã«ãæé©ãã¯ãããžã§ã¯ãã®èŠä»¶ãããŒã ã®çµéšã«ãã£ãŠç°ãªããŸããã以äžã®æ¯èŒè¡šã§äž»ãªéããç解ããŸãããã
ç¹åŸŽ | Apache Ant | Apache Maven | Gradle |
---|---|---|---|
èšå®èšè¿°åœ¢åŒ | XML (build.xml ) |
XML (pom.xml ) |
Groovy / Kotlin DSL (build.gradle / build.gradle.kts ) |
ãã«ãã®èãæ¹ | åœä»€ç (ã¿ã¹ã¯ããŒã¹) | 宣èšç (ã©ã€ããµã€ã¯ã«ããŒã¹) | 宣èšç + åœä»€ç (ã¿ã¹ã¯ã°ã©ãããŒã¹) |
æè»æ§ | éåžžã«é«ã (èªç±åºŠãé«ã) | äœã (èŠçŽéèŠ) | éåžžã«é«ã (ã¹ã¯ãªããèšèª) |
èŠçŽ (Convention) | ã»ãŒç¡ã | 匷ã (èšå®ããèŠçŽ) | ãã (ãã©ã°ã€ã³ã«ãã) / ã«ã¹ã¿ãã€ãºå®¹æ |
äŸåé¢ä¿ç®¡ç | æšæºã§ã¯ç¡ã (Ivyçãå¿ èŠ) | æšæºæ©èœ (匷å) | æšæºæ©èœ (é«æ©èœã»æè») |
ãã«ãããã©ãŒãã³ã¹ | æ®é | æ®éãããé ã | é«ã (ãã£ãã·ã¥ãããŒã¢ã³ç) |
ãã«ããããžã§ã¯ã | å¯èœã ãè€éã«ãªããã¡ | æšæºãµããŒã (芪åé¢ä¿) | æšæºãµããŒã (æè»ã»é«æ©èœ) |
åŠç¿ã³ã¹ã | äœããäžçšåºŠ | äžçšåºŠ (ã©ã€ããµã€ã¯ã«çã®æŠå¿µ) | äžãé«çšåºŠ (DSLãAPIãæŠå¿µ) |
ã«ã¹ã¿ãã€ãºæ§ | é«ã (ã«ã¹ã¿ã ã¿ã¹ã¯å®¹æ) | äœã (ãã©ã°ã€ã³éçºãå¿ èŠ) | éåžžã«é«ã (ã¹ã¯ãªããå ã§çŽæ¥èšè¿°å¯èœ) |
äž»ãªçšé/æ¡çšäŸ | ã¬ã¬ã·ãŒãããžã§ã¯ããç¹æ®ãªãã«ã | å€ãã®Javaãããžã§ã¯ããäŒæ¥æšæº | AndroidãSpring Bootã倧èŠæš¡ãããžã§ã¯ããã¢ãã³éçº |
ãªãªãŒã¹å¹Ž (v1.0) | 2000幎é | 2004幎é | 2012幎 |
éžæã®ãã€ã³ã:
- Ant: æ¢åã®Antãã«ããä¿å®ããå Žåããéåžžã«ç¹æ®ã§èŠçŽã«çžãããããªããã«ãããã»ã¹ãå¿ èŠãªéå®çãªã±ãŒã¹ãæ°èŠãããžã§ã¯ãã§ã®æ¡çšã¯çšã
- Maven: æšæºçãªJavaãããžã§ã¯ãã§ãèŠçŽã«åŸãããšã§ã·ã³ãã«ããä¿ã¡ããå ŽåãããŒã ã¡ã³ããŒãMavenã«æ £ããŠããå Žåãå€ãã®äŒæ¥ã§æšæºãšããŠäœ¿ãããŠããå®çžŸãããã
- Gradle: Androidéçºã§ã¯å¿ é ã æè»ãªã«ã¹ã¿ãã€ãºãå¿ èŠãªå Žåããã«ãããã©ãŒãã³ã¹ãéèŠããå ŽåãKotlin DSLã®æ©æµãåãããå Žåãã¢ãã³ãªéçºãã©ã¯ãã£ã¹ãåãå ¥ãããå ŽåãåŠç¿æ欲ãããã°ãæã匷åã§å°æ¥æ§ã®ããéžæè¢ãšèšããã
2021幎ã®Javaéçºè 調æ»ã§ã¯MavenãäŸç¶ãšããŠæãå©çšãããŠãããšããããŒã¿ããããŸãããGradleã®æ¡çšã¯çå®ã«å¢å ããŠãããç¹ã«æ°ãããããžã§ã¯ããããã©ãŒãã³ã¹ãéèŠãªãããžã§ã¯ãã§ã®éžæè¢ãšããŠæåã§ãã
Gradleã®ã€ã³ã¹ããŒã« ð»
Gradleã䜿ã£ãŠãã«ããå§ããã«ã¯ããŸãGradleãå®è¡ã§ããç°å¢ãæŽããå¿ èŠããããŸãããã ããåŸè¿°ããGradle Wrapperã䜿ãå Žåãéçºè åèªã®ãã·ã³ã«å¿ ãããGradleãäºåã«ã€ã³ã¹ããŒã«ããå¿ èŠã¯ãããŸãããããã¯Gradleã®å€§ããªå©ç¹ã®äžã€ã§ãã
ããã§ããGradleãããžã§ã¯ããæ°èŠã«äœæããããããŒã«ã«ã§Gradleã³ãã³ããçŽæ¥äœ¿ãããå Žåã®ããã«ãã€ã³ã¹ããŒã«æ¹æ³ã説æããŸãã
åææ¡ä»¶:
- Java Development Kit (JDK): Gradleãå®è¡ããã«ã¯ãJDKãå¿ èŠã§ããGradleã®ããŒãžã§ã³ã«ãã£ãŠèŠæ±ãããJDKã®æäœããŒãžã§ã³ã¯ç°ãªããŸãããçŸåšã®Gradle 8.xã§ã¯JDK 8以äžãå¿ èŠã§ããææ°ã®LTSçJDKïŒäŸ: JDK 17, JDK 21ïŒã®å©çšãæšå¥šãããŸãã
ãŸããJDKãã€ã³ã¹ããŒã«ãããPATH
ãéã£ãŠããããšã確èªããŸãããã
java -version
javac -version
1. ããã±ãŒãžãããŒãžã£ãŒã䜿çšãã (æšå¥š)
ã䜿ãã®OSã«å¯Ÿå¿ããããã±ãŒãžãããŒãžã£ãŒãå©çšããã®ããã€ã³ã¹ããŒã«ãšããŒãžã§ã³ç®¡çã®èŠ³ç¹ããæãç°¡åã§æšå¥šãããæ¹æ³ã§ãã
-
SDKMAN! (Linux, macOS, WSL, Cygwinãªã©): Javaãšã³ã·ã¹ãã ã®æ§ã
ãªSDKïŒJDK, Gradle, Maven, Kotlin, Groovyãªã©ïŒã®è€æ°ããŒãžã§ã³ãç°¡åã«åãæ¿ããŠç®¡çã§ããããŒã«ã§ããGradleå
¬åŒã§ãæšå¥šãããŠããŸãã
# SDKMAN!ã®ã€ã³ã¹ããŒã« (æªå°å ¥ã®å Žå) curl -s "https://get.sdkman.io" | bash source "$HOME/.sdkman/bin/sdkman-init.sh" # å©çšå¯èœãªGradleã®ããŒãžã§ã³ãç¢ºèª sdk list gradle # ææ°ã®å®å®çGradleãã€ã³ã¹ããŒã« sdk install gradle # ç¹å®ã®ããŒãžã§ã³ãã€ã³ã¹ããŒã« (äŸ: ææ°ç 8.13) sdk install gradle 8.13 # 䜿çšããGradleã®ããŒãžã§ã³ãåãæ¿ã sdk use gradle 8.13 # ããã©ã«ãã§äœ¿çšããããŒãžã§ã³ãèšå® sdk default gradle 8.13
-
Homebrew (macOS): macOSãŠãŒã¶ãŒã«äººæ°ã®ããã±ãŒãžãããŒãžã£ãŒã§ãã
brew install gradle
Homebrewã§ã€ã³ã¹ããŒã«ããå Žåã
brew upgrade gradle
ã§ææ°çã«æŽæ°ã§ããŸãã -
Scoop (Windows): Windowsåãã®ã³ãã³ãã©ã€ã³ã€ã³ã¹ããŒã©ãŒã§ãã
scoop install gradle
-
Chocolatey (Windows): Windowsåãã®ããã±ãŒãžãããŒãžã£ãŒã§ãã
choco install gradle
泚æ: Ubuntuã®apt
ãCentOSã®yum
ã®ãããªLinuxãã£ã¹ããªãã¥ãŒã·ã§ã³æšæºã®ããã±ãŒãžãããŒãžã£ãŒã§ãGradleãæäŸãããŠããå ŽåããããŸãããããŒãžã§ã³ãå€ãã£ãããå
¬åŒãšã¯ç°ãªãå€æŽãå ããããŠãããããå¯èœæ§ããããŸããäºææ§ã®åé¡ãææ°æ©èœã®å©çšãèæ
®ãããšãSDKMAN!ã®äœ¿çšã匷ãæšå¥šãããŸãã
2. æåã§ã€ã³ã¹ããŒã«ãã
ããã±ãŒãžãããŒãžã£ãŒã䜿çšããªãå Žåã¯ãGradleå ¬åŒãµã€ãããçŽæ¥ããŠã³ããŒãããŠæåã§èšå®ããŸãã
-
Gradleãã£ã¹ããªãã¥ãŒã·ã§ã³ã®ããŠã³ããŒã:
Gradleã®å
¬åŒãªãªãŒã¹_ããŒãžã«ã¢ã¯ã»ã¹ããã€ã³ã¹ããŒã«ãããããŒãžã§ã³ã®Gradleãæ¢ããŸããéåžžã¯ææ°ã®å®å®ç (Stable) ãéžæããŸãã
æ¬èšäºå·çæç¹ã§ã®ææ°å®å®ç㯠8.13 (2025幎2æ25æ¥ãªãªãŒã¹) ã§ãã
ããŠã³ããŒããããã¡ã€ã«ã«ã¯2çš®é¡ãããŸã:binary-only.zip
: Gradleã®å®è¡ã«å¿ èŠãªãã¡ã€ã«ã®ã¿ãå«ãŸããŸããéåžžã¯ãã¡ãã§ååã§ããcomplete.zip
: å®è¡ãã¡ã€ã«ã«å ããŠãããã¥ã¡ã³ãïŒUser Manual, DSL ReferenceïŒãšãœãŒã¹ã³ãŒããå«ãŸããŸãããªãã©ã€ã³ã§ããã¥ã¡ã³ããåç §ãããå Žåãªã©ã«éžæããŸãã
binary-only.zip
(ãŸãã¯complete.zip
) ãããŠã³ããŒãããŸãã -
ZIPãã¡ã€ã«ã®å±é:
ããŠã³ããŒãããZIPãã¡ã€ã«ããGradleãã€ã³ã¹ããŒã«ãããä»»æã®ãã£ã¬ã¯ããªã«å±éïŒè§£åïŒããŸãããã®ãã£ã¬ã¯ããªã¯ããŠãŒã¶ãŒãæžã蟌ã¿æš©éãæã€å ŽæãæãŸããã§ãïŒäŸ: ããŒã ãã£ã¬ã¯ããªé
äžã
/usr/local/
,C:\Gradle
ãªã©ïŒã# Linux / macOS ã®äŸ ( /usr/local/gradle ã«å±éããå Žå) sudo mkdir -p /usr/local/gradle sudo unzip gradle-8.13-bin.zip -d /usr/local/gradle/
ããã«ããã# Windows (PowerShell) ã®äŸ ( C:\Gradle ã«å±éããå Žå) New-Item -ItemType Directory -Force -Path C:\Gradle Expand-Archive .\gradle-8.13-bin.zip C:\Gradle\
/usr/local/gradle/gradle-8.13/
ãC:\Gradle\gradle-8.13\
ã®ãããªãã£ã¬ã¯ããªãäœæãããŸãããã®ãã¹ããGradleããŒã ãã£ã¬ã¯ããªããšåŒã³ãŸãã -
ç°å¢å€æ°ã®èšå®:
ã³ãã³ãã©ã€ã³ãã
gradle
ã³ãã³ããå®è¡ã§ããããã«ãGradleããŒã ãã£ã¬ã¯ããªå ã®bin
ãã£ã¬ã¯ããªãã·ã¹ãã ã®PATH
ç°å¢å€æ°ã«è¿œå ããŸãããŸããGRADLE_HOME
ç°å¢å€æ°ãèšå®ããŠãããšãä»ã®ããŒã«ãšã®é£æºã«åœ¹ç«ã€å ŽåããããŸãã-
Linux / macOS:
ã䜿ãã®ã·ã§ã«ã®èšå®ãã¡ã€ã«ïŒäŸ:
~/.bashrc
,~/.zshrc
,~/.profile
ïŒã«ä»¥äžã®è¡ãè¿œå ããŸãããã¹ã¯å®éã«å±éããå Žæã«åãããŠãã ããã
ãã¡ã€ã«ãä¿åããåŸãèšå®ãåæ ãããããã«# GradleããŒã ãã£ã¬ã¯ããªãèšå® export GRADLE_HOME=/usr/local/gradle/gradle-8.13 # PATHã«Gradleã®binãã£ã¬ã¯ããªãè¿œå export PATH=$PATH:$GRADLE_HOME/bin
source ~/.bashrc
ïŒãã¡ã€ã«åã«åãããŠïŒãå®è¡ããããæ°ããã¿ãŒããã«ãŠã£ã³ããŠãéããŸãã -
Windows:
- ãã·ã¹ãã ç°å¢å€æ°ã®ç·šéããæ€çŽ¢ããŠéããŸãã
- ãç°å¢å€æ°…ããã¿ã³ãã¯ãªãã¯ããŸãã
- ãã·ã¹ãã ç°å¢å€æ°ãã»ã¯ã·ã§ã³ã§ãæ°èŠ…ããã¯ãªãã¯ããŸãã
- å€æ°åã«
GRADLE_HOME
ãå€æ°å€ã«Gradleãå±éãããã£ã¬ã¯ããªãã¹ïŒäŸ:C:\Gradle\gradle-8.13
ïŒãå ¥åãããOKããã¯ãªãã¯ããŸãã - ãã·ã¹ãã ç°å¢å€æ°ãã®ãªã¹ããã
Path
å€æ°ãéžæãããç·šé…ããã¯ãªãã¯ããŸãã - ãæ°èŠããã¯ãªãã¯ãã
%GRADLE_HOME%\bin
ãšå ¥åããŸããïŒ%ã§å²ãããšã§ãå ã»ã©èšå®ããGRADLE_HOMEå€æ°ãåç §ããŸãïŒ - ãOKããç¹°ãè¿ãã¯ãªãã¯ããŠããã¹ãŠã®ãã€ã¢ãã°ããã¯ã¹ãéããŸãã
- èšå®ãæå¹ã«ããããã«ãéããŠããã³ãã³ãããã³ãããPowerShellãŠã£ã³ããŠããã¹ãŠéããæ°ããéããŸãã
-
Linux / macOS:
ã䜿ãã®ã·ã§ã«ã®èšå®ãã¡ã€ã«ïŒäŸ:
ã€ã³ã¹ããŒã«ã®ç¢ºèª
æ£ããã€ã³ã¹ããŒã«ãããç°å¢å€æ°ãèšå®ããããã確èªããããã«ãæ°ããã¿ãŒããã«ãŸãã¯ã³ãã³ãããã³ãããéãã以äžã®ã³ãã³ããå®è¡ããŸãã
gradle -v
# ãŸãã¯
gradle --version
以äžã®ããã«ãã€ã³ã¹ããŒã«ããGradleã®ããŒãžã§ã³ãKotlin DSLãGroovy DSLã®ããŒãžã§ã³ãJVMã®ããŒãžã§ã³ãªã©ã衚瀺ãããã°æåã§ããð
------------------------------------------------------------
Gradle 8.13
------------------------------------------------------------
Build time: 2025-02-25 14:13:08 UTC
Revision: ...
Kotlin: 1.9.22
Groovy: 3.0.17
Ant: Apache Ant(TM) version 1.10.13 compiled on January 4 2023
JVM: 17.0.10 (Eclipse Adoptium 17.0.10+7)
OS: Mac OS X 14.2 aarch64 (äŸ)
ããã§ãgradle
ã³ãã³ãã䜿ã£ãŠGradleãããžã§ã¯ããæäœããæºåãæŽããŸãããããããç¹°ãè¿ãã«ãªããŸãããæ¢åã®Gradleãããžã§ã¯ãã§äœæ¥ããå Žåã¯ã次ã«èª¬æããGradle Wrapper (gradlew
) ã䜿ãã®ãäžè¬çã§ãã
åºæ¬çãªäœ¿ãæ¹ ð ïž
Gradleã®ã€ã³ã¹ããŒã«ïŒãŸãã¯Gradle Wrapperã®æºåïŒãã§ããããããããGradleã䜿ã£ãŠã¿ãŸããããåºæ¬çãªãããžã§ã¯ãã®äœæãããã«ããã¿ã¹ã¯ã®å®è¡ãŸã§ãäžé£ã®æµãã解説ããŸãã
1. ãããžã§ã¯ãã®åæå: `gradle init`
æ°ããGradleãããžã§ã¯ããå§ããæãç°¡åãªæ¹æ³ã¯ãgradle init
ã³ãã³ãã䜿ãããšã§ããããã¯ã察話çã«ãããžã§ã¯ãã®æ§æïŒã¢ããªã±ãŒã·ã§ã³ãã©ã€ãã©ãªãã䜿çšèšèªããã«ãã¹ã¯ãªããDSLãªã©ïŒã質åããåºæ¬çãªãã£ã¬ã¯ããªæ§é ãšå¿
èŠãªèšå®ãã¡ã€ã«ãçæããŠããã䟿å©ãªã³ãã³ãã§ãã
空ã®ãã£ã¬ã¯ããªãäœæãããã®äžã§ã³ãã³ããå®è¡ããŸãã
mkdir my-first-gradle-app
cd my-first-gradle-app
gradle init
ãããšã以äžã®ãããªéžæè¢ã衚瀺ãããŸãïŒããŒãžã§ã³ã«ããå€å°ç°ãªããŸãïŒã
Select type of project to generate:
1: basic (åºæ¬çãªGradleãããžã§ã¯ãæ§é ã®ã¿)
2: application (å®è¡å¯èœãªã¢ããªã±ãŒã·ã§ã³)
3: library (åå©çšå¯èœãªã©ã€ãã©ãª)
4: Gradle plugin (Gradleãã©ã°ã€ã³éçºçš)
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 <-- Javaãéžæ
Generate multiple subprojects for application? (default: no) [yes, no] no <-- åäžãããžã§ã¯ããéžæ
Select build script DSL:
1: Groovy
2: Kotlin
Enter selection (default: Groovy) [1..2] 1 <-- ãã«ãã¹ã¯ãªããã«Groovyãéžæ
Project name (default: my-first-gradle-app):
Enter project name (default: my-first-gradle-app) <-- Enterã§ããã©ã«ãå€ã䜿çš
Source package (default: my.first.gradle.app): com.example
Enter source package (default: com.example) <-- ããã±ãŒãžåãå
¥å
> Task :init
Get more help with your project: https://docs.gradle.org/current/userguide/building_java_projects.html
BUILD SUCCESSFUL in ...s
ããã«ãããéžæããæ§æã«åºã¥ãããããžã§ã¯ããã¡ã€ã«çŸ€ãçæãããŸããäž»ãªãã®ã以äžã«ç€ºããŸãã
- ð
gradlew
,gradlew.bat
: âšGradle Wrapper ã¹ã¯ãªãããä»åŸã¯gradle
ã³ãã³ãã®ä»£ããã«ãããã䜿ããŸãã - ð
gradle/
: Gradle Wrapperã®èšå®ïŒäœ¿çšããGradleã®ããŒãžã§ã³ãªã©ïŒãæ ŒçŽãããã£ã¬ã¯ããªã - ð
settings.gradle
(ãŸãã¯settings.gradle.kts
): ãããžã§ã¯ãã®ååãããã«ããããžã§ã¯ãæ§æã®å Žåã«ã©ã®ãµããããžã§ã¯ããå«ãŸããããå®çŸ©ããèšå®ãã¡ã€ã«ã - ð
build.gradle
(ãŸãã¯build.gradle.kts
): âïž ãã®ãããžã§ã¯ãã®ãã«ãæ¹æ³ãå®çŸ©ããã¡ã€ã³ã®ãã«ãã¹ã¯ãªããããã©ã°ã€ã³ã®é©çšãäŸåé¢ä¿ã®è¿œå ãã¿ã¹ã¯ã®å®çŸ©ãªã©ãè¡ããŸãã - ð
src/
: ãœãŒã¹ã³ãŒãããªãœãŒã¹ãã¡ã€ã«ãé 眮ãããã£ã¬ã¯ããªãJavaã¢ããªã±ãŒã·ã§ã³ã®å Žåãsrc/main/java
(æ¬çªã³ãŒã),src/main/resources
(ãªãœãŒã¹),src/test/java
(ãã¹ãã³ãŒã),src/test/resources
(ãã¹ãçšãªãœãŒã¹) ãšãã£ãæšæºçãªæ§é ãäœãããŸãã
ð ãã€ã³ã: gradle init
ã§ãããžã§ã¯ããäœæãããšãGradle Wrapperãèªåçã«ã»ããã¢ãããããŸããããã«ãããä»ã®éçºè
ãCIç°å¢ã¯Gradleãå¥éã€ã³ã¹ããŒã«ããããšãªãã./gradlew
(Linux/macOS) ãŸã㯠gradlew.bat
(Windows) ã³ãã³ãã䜿ã£ãŠããããžã§ã¯ãã§æå®ãããããŒãžã§ã³ã®Gradleã§ãã«ããå®è¡ã§ããŸãã
2. ãã«ãã¹ã¯ãªããã®æ§é (`build.gradle`)
gradle init
ã§çæããã build.gradle
(Groovy DSLã®å Žå) ã®äžèº«ãèŠãŠã¿ãŸããããããã¯Gradleãããžã§ã¯ããç解ããäžã§äžå¿ãšãªããã¡ã€ã«ã§ãã
/*
* This file was generated by the Gradle 'init' task.
*/
plugins {
// applicationãã©ã°ã€ã³ãé©çšããJavaã¢ããªã±ãŒã·ã§ã³æ§ç¯ã®ãµããŒããè¿œå
id 'application'
}
repositories {
// äŸåé¢ä¿ã解決ããããã«Maven Centralãªããžããªã䜿çš
mavenCentral()
}
dependencies {
// ãã¹ãã«ã¯JUnit Jupiterã䜿çš
testImplementation libs.junit.jupiter // ããŒãžã§ã³ã¯gradle/libs.versions.tomlã§ç®¡ç(åŸè¿°)
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
// ã¢ããªã±ãŒã·ã§ã³ã§äœ¿çšãããäŸåé¢ä¿ (äŸ: Guavaã©ã€ãã©ãª)
implementation libs.guava // ããŒãžã§ã³ã¯gradle/libs.versions.tomlã§ç®¡ç
}
// Java Toolchainãé©çšãããã«ãã®ä¿¡é Œæ§ãåäžããã
java {
toolchain {
languageVersion = JavaLanguageVersion.of(17) // ãã«ãã«äœ¿çšããJavaããŒãžã§ã³ãæå®
}
}
application {
// ã¢ããªã±ãŒã·ã§ã³ã®ã¡ã€ã³ã¯ã©ã¹ãå®çŸ©
mainClass = 'com.example.App'
}
tasks.named('test') {
// ãŠããããã¹ãã«JUnit Platformã䜿çš
useJUnitPlatform()
}
// Gradle 7.0以éã§æšå¥šãããããŒãžã§ã³ã«ã¿ãã°æ©èœ (çæãããå Žå)
// gradle/libs.versions.toml ãã¡ã€ã«ã§äŸåé¢ä¿ã®ããŒãžã§ã³ãäžå
管ç
// (libs.junit.jupiter ã libs.guava ã¯ãã®ã«ã¿ãã°ãåç
§ããŠãã)
äž»ãªæ§æèŠçŽ :
plugins { ... }
ãããã¯: ãããžã§ã¯ãã§äœ¿çšããGradleãã©ã°ã€ã³ãæå®ããŸãããã©ã°ã€ã³ã¯Gradleã«æ°ããæ©èœïŒã¿ã¹ã¯ãèšå®é ç®ãèŠçŽãªã©ïŒãè¿œå ããŸããããã§ã¯application
ãã©ã°ã€ã³ãé©çšããŠãããããã«ããJavaã¢ããªã±ãŒã·ã§ã³ã®ã³ã³ãã€ã«ããã¹ããå®è¡ãããã±ãŒãžã³ã°ãªã©ã®ã¿ã¹ã¯ãå©çšå¯èœã«ãªããŸããrepositories { ... }
ãããã¯: ãããžã§ã¯ããäŸåããå€éšã©ã€ãã©ãªïŒJARãã¡ã€ã«ãªã©ïŒãæ¢ãã«è¡ãå ŽæïŒãªããžããªïŒãæå®ããŸããmavenCentral()
ã¯ãJavaã³ãã¥ããã£ã§æãåºã䜿ãããŠããå ¬éãªããžããªã§ããä»ã«ãgoogle()
(Androidéçºã§å€çš),mavenLocal()
(ããŒã«ã«ã®Mavenãã£ãã·ã¥), ãã©ã€ããŒããªããžããªãªã©ãæå®ã§ããŸããdependencies { ... }
ãããã¯: ãããžã§ã¯ããå¿ èŠãšããå€éšã©ã€ãã©ãªãä»ã®ã¢ãžã¥ãŒã«ïŒãã«ããããžã§ã¯ãã®å ŽåïŒãæå®ããŸããimplementation
,testImplementation
ãªã©ã¯ãäŸåé¢ä¿ã¹ã³ãŒã (Configuration)ããšåŒã°ãããã®äŸåé¢ä¿ãã©ã®ã¯ã©ã¹ãã¹ïŒæ¬çªã³ãŒãã®ã³ã³ãã€ã«æãå®è¡æããã¹ãã³ãŒãã®ã³ã³ãã€ã«æãªã©ïŒã§å¿ èŠãã瀺ããŸããjava { ... }
ãããã¯: Javaãã©ã°ã€ã³ã«é¢ããèšå®ãè¡ããŸããããã§ã¯toolchain
ã䜿ã£ãŠããã®ãããžã§ã¯ãã®ãã«ãã«äœ¿çšããJDKã®ããŒãžã§ã³ãæ瀺çã«æå®ããŠããŸããããã«ãããéçºè ã®ããŒã«ã«ç°å¢ã«ã€ã³ã¹ããŒã«ãããŠããJDKããŒãžã§ã³ã«é¢ããããæå®ãããããŒãžã§ã³ã§ãã«ããå®è¡ãããç°å¢äŸåã®åé¡ãé²ãããšãã§ããŸããapplication { ... }
ãããã¯:application
ãã©ã°ã€ã³ãæäŸããèšå®ã§ããmainClass
ã§ã./gradlew run
ã³ãã³ãã§å®è¡ãããã¯ã©ã¹ãæå®ããŸããtasks.named('taskName') { ... }
: ç¹å®ã®ã¿ã¹ã¯ïŒããã§ã¯test
ã¿ã¹ã¯ïŒã®èšå®ãã«ã¹ã¿ãã€ãºããŸããuseJUnitPlatform()
ã¯ããã¹ãã©ã³ããŒãšããŠJUnit 5 (Jupiter) ã䜿çšããããšãæå®ããŸãã- ããŒãžã§ã³ã«ã¿ãã° (Version Catalog): Gradle 7.0ããå°å
¥ãããæ©èœã§ãäŸåé¢ä¿ã®ããŒãžã§ã³çªå·ã
gradle/libs.versions.toml
ãšããå¥ãã¡ã€ã«ã§äžå 管çããŸãããã«ãã¹ã¯ãªããå ã§ã¯libs.ã©ã€ãã©ãªå
ã®ãããªã¿ã€ãã»ãŒããªåœ¢åŒã§åç §ã§ããããŒãžã§ã³ç®¡çã容æã«ãªãããã«ãã¹ã¯ãªããããã£ããããŸããgradle init
ã§çæããããããžã§ã¯ãã§ã¯ããã®æ©èœãå©çšãããããšãå€ãã§ãã
3. ã¿ã¹ã¯ (Tasks) ã®å®è¡
Gradleã®ãã«ãã¯ãã¿ã¹ã¯ (Task) ãšåŒã°ããç¬ç«ããäœæ¥åäœãå®è¡ããããšã§é²è¡ããŸããã³ã³ãã€ã«ããã¹ããJARäœæãããã¥ã¡ã³ãçæãã³ãŒããã§ãã¯ãã¢ããªã±ãŒã·ã§ã³å®è¡ãªã©ããã«ãããã»ã¹ã«ãããå ·äœçãªã¢ã¯ã·ã§ã³ã¯ãã¹ãŠã¿ã¹ã¯ãšããŠè¡šçŸãããŸãã
ãããžã§ã¯ãã§å©çšå¯èœãªäž»ãªã¿ã¹ã¯ã¯ã以äžã®ã³ãã³ãã§ç¢ºèªã§ããŸãã
./gradlew tasks
ãã詳现ãªã¿ã¹ã¯ïŒäŸåã¿ã¹ã¯ãªã©ïŒãå«ããŠãã¹ãŠè¡šç€ºããã«ã¯:
./gradlew tasks --all
ã¿ã¹ã¯ãå®è¡ããã«ã¯ãGradle Wrapperã¹ã¯ãªããã«ç¶ããŠã¿ã¹ã¯åãæå®ããŸãã
# JavaãœãŒã¹ã³ãŒããã³ã³ãã€ã«ãã (compileJavaã¿ã¹ã¯)
./gradlew compileJava
# ãã¹ãã³ãŒããã³ã³ãã€ã«ãã (compileTestJavaã¿ã¹ã¯)
./gradlew compileTestJava
# ãã¹ãŠã®ã¯ã©ã¹ãã¡ã€ã«ãåé€ãã (cleanã¿ã¹ã¯)
./gradlew clean
# ãŠããããã¹ããå®è¡ãã (testã¿ã¹ã¯)
./gradlew test
# å®è¡å¯èœãªJARãã¡ã€ã«ãå«ããé
åžçšã®ã¢ãŒã«ã€ããäœæãã (assembleã¿ã¹ã¯)
# éåžžãcompileJava, processResources, classes, jar/war ãªã©ã«äŸå
./gradlew assemble
# ã³ã³ãã€ã«ããã¹ããã¢ãŒã«ã€ãäœæãªã©ãäž»èŠãªãã«ãããã»ã¹ãäžéãå®è¡ (buildã¿ã¹ã¯)
# éåžžãassembleãšcheck (testãªã©) ã¿ã¹ã¯ã«äŸå
./gradlew build
# ã¢ããªã±ãŒã·ã§ã³ãå®è¡ãã (runã¿ã¹ã¯ - applicationãã©ã°ã€ã³ãå¿
èŠ)
./gradlew run
# è€æ°ã®ã¿ã¹ã¯ãé çªã«å®è¡ (äŸ: ã¯ãªãŒã³ããŠãããã«ã)
./gradlew clean build
ã¿ã¹ã¯ã¯äŸåé¢ä¿ãæã€ããšãã§ããŸããäŸãã°ãbuild
ã¿ã¹ã¯ã¯assemble
ã¿ã¹ã¯ãšcheck
ã¿ã¹ã¯ã«äŸåããassemble
ã¿ã¹ã¯ã¯jar
ã¿ã¹ã¯ã«äŸåããjar
ã¿ã¹ã¯ã¯classes
ã¿ã¹ã¯ã«äŸåããclasses
ã¿ã¹ã¯ã¯compileJava
ã¿ã¹ã¯ãšprocessResources
ã¿ã¹ã¯ã«äŸåããããšãã£ãå
·åã§ããGradleã¯ãããã®äŸåé¢ä¿ã解決ããå¿
èŠãªã¿ã¹ã¯ãæ£ããé åºã§å®è¡ããŸãïŒãããã¿ã¹ã¯ã°ã©ããšåŒã³ãŸãïŒã
ã«ã¹ã¿ã ã¿ã¹ã¯ã®å®çŸ©
build.gradle
å
ã§ç¬èªã®ã¿ã¹ã¯ãå®çŸ©ããããšãéåžžã«ç°¡åã§ãã
// build.gradle (Groovy DSL)
task hello {
group = "Custom Tasks" // tasksã³ãã³ãã§ã®è¡šç€ºã°ã«ãŒã
description = "Prints a friendly greeting." // tasksã³ãã³ãã§ã®èª¬æ
doLast { // ã¿ã¹ã¯ã®å®è¡ã¢ã¯ã·ã§ã³ãå®çŸ©
println 'Hello from my custom Gradle task! ð'
}
}
// æ¢åã®ã¿ã¹ã¯ã«äŸåãããããšãå¯èœ
task customBuild(dependsOn: ['hello', 'build']) {
group = "Custom Tasks"
description = "Runs hello task and then the standard build."
doLast {
println "Custom build finished!"
}
}
# ã«ã¹ã¿ã ã¿ã¹ã¯ãå®è¡
./gradlew hello
./gradlew customBuild
4. Gradle Wrapper (gradlew) ã®éèŠæ§ (åæ²) ð
åè¿°ã®éããGradleãããžã§ã¯ãã§ã¯gradle
ã³ãã³ãã§ã¯ãªãããããžã§ã¯ãã«ãŒãã«ãã./gradlew
(Linux/macOS) ãŸã㯠gradlew.bat
(Windows) ã䜿ãããšã匷ãæšå¥šãããŸãã
äž»ãªçç±:
- ãã«ãã®åçŸæ§: ãããžã§ã¯ãããšã«æå®ãããç¹å®ã®GradleããŒãžã§ã³ã䜿çšãããããéçºè éãCI/CDç°å¢ã§ã®ãã«ãçµæã®äžè²«æ§ãä¿ãããŸãããç§ã®ç°å¢ã§ã¯åãã®ã«âŠããšããåé¡ãé²ããŸãã
- ã€ã³ã¹ããŒã«ã®æéäžèŠ: ãããžã§ã¯ãããã§ãã¯ã¢ãŠããã人ã¯ãèªåã®ãã·ã³ã«Gradleãå¥éã€ã³ã¹ããŒã«ããå¿
èŠããããŸããã
./gradlew
ãå®è¡ããã°ãå¿ èŠãªããŒãžã§ã³ã®Gradleãèªåçã«ããŠã³ããŒããããå©çšãããŸãã - ç°¡åãªããŒãžã§ã³ç®¡ç: ãããžã§ã¯ãã§äœ¿çšããGradleã®ããŒãžã§ã³ãã¢ããã°ã¬ãŒããããå Žåã以äžã®ã³ãã³ããå®è¡ããã ãã§
gradle/wrapper/gradle-wrapper.properties
ãã¡ã€ã«ãšWrapperã¹ã¯ãªãããæŽæ°ãããŸãã
ãã®å€æŽãããŒãžã§ã³ç®¡çã·ã¹ãã ïŒGitãªã©ïŒã«ã³ãããããã°ãããŒã ã¡ã³ããŒå šå¡ã次ã«# äŸ: GradleãããŒãžã§ã³ 8.13 ã«ã¢ããã°ã¬ãŒããã ./gradlew wrapper --gradle-version 8.13
./gradlew
ãå®è¡ããéã«æ°ããããŒãžã§ã³ã䜿ãããã«ãªããŸãã
ð¡ åžžã« ./gradlew <ã¿ã¹ã¯å>
ã䜿ãç¿æ
£ãã€ããŸãããïŒ
ããã§Gradleãããžã§ã¯ãã®åºæ¬çãªäœæããã«ãã¹ã¯ãªããã®æ§é ç解ãã¿ã¹ã¯ã®å®è¡æ¹æ³ãããããŸããã次ã¯ãGradleãããã«åŒ·åã«ãããã©ã°ã€ã³ã®äžçãèŠãŠãããŸãããã
äž»èŠãªGradleãã©ã°ã€ã³ ð
Gradleã®åŒ·åãªæ©èœã®å€ãã¯ãç¹å®ã®ç®çã®ããã«äœããããã©ã°ã€ã³ã«ãã£ãŠæäŸãããŸãããã©ã°ã€ã³ã¯ãæ°ããã¿ã¹ã¯ãè¿œå ããããèŠçŽïŒãã£ã¬ã¯ããªæ§é ãªã©ïŒãèšå®ãããããã«ãã¹ã¯ãªããã§äœ¿ããæ°ããèšå®é ç®ïŒDSLïŒãå°å ¥ãããããŸããããã§ã¯ãJavaãšã³ã·ã¹ãã ã§ç¹ã«ãã䜿ãããåºæ¬çãªãã©ã°ã€ã³ã玹ä»ããŸãã
ãã©ã°ã€ã³ã¯éåžžããã«ãã¹ã¯ãªããã®å
é ã«ãã plugins { ... }
ãããã¯ã§é©çšããŸãã
1. Base Plugin (`base`)
- ç®ç: ä»ã®å€ãã®ãã©ã°ã€ã³ã®åºç€ãšãªããæãåºæ¬çãªæ©èœãæäŸããŸããå
·äœçã«ã¯ã
assemble
,clean
,build
ãšãã£ãã©ã€ããµã€ã¯ã«ã¿ã¹ã¯ããã¢ãŒã«ã€ãäœæã®ããã®åºæ¬çãªã¿ã¹ã¯ïŒzip
,tar
ïŒãªã©ãè¿œå ããŸãã - äž»ãªã¿ã¹ã¯:
clean
,assemble
,build
,check
- 䜿ãæ¹: éåžžãçŽæ¥é©çšããããšã¯å°ãªããJavaãã©ã°ã€ã³ãªã©ä»ã®ãã©ã°ã€ã³ã«ãã£ãŠèªåçã«é©çšãããŸãã
- åè: Gradleã®ãã«ãã®åºæ¬çãªã©ã€ããµã€ã¯ã«ïŒçµã¿ç«ãŠâãã§ãã¯âãã«ãïŒãç解ããäžã§éèŠãªæŠå¿µãæäŸããŸãã
2. Java Plugin (`java`)
- ç®ç: Javaãããžã§ã¯ãã®åºæ¬çãªãã«ãïŒã³ã³ãã€ã«ããã¹ããJARäœæãJavadocçæãªã©ïŒããµããŒãããŸãã
base
ãã©ã°ã€ã³ãèªåçã«é©çšããŸãã - äž»ãªèŠçŽ:
- ãœãŒã¹ã³ãŒã:
src/main/java
- ãªãœãŒã¹:
src/main/resources
- ãã¹ãã³ãŒã:
src/test/java
- ãã¹ããªãœãŒã¹:
src/test/resources
- ã³ã³ãã€ã«çµæ:
build/classes/java/main
- çæãããJAR:
build/libs/<ãããžã§ã¯ãå>-<ããŒãžã§ã³>.jar
- ãœãŒã¹ã³ãŒã:
- äž»ãªã¿ã¹ã¯:
compileJava
,processResources
,classes
,testClasses
,jar
,test
,javadoc
- è¿œå ãããäŸåé¢ä¿ã¹ã³ãŒã:
implementation
,compileOnly
,runtimeOnly
,testImplementation
,testCompileOnly
,testRuntimeOnly
- 䜿ãæ¹ (build.gradle – Groovy):
plugins { id 'java' } // ããŒãžã§ã³ããšã³ã³ãŒãã£ã³ã°ãªã©ã®èšå® version = '1.0.0' sourceCompatibility = JavaVersion.VERSION_17 // ãœãŒã¹ã³ãŒãã®JavaããŒãžã§ã³ targetCompatibility = JavaVersion.VERSION_17 // çæããããã€ãã³ãŒãã®JavaããŒãžã§ã³ java { // Java Toolchainã䜿ã£ãŠãã«ãJDKãæå®ããããšãæšå¥š toolchain { languageVersion = JavaLanguageVersion.of(17) } } repositories { mavenCentral() } dependencies { implementation 'com.google.code.gson:gson:2.10.1' testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.2' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.10.2' } tasks.named('test') { useJUnitPlatform() // JUnit 5ã䜿ãå Žå }
3. Java Library Plugin (`java-library`)
- ç®ç: Javaã©ã€ãã©ãªïŒä»ã®ãããžã§ã¯ãããå©çšãããããšãæå³ããJARïŒã®éçºã«ç¹åãããã©ã°ã€ã³ã§ãã
java
ãã©ã°ã€ã³ã®æ©èœããã¹ãŠå«ã¿ãããã«ã©ã€ãã©ãªéçºã«é©ããæ©èœãè¿œå ããŸãã - äž»ãªè¿œå æ©èœ:
api
äŸåé¢ä¿ã¹ã³ãŒã: ãã®ã¹ã³ãŒãã§è¿œå ãããäŸåé¢ä¿ã¯ãã©ã€ãã©ãªã®å ¬éAPIã®äžéšãšã¿ãªããããã®ã©ã€ãã©ãªãå©çšãããããžã§ã¯ãã®ã³ã³ãã€ã«ã¯ã©ã¹ãã¹ã«ãå«ãŸããŸããimplementation
äŸåé¢ä¿ã¹ã³ãŒã: ãã®ã¹ã³ãŒãã§è¿œå ãããäŸåé¢ä¿ã¯ãã©ã€ãã©ãªã®å éšå®è£ ã®è©³çŽ°ãšã¿ãªããããã®ã©ã€ãã©ãªãå©çšãããããžã§ã¯ãã®ã³ã³ãã€ã«ã¯ã©ã¹ãã¹ã«ã¯å«ãŸããŸããïŒå®è¡æã¯ã©ã¹ãã¹ã«ã¯å«ãŸããŸãïŒã
- ã¡ãªãã:
implementation
ãé©åã«äœ¿ãããšã§ãã©ã€ãã©ãªã®å éšå®è£ ã®å€æŽãããã®ã©ã€ãã©ãªãå©çšãããããžã§ã¯ãã®äžèŠãªåã³ã³ãã€ã«ãåŒãèµ·ããã®ãé²ãããã«ãããã©ãŒãã³ã¹ãåäžãããäŸåé¢ä¿ã®ãªãŒã¯ãé²ããŸãã - 䜿ãæ¹ (build.gradle – Groovy):
plugins { id 'java-library' // 'java' ã®ä»£ããã«ãã¡ããäœ¿çš } // ... repositories, version, sourceCompatibility ãªã©ã¯åæ§ ... dependencies { // ãã®ã©ã€ãã©ãªã®å ¬éAPIãäŸåããã©ã€ãã©ãª (äŸ: ã€ã³ã¿ãŒãã§ãŒã¹ã§äœ¿ãå) api 'org.apache.commons:commons-lang3:3.12.0' // ãã®ã©ã€ãã©ãªã®å éšå®è£ ã§ã®ã¿äœ¿ãã©ã€ãã©ãª implementation 'com.fasterxml.jackson.core:jackson-databind:2.15.0' testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.2' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.10.2' }
- æšå¥š: æ°ããJavaã©ã€ãã©ãªãäœæããå Žåã¯ã
java
ã§ã¯ãªãjava-library
ãã©ã°ã€ã³ã䜿çšããããšã匷ãæšå¥šãããŸãã
4. Application Plugin (`application`)
- ç®ç: å®è¡å¯èœãªJavaã¢ããªã±ãŒã·ã§ã³ïŒã³ã³ãœãŒã«ã¢ããªãªã©ïŒã®äœæãå®è¡ãé
åžã容æã«ããŸãã
java
ãã©ã°ã€ã³ãèªåçã«é©çšããŸãã - äž»ãªã¿ã¹ã¯:
run
ïŒã¢ããªã±ãŒã·ã§ã³ãçŽæ¥å®è¡ïŒãstartScripts
ïŒå®è¡çšã®ã·ã§ã«ã¹ã¯ãªãã/Batãã¡ã€ã«ãçæïŒãdistZip
/distTar
ïŒäŸåé¢ä¿ãå«ãé åžçšã¢ãŒã«ã€ããäœæïŒ - 䜿ãæ¹ (build.gradle – Groovy):
plugins { id 'application' } version = '1.0' sourceCompatibility = JavaVersion.VERSION_17 repositories { mavenCentral() } dependencies { implementation 'org.slf4j:slf4j-api:2.0.7' runtimeOnly 'ch.qos.logback:logback-classic:1.4.8' // å®è¡æã«å¿ èŠ } application { // ã¢ããªã±ãŒã·ã§ã³ã®ãšã³ããªãŒãã€ã³ããšãªãã¡ã€ã³ã¯ã©ã¹ãæå® (å¿ é ) mainClass = 'com.example.MyApplication' // (ä»»æ) å®è¡æã®JVMåŒæ° applicationDefaultJvmArgs = ["-Xms512m", "-Xmx1024m"] } java { // Toolchainæšå¥š toolchain { languageVersion = JavaLanguageVersion.of(17) } } // applicationãã©ã°ã€ã³ã¯ããã©ã«ãã§JUnit 4ã䜿ããããJUnit 5ã䜿ãå Žåã¯èšå®ãå¿ èŠ tasks.named('test') { useJUnitPlatform() }
./gradlew run
ã§ã¢ããªã±ãŒã·ã§ã³ãç°¡åã«èµ·åã§ãã./gradlew distZip
ã§äŸåã©ã€ãã©ãªãå«ãã å®è¡å¯èœãªZIPãã¡ã€ã«ãäœæã§ããŸãã
5. War Plugin (`war`)
- ç®ç: Java Webã¢ããªã±ãŒã·ã§ã³ãããã±ãŒãžåããããã®WAR (Web Application Archive) ãã¡ã€ã«ã®äœæããµããŒãããŸãã
java
ãã©ã°ã€ã³ãèªåçã«é©çšããŸãã - äž»ãªèŠçŽ: Webã¢ããªã±ãŒã·ã§ã³ã®ãªãœãŒã¹ïŒHTML, JSP, CSS, JavaScriptãªã©ïŒã¯
src/main/webapp
ã«é 眮ããŸãã - äž»ãªã¿ã¹ã¯:
war
(WARãã¡ã€ã«ã®äœæ) - è¿œå ãããäŸåé¢ä¿ã¹ã³ãŒã:
providedCompile
ïŒã³ã³ãã€ã«æã«å¿ èŠã ãWARã«ã¯å«ããªããServletã³ã³ããçãæäŸããããïŒãprovidedRuntime
ïŒå®è¡æã«å¿ èŠã ãWARã«ã¯å«ããªãïŒ - 䜿ãæ¹ (build.gradle – Groovy):
plugins { id 'war' } version = '1.0.0-SNAPSHOT' sourceCompatibility = JavaVersion.VERSION_17 repositories { mavenCentral() } dependencies { // Servlet API (Tomcatãªã©ã®ã³ã³ãããæäŸããã®ã§WARã«ã¯å«ããªã) providedCompile 'jakarta.servlet:jakarta.servlet-api:6.0.0' providedCompile 'jakarta.servlet.jsp:jakarta.servlet.jsp-api:3.1.0' // Webãã¬ãŒã ã¯ãŒã¯ (äŸ: Spring MVC) implementation 'org.springframework:spring-webmvc:6.0.11' testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.2' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.10.2' } java { toolchain { languageVersion = JavaLanguageVersion.of(17) } } tasks.named('test') { useJUnitPlatform() } war { // (ä»»æ) WARãã¡ã€ã«åãå€æŽ archiveFileName = "${project.name}.war" // (ä»»æ) Webã¢ããªã±ãŒã·ã§ã³ã®ã³ã³ããã¹ãã«ãŒã // webAppDirName = 'src/main/webapp' // ããã©ã«ã }
./gradlew war
ãå®è¡ãããšbuild/libs
ãã£ã¬ã¯ããªã«WARãã¡ã€ã«ãçæãããŸãã
ãããã®åºæ¬çãªãã©ã°ã€ã³ãç解ããçµã¿åãããããšã§ãæ§ã ãªã¿ã€ãã®Javaãããžã§ã¯ãã®ãã«ããå¹ççã«ç®¡çã§ããŸããããã«ç¹å®ã®ãã¬ãŒã ã¯ãŒã¯ïŒSpring Boot, Androidãªã©ïŒãããŒã«ïŒDocker, ã³ãŒã解æããŒã«ãªã©ïŒãå©çšããå Žåã¯ãããããã«å¯Ÿå¿ãããã©ã°ã€ã³ãè¿œå ããŠããããšã«ãªããŸãããã©ã°ã€ã³ãæ¢ãã«ã¯ Gradle Plugin Portal (https://plugins.gradle.org/) ã䟿å©ã§ãã
Gradleã®å¿çšïŒãã«ããããžã§ã¯ããã«ã ðïž
çŸå®ã®ãœãããŠã§ã¢ãããžã§ã¯ãã¯ãåäžã®å¡ãšããŠéçºãããããããæ©èœãé¢å¿äºã«åºã¥ããŠè€æ°ã®ç¬ç«ããã¢ãžã¥ãŒã«ïŒã©ã€ãã©ãªããµãã¢ããªã±ãŒã·ã§ã³ïŒã«åå²ãããããšããããããŸããäŸãã°ã以äžã®ãããªæ§æãèããããŸãã
core
: ã¢ããªã±ãŒã·ã§ã³ã®ã³ã¢ãšãªãããžãã¹ããžãã¯ããã¡ã€ã³ã¢ãã«ãdata-access
: ããŒã¿ããŒã¹ã¢ã¯ã»ã¹ãæ åœããã¢ãžã¥ãŒã«ãcore
ã«äŸåãweb-api
: REST APIãæäŸããWebã¢ããªã±ãŒã·ã§ã³ã¢ãžã¥ãŒã«ãcore
ã«äŸåãbatch-app
: ãããåŠçãå®è¡ããã³ã³ãœãŒã«ã¢ããªã±ãŒã·ã§ã³ã¢ãžã¥ãŒã«ãcore
ãdata-access
ã«äŸåãcommon-utils
: è€æ°ã®ã¢ãžã¥ãŒã«ã§å ±æããããŠãŒãã£ãªãã£ã¯ã©ã¹ã
ãã®ããã«è€æ°ã®ã¢ãžã¥ãŒã«ïŒGradleã§ã¯ãããžã§ã¯ããšåŒã³ãŸãïŒããæ§æããããã«ãããã«ããããžã§ã¯ããã«ããšåŒã³ãŸããGradleã¯ããã®ãããªãã«ããããžã§ã¯ããã«ããéåžžã«å¹ççãã€æè»ã«ç®¡çããããã®åŒ·åãªæ©èœãæäŸããŸãã
ãã«ããããžã§ã¯ãã®æ§é ãšèšå®ãã¡ã€ã«
å žåçãªGradleãã«ããããžã§ã¯ãã®ãã£ã¬ã¯ããªæ§é ã¯ä»¥äžã®ããã«ãªããŸãã
my-multi-project/ <-- ã«ãŒããã£ã¬ã¯ããª
âââ gradlew <-- Gradle Wrapper ã¹ã¯ãªãã
âââ gradlew.bat <-- Gradle Wrapper ã¹ã¯ãªãã (Windows)
âââ gradle/ <-- Gradle Wrapper èšå®
â âââ wrapper/
â âââ gradle-wrapper.properties
âââ settings.gradle(.kts) <-- â ãããžã§ã¯ãæ§æå®çŸ©ãã¡ã€ã« (å¿
é )
âââ build.gradle(.kts) <-- ã«ãŒããããžã§ã¯ãã®ãã«ãã¹ã¯ãªãã (å
±éèšå®ãªã©)
âââ gradle.properties <-- (ä»»æ) ãããžã§ã¯ãå
±éã®ããããã£èšå®
âââ core/ <-- ãµããããžã§ã¯ã 'core'
â âââ build.gradle(.kts) <-- 'core' åºæã®ãã«ãã¹ã¯ãªãã
â âââ src/
âââ data-access/ <-- ãµããããžã§ã¯ã 'data-access'
â âââ build.gradle(.kts) <-- 'data-access' åºæã®ãã«ãã¹ã¯ãªãã
â âââ src/
âââ web-api/ <-- ãµããããžã§ã¯ã 'web-api'
â âââ build.gradle(.kts) <-- 'web-api' åºæã®ãã«ãã¹ã¯ãªãã
â âââ src/
âââ common-utils/ <-- ãµããããžã§ã¯ã 'common-utils'
âââ build.gradle(.kts) <-- 'common-utils' åºæã®ãã«ãã¹ã¯ãªãã
âââ src/
éèŠãªãã¡ã€ã«:
settings.gradle
(ãŸãã¯settings.gradle.kts
): ãã®ãã¡ã€ã«ããã«ããããžã§ã¯ããã«ãã®èŠã§ããã«ãŒããã£ã¬ã¯ããªã«é 眮ããããã®ãã«ãã«ã©ã®ãµããããžã§ã¯ããå«ãŸããããå®çŸ©ããŸãããã®ãã¡ã€ã«ãååšããªãå ŽåãGradleã¯ã·ã³ã°ã«ãããžã§ã¯ããã«ããšããŠæ±ããŸãã- ã«ãŒããããžã§ã¯ãã®
build.gradle(.kts)
: ãããžã§ã¯ãå šäœã®å ±éèšå®ïŒãã¹ãŠã®ãµããããžã§ã¯ãã«é©çšããããã©ã°ã€ã³ããªããžããªãäŸåé¢ä¿ã®ããŒãžã§ã³ç®¡çãã«ã¹ã¿ã ã¿ã¹ã¯ãªã©ïŒãèšè¿°ããã®ã«é©ããŠããŸãã - åãµããããžã§ã¯ãã®
build.gradle(.kts)
: ããããã®ãµããããžã§ã¯ãåºæã®èšå®ïŒé©çšãããã©ã°ã€ã³ãäŸåé¢ä¿ãã¿ã¹ã¯èšå®ãªã©ïŒãèšè¿°ããŸãã
`settings.gradle(.kts)` ã§ãããžã§ã¯ããå®çŸ©ãã
settings.gradle(.kts)
ãã¡ã€ã«ã§ããã«ãã«å«ãŸãããµããããžã§ã¯ãã宣èšããŸãã
// settings.gradle (Groovy DSL)
// ã«ãŒããããžã§ã¯ãã®ååãèšå® (ä»»æã ãæšå¥š)
// ãã®ååã¯ããã«ãææç©ã®ããã©ã«ãåãªã©ã«åœ±é¿ãã
rootProject.name = 'my-awesome-app'
// ãã®ãã«ãã«å«ãŸãããµããããžã§ã¯ãã宣èš
// ãã£ã¬ã¯ããªåãšäžèŽãããã®ãäžè¬ç
include 'core'
include 'data-access'
include 'web-api'
include 'common-utils'
// ãã¹ããããã£ã¬ã¯ããªæ§é ã®å Žåãå¯èœ
// include 'services:user-service'
// include 'services:product-service'
// settings.gradle.kts (Kotlin DSL)
// ã«ãŒããããžã§ã¯ãã®ååãèšå®
rootProject.name = "my-awesome-app"
// ãã®ãã«ãã«å«ãŸãããµããããžã§ã¯ãã宣èš
include("core")
include("data-access")
include("web-api")
include("common-utils")
// ãã¹ããããã£ã¬ã¯ããªæ§é ã®å Žåãå¯èœ
// include("services:user-service")
// include("services:product-service")
include
ã§æå®ãããååã®ãã£ã¬ã¯ããªããµããããžã§ã¯ããšããŠèªèãããŸãã
ãµããããžã§ã¯ãéã®äŸåé¢ä¿ã®å®çŸ©
ãã«ããããžã§ã¯ããã«ãã®å€§ããªå©ç¹ã®äžã€ã¯ããµããããžã§ã¯ãéã§ç°¡åã«äŸåé¢ä¿ãå®çŸ©ã§ããããšã§ããäŸãã°ãweb-api
ãããžã§ã¯ãã core
ãããžã§ã¯ãã«äŸåããŠããå Žåãweb-api/build.gradle(.kts)
ã® dependencies
ãããã¯ã§æ¬¡ã®ããã«èšè¿°ããŸãã
// web-api/build.gradle (Groovy DSL)
plugins {
id 'java' // ãŸã㯠'application', 'war' ãªã©
// ... ä»ã®ãã©ã°ã€ã³
}
repositories {
mavenCentral()
}
dependencies {
// 'core' ãµããããžã§ã¯ããžã®äŸåé¢ä¿ã宣èš
implementation project(':core')
// (ããããã°) 'common-utils' ãµããããžã§ã¯ããžã®äŸå
implementation project(':common-utils')
// å€éšã©ã€ãã©ãªãžã®äŸåé¢ä¿
implementation 'org.springframework.boot:spring-boot-starter-web:3.2.4' // äŸ
// ...
}
// web-api/build.gradle.kts (Kotlin DSL)
plugins {
java // ãŸã㯠application, war ãªã©
// ... ä»ã®ãã©ã°ã€ã³
}
repositories {
mavenCentral()
}
dependencies {
// 'core' ãµããããžã§ã¯ããžã®äŸåé¢ä¿ã宣èš
implementation(project(":core"))
// (ããããã°) 'common-utils' ãµããããžã§ã¯ããžã®äŸå
implementation(project(":common-utils"))
// å€éšã©ã€ãã©ãªãžã®äŸåé¢ä¿
implementation("org.springframework.boot:spring-boot-starter-web:3.2.4") // äŸ
// ...
}
project(':ãµããããžã§ã¯ãå')
ãšããèšæ³ã§ãåããã«ãå
ã®ä»ã®ãããžã§ã¯ããæå®ããŸããGradleã¯ãããèªèãããã«ããå®è¡ããéã«ãäŸåãããŠãããããžã§ã¯ãïŒãã®äŸã§ã¯ core
ãš common-utils
ïŒãå
ã«ãã«ããããããã«ãã¿ã¹ã¯ã®å®è¡é åºãèªåçã«è§£æ±ºããŸããããã«ãããåžžã«ææ°ã®äŸåãããžã§ã¯ãã®ææç©ã䜿ã£ãŠãã«ããè¡ãããŸãã
å ±éèšå®ã®å¹ççãªç®¡ç
è€æ°ã®ãµããããžã§ã¯ãã§åãèšå®ïŒäœ¿çšããJavaã®ããŒãžã§ã³ãé©çšãããã©ã°ã€ã³ãå
±éã®äŸåã©ã€ãã©ãªããªããžããªãªã©ïŒãç¹°ãè¿èšè¿°ããã®ã¯éå¹çã§ãã¡ã³ããã³ã¹æ§ãäœäžãããŸããGradleã§ã¯ãã«ãŒããããžã§ã¯ãã® build.gradle(.kts)
ã䜿ã£ãŠãããã®å
±éèšå®ãäžç®æã§ç®¡çã§ããŸãã
// ã«ãŒããããžã§ã¯ãã® build.gradle (Groovy DSL)
// allprojects: ã«ãŒããããžã§ã¯ãèªèº« + å
šãŠã®ãµããããžã§ã¯ãã«é©çš
allprojects {
// äŸ: å
šãããžã§ã¯ãã§Maven CentralãšGoogleãªããžããªã䜿çš
repositories {
mavenCentral()
google() // Androidãããžã§ã¯ããªã©ãããå Žå
}
}
// subprojects: å
šãŠã®ãµããããžã§ã¯ãã®ã¿ã«é©çš (ã«ãŒããããžã§ã¯ãã«ã¯é©çšãããªã)
subprojects {
// äŸ: å
šãŠã®ãµããããžã§ã¯ãã«Java Libraryãã©ã°ã€ã³ãšCheckstyleãã©ã°ã€ã³ãé©çš
apply plugin: 'java-library'
apply plugin: 'checkstyle'
// äŸ: å
šãµããããžã§ã¯ãã§Java 17ãäœ¿çš (Toolchainæšå¥š)
java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}
// äŸ: å
šãµããããžã§ã¯ãã«å
±éã®ãã¹ãäŸåé¢ä¿ãè¿œå
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.10.2'
testImplementation 'org.assertj:assertj-core:3.25.1'
}
// äŸ: å
šãµããããžã§ã¯ãã®testã¿ã¹ã¯ã§JUnit 5ã䜿çš
tasks.named('test') {
useJUnitPlatform()
}
// äŸ: Checkstyleã¿ã¹ã¯ã®èšå®
checkstyle {
toolVersion = '10.12.7' // 䜿çšããCheckstyleã®ããŒãžã§ã³
// configFile = rootProject.file('config/checkstyle/google_checks.xml') // èšå®ãã¡ã€ã«ã®æå®
}
}
// ã«ãŒããããžã§ã¯ãåºæã®èšå® (ããããã°)
// äŸ: ã«ãŒããããžã§ã¯ãèªäœã¯ãã«ãææç©ãæããªãå Žåãªã©
// apply plugin: 'base'
// version = '1.0.0-SNAPSHOT'
allprojects { ... }
ãããã¯ã¯ã«ãŒããããžã§ã¯ããšå
šãŠã®ãµããããžã§ã¯ãã«ãsubprojects { ... }
ãããã¯ã¯å
šãŠã®ãµããããžã§ã¯ãã«ã®ã¿èšå®ãé©çšããŸããããã«ãããèšå®ã®éè€ãé¿ãããããžã§ã¯ãå
šäœã§ã®äžè²«æ§ãä¿ã€ããšãã§ããŸããäŸåé¢ä¿ã®ããŒãžã§ã³ç®¡çã«ã¯ãããŒãžã§ã³ã«ã¿ãã°æ©èœãext
ãããã¯ã䜿ã£ãã«ã¹ã¿ã ããããã£ãªã©ã掻çšã§ããŸãã
ãã«ããããžã§ã¯ãã§ã®ã¿ã¹ã¯å®è¡
ãã«ããããžã§ã¯ããã«ãã§ã¯ãã«ãŒããã£ã¬ã¯ããªããæ§ã ãªæ¹æ³ã§ã¿ã¹ã¯ãå®è¡ã§ããŸãã
# å
šãŠã®ãµããããžã§ã¯ãã® `build` ã¿ã¹ã¯ãå®è¡ (äŸåé¢ä¿é ã«å®è¡)
./gradlew build
# ç¹å®ã®ãµããããžã§ã¯ã (äŸ: web-api) ã® `build` ã¿ã¹ã¯ã®ã¿ãå®è¡
# (äŸåãã :core ã :common-utils ãå¿
èŠãªããã«ãããã)
./gradlew :web-api:build
# å
šãŠã®ãµããããžã§ã¯ãã® `clean` ã¿ã¹ã¯ãå®è¡
./gradlew clean
# ç¹å®ã®ãµããããžã§ã¯ã (äŸ: core) ã® `test` ã¿ã¹ã¯ãå®è¡
./gradlew :core:test
# ã«ãŒããããžã§ã¯ãã® `build` ãå®è¡ãã`web-api` ã® `test` ã¯ã¹ããããã
./gradlew build -x :web-api:test
# `web-api` ãããžã§ã¯ããšãã®äŸåãããžã§ã¯ãã®ã¿ããã«ããã (Gradle 7.6+)
./gradlew :web-api:build --build-needed
:ãããžã§ã¯ãå:ã¿ã¹ã¯å
ãšãã圢åŒã§ã察象ã®ãããžã§ã¯ããšã¿ã¹ã¯ãæå®ããŸãããããžã§ã¯ãåãçç¥ãããšãã«ã¬ã³ããã£ã¬ã¯ããªã®ãããžã§ã¯ãïŒéåžžã¯ã«ãŒããããžã§ã¯ãïŒã察象ãšãªããŸããã«ãŒããããžã§ã¯ãã§ãµããããžã§ã¯ãã«å®çŸ©ãããŠããªãã¿ã¹ã¯ïŒäŸ: clean
ïŒãå®è¡ãããšãéåžžã¯ãã®ã¿ã¹ã¯ãæã€å
šãŠã®ãµããããžã§ã¯ãã§å®è¡ãããŸãã
Gradleã®ãã«ããããžã§ã¯ãæ©èœã¯éåžžã«åŒ·åã§ãã³ãŒãã®ã¢ãžã¥ãŒã«æ§ãé«ãã倧èŠæš¡ã§è€éãªãœãããŠã§ã¢ã·ã¹ãã ã®éçºãšä¿å®ã容æã«ããŸããäŸåé¢ä¿ã®èªå解決ãå ±éèšå®ã®éçŽãæè»ãªã¿ã¹ã¯å®è¡ã«ãããéçºè ã¯åã ã®ã¢ãžã¥ãŒã«ã«éäžãã€ã€ããããžã§ã¯ãå šäœã®äžè²«æ§ãä¿ã€ããšãã§ããŸããðª
ãŸãšãïŒGradleã§éçºããã£ãšå¹ççã«ïŒð
æ¬èšäºã§ã¯ãã¢ãã³ãªãã«ãèªååããŒã«ã§ããGradleã«ã€ããŠããã®å ¥éãšããŠå æ¬çã«è§£èª¬ããŠããŸãããGradleãšã¯äœãããã®æŽå²çèæ¯ãAntãMavenãšã®éãããããŠæ žãšãªãç¹åŸŽïŒæè»ãªDSLãããã©ãŒãã³ã¹æé©åãäŸåé¢ä¿ç®¡çããã©ã°ã€ã³ã·ã¹ãã ããã«ããããžã§ã¯ããµããŒãïŒãåŠã³ãŸãããããã«ãã€ã³ã¹ããŒã«æ¹æ³ããåºæ¬çãªäœ¿ãæ¹ïŒãããžã§ã¯ãåæåããã«ãã¹ã¯ãªããã®æ§é ãã¿ã¹ã¯å®è¡ãGradle Wrapperã®éèŠæ§ïŒãäž»èŠãªJavaé¢é£ãã©ã°ã€ã³ããããŠå¿çšçãªãã«ããããžã§ã¯ããã«ãã®ç®¡çæ¹æ³ãŸã§ãå ·äœçãªã³ãŒãäŸã亀ããªããèŠãŠããŸããã
Gradleãå°å ¥ã»æŽ»çšããäž»ãªã¡ãªãããæ¹ããŠæŽçããŸããã:
- â è¡šçŸåè±ãã§æè»ãªãã«ãå®çŸ©: GroovyãŸãã¯Kotlinã«ããããã°ã©ããã«ãªãã«ãã¹ã¯ãªããã«ãããåçŽãªèšå®ããè€éãªã«ã¹ã¿ã ããžãã¯ãŸã§ããã«ãããã»ã¹ãæãéãã«å¶åŸ¡ã§ããŸãã
- â¡ åè¶ãããã«ãããã©ãŒãã³ã¹: ã€ã³ã¯ãªã¡ã³ã¿ã«ãã«ãããã«ããã£ãã·ã¥ïŒããŒã«ã«/ãªã¢ãŒãïŒãGradleããŒã¢ã³ãèšå®ãã£ãã·ã¥ã䞊åå®è¡ãšãã£ãé«åºŠãªæé©åæè¡ã«ãããç¹ã«å€§èŠæš¡ãããžã§ã¯ããå埩çãªéçºãµã€ã¯ã«ã«ãããŠãã«ãæéãå€§å¹ ã«ççž®ããŸãã
- ð é«åºŠãªäŸåé¢ä¿ç®¡ç: æšç§»çäŸåé¢ä¿ã®è§£æ±ºãæè»ãªããŒãžã§ã³æå®ãäŸåé¢ä¿ã¹ã³ãŒãïŒ
api
/implementation
ãªã©ïŒã«ããã¯ã©ã¹ãã¹å¶åŸ¡ãã³ã³ããªã¯ã解決ãBOMãµããŒããªã©ãè€éãªäŸåé¢ä¿ãå¹æçã«ç®¡çã§ããŸãã - 𧩠æ¡åŒµæ§ã®é«ããã©ã°ã€ã³ãšã³ã·ã¹ãã : è±å¯ãªã³ã¢ãã©ã°ã€ã³ãšã³ãã¥ããã£ãã©ã°ã€ã³ã«ãããç¹å®ã®ãã¬ãŒã ã¯ãŒã¯ïŒAndroid, Spring Bootãªã©ïŒãããŒã«ïŒãã¹ããéç解æããããã€ãªã©ïŒåãã®æ©èœãç°¡åã«è¿œå ã»èšå®ã§ããŸããã«ã¹ã¿ã ãã©ã°ã€ã³ã«ãããã«ãããžãã¯ã®åå©çšãå¯èœã§ãã
- ðïž æŽç·Žããããã«ããããžã§ã¯ããµããŒã: è€æ°ã®ã¢ãžã¥ãŒã«ããæã倧èŠæš¡ãããžã§ã¯ããå¹ççã«æ§æã»ç®¡çã§ããŸããå ±éèšå®ã®éçŽããããžã§ã¯ãéäŸåã®èªå解決ãæè»ãªã¿ã¹ã¯å®è¡ã«ãããã¢ãžã¥ãŒã«æ§ãšã¡ã³ããã³ã¹æ§ãåäžãããŸãã
- ð å¹ åºãæ¡çšãšæŽ»çºãªã³ãã¥ããã£: Androidéçºã®ããã¡ã¯ãã¹ã¿ã³ããŒãã§ãããJavaãšã³ã·ã¹ãã å šäœã§åºãæ¡çšãé²ãã§ããŸãã掻çºãªã³ãã¥ããã£ãšç¶ç¶çãªéçºã«ãããå°æ¥æ§ãæåŸ ã§ããŸãã
Gradleã¯éåžžã«é«æ©èœã§ããããã®å šãŠã®æ©èœãæåãããã¹ã¿ãŒããå¿ èŠã¯ãããŸãããMavenãšæ¯èŒãããšåŠç¿æ²ç·ãããæ¥ã§ãããšããåŽé¢ã¯ãããŸãããåºæ¬çãªäœ¿ãæ¹ãèŠããèŠçŽã«åŸã£ãŠãã©ã°ã€ã³ã掻çšããã°ãå€ãã®ãããžã§ã¯ãã§ãã®æ©æµãããã«æããããã¯ãã§ãããããŠããããžã§ã¯ããè€éåããã«ã€ããŠãGradleã®æã€æè»æ§ãé«åºŠãªæ©èœãç䟡ãçºæ®ããŸãã
ããããGradleãããã«æ·±ãåŠã¶ããã®ã¹ããã:
- æãåãã:
gradle init
ã§æ§ã ãªã¿ã€ãã®ãããžã§ã¯ããäœæãããã«ãã¹ã¯ãªãããå®éã«ç·šéããŠã¿ãŸããããäŸåé¢ä¿ãè¿œå ããããç°¡åãªã«ã¹ã¿ã ã¿ã¹ã¯ãäœæãããããããšããå§ããŸãã - å ¬åŒããã¥ã¡ã³ããèªã: Gradleã®å ¬åŒãŠã§ããµã€ãã«ã¯éåžžã«å å®ããããã¥ã¡ã³ãããããŸããç¹ã« Gradle User Manual ã¯å¿ èªã§ããç¹å®ã®æ©èœïŒäŸåé¢ä¿ç®¡çããã©ã°ã€ã³éçºãªã©ïŒã«ã€ããŠæ·±ãç¥ãããå Žåã«åç §ããŸããããDSL Referenceã圹ç«ã¡ãŸãã
- ãã©ã°ã€ã³ãæ¢æ±ãã: èªåã®äœ¿ã£ãŠãããã¬ãŒã ã¯ãŒã¯ãããŒã«ã«å¯Ÿå¿ãããã©ã°ã€ã³ãæ¢ãïŒGradle Plugin PortalïŒããã®äœ¿ãæ¹ãåŠãã§ã¿ãŸãããã
- Kotlin DSLã«ææŠãã: ããKotlinã«æ
£ããŠããããŸãã¯éçåä»ãã®ã¡ãªããã享åããããªãã
build.gradle.kts
ãšsettings.gradle.kts
ã䜿ã£ããã«ãã¹ã¯ãªããèšè¿°ãè©ŠããŠã¿ãŸããããIDEã®ãµããŒããæ Œæ®µã«åäžããŸãã - ãã«ãã®å
éšãç解ãã: ãã«ãã®ã©ã€ããµã€ã¯ã«ïŒåæåãèšå®ãå®è¡ãã§ãŒãºïŒããããžã§ã¯ããªããžã§ã¯ããšã¿ã¹ã¯ãªããžã§ã¯ããã¿ã¹ã¯ã°ã©ãã®æŠå¿µãªã©ãç解ãããšãããé«åºŠãªã«ã¹ã¿ãã€ãºããã©ãã«ã·ã¥ãŒãã£ã³ã°ãå¯èœã«ãªããŸãã
--scan
ãªãã·ã§ã³ã䜿ã£ãŠãã«ãã¹ãã£ã³ãçæãããã«ãã®è©³çŽ°ãåæããã®ãè¯ãåŠç¿æ¹æ³ã§ãã - ã³ãã¥ããã£ã«åå ãã: Stack OverflowãGradleã®ãã©ãŒã©ã ãªã©ã§è³ªåããããä»ã®äººã®è³ªåãåçãèªãã ãããã®ãæå¹ã§ãã
Gradleã¯ãçŸä»£ã®ãœãããŠã§ã¢éçºã«ããããã«ãèªååã®èª²é¡ã«å¯Ÿãã匷åãªãœãªã¥ãŒã·ã§ã³ã§ãããã®åŠç¿ã¯ãéçºè ãšããŠã®ã¹ãã«ã»ãããåäžãããæ¥ã ã®éçºäœæ¥ãããå¹ççã§å¿«é©ãªãã®ã«ããŠãããã§ãããã
ãã®èšäºããçããã®Gradleãžã®ç¬¬äžæ©ãèžã¿åºãããããããã¯æ¢ã«ã䜿ãã®æ¹ã«ãšã£ãŠã¯ç¥èãæŽçã»æ·±åãããããã®äžå©ãšãªãã°ã倧å€å¬ããæããŸããHappy Gradling! ð
ã³ã¡ã³ã