├── initial ├── build.gradle ├── .gitignore └── src │ └── main │ └── java │ └── hello │ ├── Greeter.java │ └── HelloWorld.java ├── .codesets.json ├── LICENSE.writing.txt ├── complete ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── src │ ├── main │ │ └── java │ │ │ └── hello │ │ │ ├── Greeter.java │ │ │ └── HelloWorld.java │ └── test │ │ └── java │ │ └── hello │ │ └── HelloWorldTests.java ├── build.gradle ├── gradlew.bat └── gradlew ├── CONTRIBUTING.adoc ├── test └── run.sh ├── .gitignore ├── Jenkinsfile ├── LICENSE.txt └── README.adoc /initial/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java' 2 | -------------------------------------------------------------------------------- /.codesets.json: -------------------------------------------------------------------------------- 1 | [ { "name" : "complete", "dir" : "complete" } ] 2 | -------------------------------------------------------------------------------- /initial/.gitignore: -------------------------------------------------------------------------------- 1 | .classpath 2 | .gradle/ 3 | .project 4 | .settings/ 5 | bin/ 6 | build/ -------------------------------------------------------------------------------- /LICENSE.writing.txt: -------------------------------------------------------------------------------- 1 | Except where otherwise noted, this work is licensed under https://creativecommons.org/licenses/by-nd/3.0/ 2 | -------------------------------------------------------------------------------- /complete/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/spring-attic/gs-gradle/main/complete/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /CONTRIBUTING.adoc: -------------------------------------------------------------------------------- 1 | If you have not previously done so, please fill out and 2 | submit the https://cla.pivotal.io/sign/spring[Contributor License Agreement]. -------------------------------------------------------------------------------- /complete/src/main/java/hello/Greeter.java: -------------------------------------------------------------------------------- 1 | package hello; 2 | 3 | public class Greeter { 4 | public String sayHello() { 5 | return "Hello world!"; 6 | } 7 | } -------------------------------------------------------------------------------- /initial/src/main/java/hello/Greeter.java: -------------------------------------------------------------------------------- 1 | package hello; 2 | 3 | public class Greeter { 4 | public String sayHello() { 5 | return "Hello world!"; 6 | } 7 | } -------------------------------------------------------------------------------- /initial/src/main/java/hello/HelloWorld.java: -------------------------------------------------------------------------------- 1 | package hello; 2 | 3 | public class HelloWorld { 4 | public static void main(String[] args) { 5 | Greeter greeter = new Greeter(); 6 | System.out.println(greeter.sayHello()); 7 | } 8 | } -------------------------------------------------------------------------------- /complete/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.0.1-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /complete/src/main/java/hello/HelloWorld.java: -------------------------------------------------------------------------------- 1 | package hello; 2 | 3 | import org.joda.time.LocalTime; 4 | 5 | public class HelloWorld { 6 | public static void main(String[] args) { 7 | LocalTime currentTime = new LocalTime(); 8 | System.out.println("The current local time is: " + currentTime); 9 | 10 | Greeter greeter = new Greeter(); 11 | System.out.println(greeter.sayHello()); 12 | } 13 | } -------------------------------------------------------------------------------- /test/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | cd $(dirname $0) 3 | 4 | cd ../complete 5 | ./gradlew build 6 | ret=$? 7 | if [ $ret -ne 0 ]; then 8 | exit $ret 9 | fi 10 | rm -rf build 11 | 12 | cd ../initial 13 | ../complete/gradlew -b ../initial/build.gradle wrapper 14 | ./gradlew compileJava 15 | ret=$? 16 | if [ $ret -ne 0 ]; then 17 | exit $ret 18 | fi 19 | rm -rf build 20 | rm -rf gradle 21 | rm gradlew* 22 | 23 | exit 24 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Operating System Files 2 | 3 | *.DS_Store 4 | Thumbs.db 5 | *.sw? 6 | .#* 7 | *# 8 | *~ 9 | *.sublime-* 10 | 11 | # Build Artifacts 12 | 13 | .gradle/ 14 | build/ 15 | target/ 16 | bin/ 17 | dependency-reduced-pom.xml 18 | 19 | # Eclipse Project Files 20 | 21 | .classpath 22 | .project 23 | .settings/ 24 | 25 | # IntelliJ IDEA Files 26 | 27 | *.iml 28 | *.ipr 29 | *.iws 30 | *.idea 31 | 32 | README.html 33 | -------------------------------------------------------------------------------- /complete/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java' 2 | apply plugin: 'eclipse' 3 | apply plugin: 'application' 4 | 5 | mainClassName = 'hello.HelloWorld' 6 | 7 | // tag::repositories[] 8 | repositories { 9 | mavenCentral() 10 | } 11 | // end::repositories[] 12 | 13 | // tag::jar[] 14 | jar { 15 | archiveBaseName = 'gs-gradle' 16 | archiveVersion = '0.1.0' 17 | } 18 | // end::jar[] 19 | 20 | // tag::dependencies[] 21 | sourceCompatibility = 1.8 22 | targetCompatibility = 1.8 23 | 24 | dependencies { 25 | implementation "joda-time:joda-time:2.2" 26 | testImplementation "junit:junit:4.12" 27 | } 28 | // end::dependencies[] 29 | 30 | // tag::wrapper[] 31 | // end::wrapper[] 32 | -------------------------------------------------------------------------------- /complete/src/test/java/hello/HelloWorldTests.java: -------------------------------------------------------------------------------- 1 | package hello; 2 | 3 | import java.io.ByteArrayOutputStream; 4 | import java.io.PrintStream; 5 | import java.nio.charset.StandardCharsets; 6 | 7 | import org.junit.Before; 8 | import org.junit.Test; 9 | 10 | import static org.hamcrest.CoreMatchers.containsString; 11 | import static org.junit.Assert.assertThat; 12 | 13 | public class HelloWorldTests { 14 | 15 | private ByteArrayOutputStream baos = new ByteArrayOutputStream(); 16 | private PrintStream ps = new PrintStream(baos); 17 | 18 | @Before 19 | public void setup() { 20 | System.setOut(ps); 21 | } 22 | 23 | @Test 24 | public void shouldPrintTimeToConsole() { 25 | HelloWorld.main(new String[] { }); 26 | 27 | assertThat(output(), containsString("The current local time is")); 28 | } 29 | 30 | @Test 31 | public void shouldPrintHelloWorldToConsole() { 32 | HelloWorld.main(new String[] { }); 33 | 34 | assertThat(output(), containsString("Hello world!")); 35 | } 36 | 37 | private String output() { 38 | return new String(baos.toByteArray(), StandardCharsets.UTF_8); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Jenkinsfile: -------------------------------------------------------------------------------- 1 | pipeline { 2 | agent none 3 | 4 | triggers { 5 | pollSCM 'H/10 * * * *' 6 | } 7 | 8 | options { 9 | disableConcurrentBuilds() 10 | buildDiscarder(logRotator(numToKeepStr: '14')) 11 | } 12 | 13 | stages { 14 | stage("test: baseline (jdk8)") { 15 | agent { 16 | docker { 17 | image 'adoptopenjdk/openjdk8:latest' 18 | args '-v $HOME/.m2:/tmp/jenkins-home/.m2' 19 | } 20 | } 21 | options { timeout(time: 30, unit: 'MINUTES') } 22 | steps { 23 | sh 'test/run.sh' 24 | } 25 | } 26 | 27 | } 28 | 29 | post { 30 | changed { 31 | script { 32 | slackSend( 33 | color: (currentBuild.currentResult == 'SUCCESS') ? 'good' : 'danger', 34 | channel: '#sagan-content', 35 | message: "${currentBuild.fullDisplayName} - `${currentBuild.currentResult}`\n${env.BUILD_URL}") 36 | emailext( 37 | subject: "[${currentBuild.fullDisplayName}] ${currentBuild.currentResult}", 38 | mimeType: 'text/html', 39 | recipientProviders: [[$class: 'CulpritsRecipientProvider'], [$class: 'RequesterRecipientProvider']], 40 | body: "${currentBuild.fullDisplayName} is reported as ${currentBuild.currentResult}") 41 | } 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /complete/gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /complete/gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | https://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "{}" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright {yyyy} {name of copyright owner} 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | https://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /README.adoc: -------------------------------------------------------------------------------- 1 | :spring_boot_version: 1.5.10.RELEASE 2 | :jdk: https://www.oracle.com/technetwork/java/javase/downloads/index.html 3 | :toc: 4 | :icons: font 5 | :source-highlighter: prettify 6 | :project_id: gs-gradle 7 | 8 | # This repository is no longer maintained. 9 | 10 | This guide walks you through using Gradle to build a simple Java project. 11 | 12 | == What you'll build 13 | 14 | You'll create a simple app and then build it using Gradle. 15 | 16 | 17 | == What you'll need 18 | 19 | - About 15 minutes 20 | - A favorite text editor or IDE 21 | - {jdk}[JDK 6] or later 22 | 23 | 24 | include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/main/how_to_complete_this_guide.adoc[] 25 | 26 | 27 | [[scratch]] 28 | == Set up the project 29 | 30 | First you set up a Java project for Gradle to build. To keep the focus on Gradle, make the project as simple as possible for now. 31 | 32 | include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/main/create_directory_structure_hello.adoc[] 33 | 34 | 35 | Within the `src/main/java/hello` directory, you can create any Java classes you want. For simplicity's sake and for consistency with the rest of this guide, Spring recommends that you create two classes: `HelloWorld.java` and `Greeter.java`. 36 | 37 | `src/main/java/hello/HelloWorld.java` 38 | [source,java,tabsize=2] 39 | ---- 40 | include::initial/src/main/java/hello/HelloWorld.java[] 41 | ---- 42 | 43 | `src/main/java/hello/Greeter.java` 44 | [source,java,tabsize=2] 45 | ---- 46 | include::initial/src/main/java/hello/Greeter.java[] 47 | ---- 48 | 49 | 50 | [[initial]] 51 | == Install Gradle 52 | 53 | Now that you have a project that you can build with Gradle, you can install Gradle. 54 | 55 | It's highly recommended to use an installer: 56 | 57 | - https://sdkman.io/[SDKMAN] 58 | - https://brew.sh[Homebrew] (brew install gradle) 59 | 60 | As a last resort, if neither of these tools suit your needs, you can download the binaries from https://www.gradle.org/downloads. Only the binaries are required, so look for the link to gradle-_version_-bin.zip. (You can also choose gradle-_version_-all.zip to get the sources and documentation as well as the binaries.) 61 | 62 | Unzip the file to your computer, and add the bin folder to your path. 63 | 64 | To test the Gradle installation, run Gradle from the command-line: 65 | 66 | ---- 67 | gradle 68 | ---- 69 | 70 | If all goes well, you see a welcome message: 71 | 72 | .... 73 | :help 74 | 75 | Welcome to Gradle 6.0.1. 76 | 77 | To run a build, run gradle ... 78 | 79 | To see a list of available tasks, run gradle tasks 80 | 81 | To see a list of command-line options, run gradle --help 82 | 83 | To see more detail about a task, run gradle help --task 84 | 85 | For troubleshooting, visit https://help.gradle.org 86 | 87 | Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0. 88 | Use '--warning-mode all' to show the individual deprecation warnings. 89 | See https://docs.gradle.org/6.0.1/userguide/command_line_interface.html#sec:command_line_warnings 90 | 91 | BUILD SUCCESSFUL in 455ms 92 | 1 actionable task: 1 executed 93 | .... 94 | 95 | You now have Gradle installed. 96 | 97 | 98 | == Find out what Gradle can do 99 | Now that Gradle is installed, see what it can do. Before you even create a build.gradle file for the project, you can ask it what tasks are available: 100 | 101 | ---- 102 | gradle tasks 103 | ---- 104 | 105 | You should see a list of available tasks. Assuming you run Gradle in a folder that doesn't already have a _build.gradle_ file, you'll see some very elementary tasks such as this: 106 | 107 | .... 108 | :tasks 109 | 110 | ------------------------------------------------------------ 111 | Tasks runnable from root project 112 | ------------------------------------------------------------ 113 | 114 | Build Setup tasks 115 | ----------------- 116 | init - Initializes a new Gradle build. 117 | wrapper - Generates Gradle wrapper files. 118 | 119 | Help tasks 120 | ---------- 121 | buildEnvironment - Displays all buildscript dependencies declared in root project 'gs-gradle'. 122 | components - Displays the components produced by root project 'gs-gradle'. [incubating] 123 | dependencies - Displays all dependencies declared in root project 'gs-gradle'. 124 | dependencyInsight - Displays the insight into a specific dependency in root project 'gs-gradle'. 125 | dependentComponents - Displays the dependent components of components in root project 'gs-gradle'. [incubating] 126 | help - Displays a help message. 127 | model - Displays the configuration model of root project 'gs-gradle'. [incubating] 128 | outgoingVariants - Displays the outgoing variants of root project 'gs-gradle'. 129 | projects - Displays the sub-projects of root project 'gs-gradle'. 130 | properties - Displays the properties of root project 'gs-gradle'. 131 | tasks - Displays the tasks runnable from root project 'gs-gradle'. 132 | 133 | To see all tasks and more detail, run gradle tasks --all 134 | 135 | To see more detail about a task, run gradle help --task 136 | 137 | Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0. 138 | Use '--warning-mode all' to show the individual deprecation warnings. 139 | See https://docs.gradle.org/6.0.1/userguide/command_line_interface.html#sec:command_line_warnings 140 | 141 | BUILD SUCCESSFUL in 477ms 142 | 1 actionable task: 1 executed 143 | .... 144 | 145 | Even though these tasks are available, they don't offer much value without a project build configuration. As you flesh out the `build.gradle` file, some tasks will be more useful. The list of tasks will grow as you add plugins to `build.gradle`, so you'll occasionally want to run **tasks** again to see what tasks are available. 146 | 147 | Speaking of adding plugins, next you add a plugin that enables basic Java build functionality. 148 | 149 | 150 | == Build Java code 151 | Starting simple, create a very basic `build.gradle` file in the you created at the beginning of this guide. Give it just just one line: 152 | 153 | [source,groovy] 154 | ---- 155 | include::initial/build.gradle[] 156 | ---- 157 | 158 | This single line in the build configuration brings a significant amount of power. Run **gradle tasks** again, and you see new tasks added to the list, including tasks for building the project, creating JavaDoc, and running tests. 159 | 160 | You'll use the **gradle build** task frequently. This task compiles, tests, and assembles the code into a JAR file. You can run it like this: 161 | 162 | ---- 163 | gradle build 164 | ---- 165 | 166 | After a few seconds, "BUILD SUCCESSFUL" indicates that the build has completed. 167 | 168 | To see the results of the build effort, take a look in the _build_ folder. Therein you'll find several directories, including these three notable folders: 169 | 170 | * _classes_. The project's compiled .class files. 171 | * _reports_. Reports produced by the build (such as test reports). 172 | * _libs_. Assembled project libraries (usually JAR and/or WAR files). 173 | 174 | The classes folder has .class files that are generated from compiling the Java code. Specifically, you should find HelloWorld.class and Greeter.class. 175 | 176 | At this point, the project doesn't have any library dependencies, so there's nothing in the *dependency_cache* folder. 177 | 178 | The reports folder should contain a report of running unit tests on the project. Because the project doesn't yet have any unit tests, that report will be uninteresting. 179 | 180 | The libs folder should contain a JAR file that is named after the project's folder. Further down, you'll see how you can specify the name of the JAR and its version. 181 | 182 | 183 | == Declare dependencies 184 | 185 | The simple Hello World sample is completely self-contained and does not depend on any additional libraries. Most applications, however, depend on external libraries to handle common and/or complex functionality. 186 | 187 | For example, suppose that in addition to saying "Hello World!", you want the application to print the current date and time. You could use the date and time facilities in the native Java libraries, but you can make things more interesting by using the Joda Time libraries. 188 | 189 | First, change HelloWorld.java to look like this: 190 | 191 | [source,java,tabsize=2] 192 | ---- 193 | package hello; 194 | 195 | import org.joda.time.LocalTime; 196 | 197 | public class HelloWorld { 198 | public static void main(String[] args) { 199 | LocalTime currentTime = new LocalTime(); 200 | System.out.println("The current local time is: " + currentTime); 201 | 202 | Greeter greeter = new Greeter(); 203 | System.out.println(greeter.sayHello()); 204 | } 205 | } 206 | ---- 207 | 208 | Here `HelloWorld` uses Joda Time's `LocalTime` class to get and print the current time. 209 | 210 | If you ran `gradle build` to build the project now, the build would fail because you have not declared Joda Time as a compile dependency in the build. 211 | 212 | For starters, you need to add a source for 3rd party libraries. 213 | 214 | [source,groovy] 215 | ---- 216 | include::complete/build.gradle[tag=repositories] 217 | ---- 218 | 219 | The `repositories` block indicates that the build should resolve its dependencies from the Maven Central repository. Gradle leans heavily on many conventions and facilities established by the Maven build tool, including the option of using Maven Central as a source of library dependencies. 220 | 221 | Now that we're ready for 3rd party libraries, let's declare some. 222 | 223 | [source,groovy] 224 | ---- 225 | include::complete/build.gradle[tag=dependencies] 226 | ---- 227 | 228 | With the `dependencies` block, you declare a single dependency for Joda Time. Specifically, you're asking for (reading right to left) version 2.2 of the joda-time library, in the joda-time group. 229 | 230 | Another thing to note about this dependency is that it is a `compile` dependency, indicating that it should be available during compile-time (and if you were building a WAR file, included in the /WEB-INF/libs folder of the WAR). Other notable types of dependencies include: 231 | 232 | * `implementation`. Required dependencies for compiling the project code, but that will be provided at runtime by a container running the code (for example, the Java Servlet API). 233 | * `testImplementation`. Dependencies used for compiling and running tests, but not required for building or running the project's runtime code. 234 | 235 | Finally, let's specify the name for our JAR artifact. 236 | 237 | [source,groovy] 238 | ---- 239 | include::complete/build.gradle[tag=jar] 240 | ---- 241 | 242 | The `jar` block specifies how the JAR file will be named. In this case, it will render `gs-gradle-0.1.0.jar`. 243 | 244 | Now if you run `gradle build`, Gradle should resolve the Joda Time dependency from the Maven Central repository and the build will succeed. 245 | 246 | 247 | == Build your project with Gradle Wrapper 248 | 249 | The Gradle Wrapper is the preferred way of starting a Gradle build. It consists of a batch script for Windows and a shell script for OS X and Linux. These scripts allow you to run a Gradle build without requiring that Gradle be installed on your system. This used to be something added to your build file, but it's been folded into Gradle, so there is no longer any need. Instead, you simply use the following command. 250 | 251 | ---- 252 | $ gradle wrapper --gradle-version 6.0.1 253 | ---- 254 | 255 | After this task completes, you will notice a few new files. The two scripts are in the root of the folder, while the wrapper jar and properties files have been added to a new `gradle/wrapper` folder. 256 | 257 | └── 258 | └── gradlew 259 | └── gradlew.bat 260 | └── gradle 261 | └── wrapper 262 | └── gradle-wrapper.jar 263 | └── gradle-wrapper.properties 264 | 265 | The Gradle Wrapper is now available for building your project. Add it to your version control system, and everyone that clones your project can build it just the same. It can be used in the exact same way as an installed version of Gradle. Run the wrapper script to perform the build task, just like you did previously: 266 | 267 | ---- 268 | ./gradlew build 269 | ---- 270 | 271 | The first time you run the wrapper for a specified version of Gradle, it downloads and caches the Gradle binaries for that version. The Gradle Wrapper files are designed to be committed to source control so that anyone can build the project without having to first install and configure a specific version of Gradle. 272 | 273 | At this stage, you will have built your code. You can see the results here: 274 | 275 | ---- 276 | build 277 | ├── classes 278 | │   └── main 279 | │   └── hello 280 | │   ├── Greeter.class 281 | │   └── HelloWorld.class 282 | ├── dependency-cache 283 | ├── libs 284 | │   └── gs-gradle-0.1.0.jar 285 | └── tmp 286 | └── jar 287 | └── MANIFEST.MF 288 | ---- 289 | 290 | Included are the two expected class files for `Greeter` and `HelloWorld`, as well as a JAR file. Take a quick peek: 291 | 292 | ---- 293 | $ jar tvf build/libs/gs-gradle-0.1.0.jar 294 | 0 Fri May 30 16:02:32 CDT 2014 META-INF/ 295 | 25 Fri May 30 16:02:32 CDT 2014 META-INF/MANIFEST.MF 296 | 0 Fri May 30 16:02:32 CDT 2014 hello/ 297 | 369 Fri May 30 16:02:32 CDT 2014 hello/Greeter.class 298 | 988 Fri May 30 16:02:32 CDT 2014 hello/HelloWorld.class 299 | ---- 300 | 301 | The class files are bundled up. It's important to note, that even though you declared joda-time as a dependency, the library isn't included here. And the JAR file isn't runnable either. 302 | 303 | To make this code runnable, we can use gradle's `application` plugin. Add this to your `build.gradle` file. 304 | 305 | ---- 306 | apply plugin: 'application' 307 | 308 | mainClassName = 'hello.HelloWorld' 309 | ---- 310 | 311 | Then you can run the app! 312 | 313 | ---- 314 | $ ./gradlew run 315 | :compileJava UP-TO-DATE 316 | :processResources UP-TO-DATE 317 | :classes UP-TO-DATE 318 | :run 319 | The current local time is: 16:16:20.544 320 | Hello world! 321 | 322 | BUILD SUCCESSFUL 323 | 324 | Total time: 3.798 secs 325 | ---- 326 | 327 | To bundle up dependencies requires more thought. For example, if we were building a WAR file, a format commonly associated with packing in 3rd party dependencies, we could use gradle's 328 | https://www.gradle.org/docs/current/userguide/war_plugin.html[WAR plugin]. If you are using Spring Boot and want a runnable JAR file, the https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using-boot-gradle[spring-boot-gradle-plugin] is quite handy. At this stage, gradle doesn't know enough about your system 329 | to make a choice. But for now, this should be enough to get started using gradle. 330 | 331 | To wrap things up for this guide, here is the completed `build.gradle` file: 332 | 333 | `build.gradle` 334 | [source,gradle] 335 | ---- 336 | include::complete/build.gradle[] 337 | ---- 338 | 339 | NOTE: There are many start/end comments embedded here. This makes it possible to extract bits of the build file into this guide for the detailed explanations above. You don't need 340 | them in your production build file. 341 | 342 | == Summary 343 | 344 | Congratulations! You have now created a simple yet effective Gradle build file for building Java projects. 345 | 346 | == See Also 347 | 348 | The following guide may also be helpful: 349 | 350 | * https://spring.io/guides/gs/maven/[Building Java Projects with Maven] 351 | 352 | include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/main/footer.adoc[] 353 | --------------------------------------------------------------------------------