Gradleã¯å ã ã®ä½ããé常ã«ãã©ãã¯ããã¯ã¹ãªãããè¨å®ããããªãã¸ã§ã¯ãã®ã©ã¤ããµã¤ã¯ã«ããããã¥ãããªã©ã®åé¡ããããGradle3ã«åãã¦ããããä¿®æ£ãã¦ããæä¸ã§ãããã®ä¸æ ¸ã¨ãªãã®ãRule Based Model Configurationã¨ããä»çµã¿ã§ãã
ãã®ä»çµã«ã¤ãã¦ã¯ä»¥åã«ãã¨ã³ããªã¼ãæ¸ãã¦ããã®ã§ããã¡ããåç §ãã¦ãã ããã
åºæ¬çãªèãæ¹ã¯
- å¤ãªãã¸ã§ã¯ãã®çæ(ãã©ã°ã¤ã³ãå¤ãªãã¸ã§ã¯ããçæãã)
- å¤ãªãã¸ã§ã¯ãã®è¨å®(ã¦ã¼ã¶ã¼ããªãã¸ã§ã¯ãã®ä¸ã®å¤ãå¤æ´ã§ãã/mutableãªãªãã¸ã§ã¯ã)
- å¤ã®å©ç¨(ã¦ã¼ã¶ã¼ããã©ã°ã¤ã³ãå¤ãå¤æ´ã§ããªã/immutableãªãªãã¸ã§ã¯ã)
ã¨ããæµãã«ãã£ã¦è¨å®ãè¨è¿°ãã¦ããã¨ããèãæ¹ã§ãã
ã§ã¯ãJavaããã¸ã§ã¯ãã¯ã©ã®ããã«å¤ãã£ã¦ããããä»åã¯ç°¡åã«èª¬æããã¨ã¨ãã«ãç°¡åãªããã¸ã§ã¯ããä½ã£ã¦ã¿ã¾ãã
ãã©ã°ã¤ã³
ããã¾ã§ã¯Javaããã¸ã§ã¯ãã®å ´åã¯æ¬¡ã®ããã«è¨è¿°ãã¦ãã¾ããã
apply plugin: 'java'
ãããã¯Gradle2.2以éã§ããã°ã次ã®ããã«ãè¨è¿°ã§ãã¾ããã
plugins {
id 'java'
}
Gradle3以éã®Javaããã¸ã§ã¯ãã§ã¯ãã®é¨åã次ã®ããã«å¤ããã¾ãã
plugins { id 'jvm-component' id 'java-lang' }
jvm-component
ãã©ã°ã¤ã³ã¨java-lang
ãã©ã°ã¤ã³ã®äºã¤ãå©ç¨ãã¾ãã
jvm-component
ãã©ã°ã¤ã³ã§ã¯ä¸»ã«JVMç³»ã®ãã©ã°ã¤ã³ã«å ±éã®ãã£ã¬ã¯ããªã¼ã»ãããªã©ãrule based modelãªãã¸ã§ã¯ãã«å¾ã£ã¦å®ç¾©ãã¾ããjava-lang
ãã©ã°ã¤ã³ã§ã¯Javaã«ç¹åãããã£ã¬ã¯ããªã¼ã»ãããææç©çæã®ã¿ã¹ã¯ãªã©ãrule based modelãªãã¸ã§ã¯ãã«å¾ã£ã¦å®ç¾©ãã¾ãã
ã³ã³ãã¼ãã³ã
ä¸è¨ã®æå¾ã®ã»ãã§ããrule based modelãªãã¸ã§ã¯ãã«å¾ã£ã¦å®ç¾©ãã¾ããã¨ããè¨èãä½åº¦ã使ãã¾ãããããã¯ãã«ãã¹ã¯ãªããã®ä¸ã§model{}
ãããã¯å
ã§å®ç¾©ãããã³ã³ãã¼ãã³ã(components
)ã¨ãããªãã¸ã§ã¯ããããã¸ã§ã¯ãã®ãã£ã¬ã¯ããªã¼æ§é ãªã©ã決å®ãããã¨ãæå³ãã¾ãã
ããã§ãæã
ãmodel{}
ãããã¯å
ã«ããã¦components
ãªãã¸ã§ã¯ãã«å¤ãè¨å®ãã¦ã¿ããã¨æãã¾ãã
model { components { main(JvmLibrarySpec) } }
ãã¦ããã®è¨è¿°ãããå¾ã§ãcomponents
ã¿ã¹ã¯ãå®è¡ãã¦ã¿ã¦ãã ããã
$ gradle components :components ------------------------------------------------------------ Root project ------------------------------------------------------------ JVM library 'main' ------------------ Source sets Java source 'main:java' srcDir: src/main/java JVM resources 'main:resources' srcDir: src/main/resources Binaries Jar 'mainJar' build using task: :mainJar targetPlatform: Java SE 8 tool chain: JDK 8 (1.8) Jar file: build/jars/mainJar/main.jar Note: currently not all plugins register their components, so some components may not be visible here. BUILD SUCCESSFUL Total time: 0.793 secs $
ãã®æ®µéã§ã以ä¸ã®æ å ±ããããã¾ãã
- ã©ã¤ãã©ãªã¼ã®ååã¯
main
- ã½ã¼ã¹ã¯
src/main/java
ãã£ã¬ã¯ããªã¼ä»¥ä¸ã«é ç½®ããã - ãªã½ã¼ã¹ãã¡ã¤ã«ã¯
src/main/resources
ãã£ã¬ã¯ããªã¼ä»¥ä¸ã«é ç½®ããã - çæãããã¯ã©ã¹ãã¡ã¤ã«ã®ã¿ã¼ã²ãããã©ãããã©ã¼ã ã¯Java8
- ä½æãããjarã®ãã£ã¬ã¯ããªã¼ã¨ååã¯
build/jars/mainJar/main.jar
ã³ã³ãã¼ãã³ãã¯ããã¤ã§ãä½ããã¨ãã§ãã¾ããä¾ãã°ã次ã®ããã«è¨è¿°ãããã¨ã§main
ã¨ããã©ã¤ãã©ãªã¼ã¨sub
ã¨ããã©ã¤ãã©ãªã¼ãä½æããã¾ãã
model { components { main(JvmLibrarySpec) sub(JvmLibrarySpec) } }
ãã®å ´åãããããã®ãã£ã¬ã¯ããªã¼ã¯æ¬¡ã®ããã«ãªãã¾ãã
ã©ã¤ãã©ãªã¼ | ã½ã¼ã¹ | ãªã½ã¼ã¹ | Jarãã¡ã¤ã« |
---|---|---|---|
main |
src/main/java |
src/main/resources |
build/jars/mainJar/main.jar |
sub |
src/sub/java |
src/sub/resources |
build/jars/subJar/sub.jar |
ãã«ã
ãã¦ãåé¡ãç°¡åã«ããããã«sub
ã©ã¤ãã©ãªã¼ã«ã¯æ¶ãã¦ããããã¨ã«ãã¾ãã
src/main/java
ããã³src/main/resources
ã«ãã¡ã¤ã«ãä½ã£ã¦ã¿ã¾ãããã
src/main/java/sample/Main.java
package sample; import java.io.IOException; public class Main { public static void main(String... args) throws IOException { Res res = new Res(); System.out.println(res.getMessage("message")); System.out.println(res.getMessage("name")); System.out.println(res.getMessage("my")); } }
src/main/java/sample/Res.java
package sample; import java.io.InputStream; import java.io.InputStreamReader; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.Properties; public class Res { private final Properties prop; public Res() throws IOException { ClassLoader cl = getClass().getClassLoader(); prop = new Properties(); try(InputStream is = cl.getResourceAsStream("app.properties")) { InputStreamReader r = new InputStreamReader(is, StandardCharsets.UTF_8); prop.load(r); } } public String getMessage(String key) { return prop.getProperty(key, "<no-entry>"); } }
src/main/resources/app.properties
message=ããã«ã¡ã name=mikeãã my=åã¯Gradle
ã§ã¯ãããããã«ããã¾ãããã«ãã³ãã³ãã¯gradle build
ã§ãã
$ gradle clean build :clean :compileMainJarMainJava :processMainJarMainResources :createMainJar :mainJar :assemble :check UP-TO-DATE :build BUILD SUCCESSFUL Total time: 0.847 secs $
ã¨ããããã§ãåºæ¥ä¸ãã£ãããã§ãã
ææç©ã®ä½ç½®ã調ã¹ã¦ã¿ã¾ãã
$ tree build build âââ classes â  âââ mainJar â  âââ META-INF â  âââ app.properties â  âââ sample â  âââ Main.class â  âââ Res.class âââ jars â  âââ mainJar â  âââ main.jar âââ jvm-dep-cache âââ tmp âââ compileMainJarMainJava âââ createMainJar âââ MANIFEST.MF 10 directories, 5 files
build/jars/mainJar/main.jar
ã«ææç©ãä½æããã¦ãã¾ãã
ã§ã¯ãå®è¡ãã¦ã¿ã¾ãããã¡ãã£ã¨èª¿ã¹ãã®ã§ãããMANIFEST.MF
ããããããã®DSLãè¦å½ããã¾ããã§ãã(´ã»Ïã»ï½)
ãã®ããã-cp
ã§jarãèªã¿è¾¼ãã§ã¡ã¤ã³ã¯ã©ã¹ãæå®ãã¦å®è¡ãã¾ãã
$ java -cp build/jars/mainJar/main.jar sample.Main ããã«ã¡ã mikeãã åã¯Gradle
ã¯ããã¾ãã大ãããã¨ã®ãªãããã¸ã§ã¯ãã§ããã
以ä¸ãJVM component modelãç¨ããJavaããã¸ã§ã¯ãã®ä½æå ¥éã§ããã
ãªããä»åä½æããããã¸ã§ã¯ãã®ãµã³ãã«ã¯ä»¥ä¸ã®ã¬ãã¸ããªã¼ããåå¾ã§ãã¾ãã
次å(ãããã©ããã¯ç¥ããªã)ã¯JVM component modelã¨Java9 Project Jigsawã®é¢ä¿ã«ã¤ãã¦è¨åã§ããã°ãããªãâ¦