gradle init: javaとjava_libraryの違い


`gradle init --type java-application` と`gradle init --type java_library`ってなにが違うのか調べたメモ

gradleはバージョン4.0を利用しました。


## build.gradleの違い

apply pluginの違い

| type         | 違い                         |
|--------------|------------------------------|
| java-application         | apply plugin: 'java'         |
| java-library | apply plugin: 'java-library' |


dependenciesの違い

| type         | 違い                         |
|--------------|------------------------------|
| java-application         | compile 'com.google.guava:guava:21.0'         |
|              | testCompile 'junit:junit:4.12' |
| java-library | api 'org.apache.commons:commons-math3:3.6.1' |
|              | implementation 'com.google.guava:guava:21.0' |
|              | testImplementation 'junit:junit:4.12' |

mainClassName

| type         | 違い                         |
|--------------|------------------------------|
| java-application         | -mainClassName = 'App' |        
| java-library | 未定義 |


diffによる出力

```diff
--- java/build.gradle	2017-06-25 18:05:15.000000000 +0900
+++ ./java_library/build.gradle	2017-06-25 18:05:34.000000000 +0900
@@ -1,16 +1,13 @@
 /*
  * This build 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 guide available at https://docs.gradle.org/4.0/userguide/tutorial_java_projects.html
+ * This generated file contains a sample Java Library project to get you started.
+ * For more details take a look at the Java Libraries chapter in the Gradle
+ * user guide available at https://docs.gradle.org/4.0/userguide/java_library_plugin.html
  */

-// Apply the java plugin to add support for Java
-apply plugin: 'java'
-
-// Apply the application plugin to add support for building an application
-apply plugin: 'application'
+// Apply the java-library plugin to add support for Java Library
+apply plugin: 'java-library'

 // In this section you declare where to find the dependencies of your project
 repositories {
@@ -20,13 +17,13 @@
 }

 dependencies {
-    // This dependency is found on compile classpath of this component and consumers.
-    compile 'com.google.guava:guava:21.0'
+    // This dependency is exported to consumers, that is to say found on their compile classpath.
+    api 'org.apache.commons:commons-math3:3.6.1'
+
+    // This dependency is used internally, and not exposed to consumers on their own compile classpath.
+    implementation 'com.google.guava:guava:21.0'

     // Use JUnit test framework
-    testCompile 'junit:junit:4.12'
+    testImplementation 'junit:junit:4.12'
 }

-// Define the main class for the application
-mainClassName = 'App'
-
```

## ファイル出力


`gradle init --type java-application`の場合

```
.
|-- build.gradle
|-- gradle
|   `-- wrapper
|       |-- gradle-wrapper.jar
|       `-- gradle-wrapper.properties
|-- gradlew
|-- gradlew.bat
|-- settings.gradle
`-- src
    |-- main
    |   `-- java
    |       `-- App.java
    `-- test
        `-- java
            `-- AppTest.java
```

`gradle init --type java-library`の場合

```
.
|-- build.gradle
|-- gradle
|   `-- wrapper
|       |-- gradle-wrapper.jar
|       `-- gradle-wrapper.properties
|-- gradlew
|-- gradlew.bat
|-- settings.gradle
`-- src
    |-- main
    |   `-- java
    |       `-- Library.java
    `-- test
        `-- java
            `-- LibraryTest.java
```

## diff出力

```diff
diff -ur java/.gradle/buildOutputCleanup/cache.properties java_library/.gradle/buildOutputCleanup/cache.properties
--- java/.gradle/buildOutputCleanup/cache.properties	2017-06-25 18:05:05.000000000 +0900
+++ java_library/.gradle/buildOutputCleanup/cache.properties	2017-06-25 18:05:34.000000000 +0900
@@ -1,2 +1,2 @@
-#Sun Jun 25 18:05:05 JST 2017
+#Sun Jun 25 18:05:34 JST 2017
 gradle.version=4.0
diff -ur java/build.gradle java_library/build.gradle
--- java/build.gradle	2017-06-25 18:05:15.000000000 +0900
+++ java_library/build.gradle	2017-06-25 18:05:34.000000000 +0900
@@ -1,16 +1,13 @@
 /*
  * This build 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 guide available at https://docs.gradle.org/4.0/userguide/tutorial_java_projects.html
+ * This generated file contains a sample Java Library project to get you started.
+ * For more details take a look at the Java Libraries chapter in the Gradle
+ * user guide available at https://docs.gradle.org/4.0/userguide/java_library_plugin.html
  */

-// Apply the java plugin to add support for Java
-apply plugin: 'java'
-
-// Apply the application plugin to add support for building an application
-apply plugin: 'application'
+// Apply the java-library plugin to add support for Java Library
+apply plugin: 'java-library'

 // In this section you declare where to find the dependencies of your project
 repositories {
@@ -20,13 +17,13 @@
 }

 dependencies {
-    // This dependency is found on compile classpath of this component and consumers.
-    compile 'com.google.guava:guava:21.0'
+    // This dependency is exported to consumers, that is to say found on their compile classpath.
+    api 'org.apache.commons:commons-math3:3.6.1'
+
+    // This dependency is used internally, and not exposed to consumers on their own compile classpath.
+    implementation 'com.google.guava:guava:21.0'

     // Use JUnit test framework
-    testCompile 'junit:junit:4.12'
+    testImplementation 'junit:junit:4.12'
 }

-// Define the main class for the application
-mainClassName = 'App'
-
Binary files java/gradle/wrapper/gradle-wrapper.jar and java_library/gradle/wrapper/gradle-wrapper.jar differ
diff -ur java/gradle/wrapper/gradle-wrapper.properties java_library/gradle/wrapper/gradle-wrapper.properties
--- java/gradle/wrapper/gradle-wrapper.properties	2017-06-25 18:05:05.000000000 +0900
+++ java_library/gradle/wrapper/gradle-wrapper.properties	2017-06-25 18:05:34.000000000 +0900
@@ -1,4 +1,4 @@
-#Sun Jun 25 18:05:05 JST 2017
+#Sun Jun 25 18:05:34 JST 2017
 distributionBase=GRADLE_USER_HOME
 distributionPath=wrapper/dists
 zipStoreBase=GRADLE_USER_HOME
diff -ur java/settings.gradle java_library/settings.gradle
--- java/settings.gradle	2017-06-25 18:05:15.000000000 +0900
+++ java_library/settings.gradle	2017-06-25 18:05:34.000000000 +0900
@@ -15,4 +15,4 @@
 include 'services:webservice'
 */

-rootProject.name = 'java'
+rootProject.name = 'java_library'
Only in java/src/main/java: App.java
Only in java_library/src/main/java: Library.java
Only in java/src/test/java: AppTest.java
Only in java_library/src/test/java: LibraryTest.java
```