From 90339b82888030d384c476d672f7173f9fff2897 Mon Sep 17 00:00:00 2001 From: Greg Turnquist Date: Fri, 16 Aug 2013 13:36:29 -0500 Subject: [PATCH] Convert guide to gradle --- README.ftl.md | 21 +++++-- README.md | 141 ++++++++++++++++++++----------------------- initial/build.gradle | 3 - 3 files changed, 83 insertions(+), 82 deletions(-) diff --git a/README.ftl.md b/README.ftl.md index a16b269a98..010531b874 100644 --- a/README.ftl.md +++ b/README.ftl.md @@ -23,9 +23,9 @@ Set up the project <@create_directory_structure_hello/> -### Create a Maven POM +### Create a Gradle build file - <@snippet path="pom.xml" prefix="initial"/> + <@snippet path="build.gradle" prefix="initial"/> <@bootstrap_starter_pom_disclaimer/> @@ -119,12 +119,25 @@ For demonstration purposes, there is code to create a `JdbcTemplate`, query the <@build_an_executable_jar_subhead/> -<@build_an_executable_jar/> +<@build_an_executable_jar_with_gradle/> -<@run_the_application_with_maven module="batch job"/> +<@run_the_application_with_gradle module="batch job"/> The job prints out a line for each person that gets transformed. After the job runs, you can also see the output from querying the database. +```sh +Converting (firstName: Jill, lastName: Doe) into (firstName: JILL, lastName: DOE) +Converting (firstName: Joe, lastName: Doe) into (firstName: JOE, lastName: DOE) +Converting (firstName: Justin, lastName: Doe) into (firstName: JUSTIN, lastName: DOE) +Converting (firstName: Jane, lastName: Doe) into (firstName: JANE, lastName: DOE) +Converting (firstName: John, lastName: Doe) into (firstName: JOHN, lastName: DOE) +Found in the database. +Found in the database. +Found in the database. +Found in the database. +Found in the database. +``` + Summary ------- diff --git a/README.md b/README.md index 38b38a13e3..f84344e918 100644 --- a/README.md +++ b/README.md @@ -12,9 +12,10 @@ What you'll need - About 15 minutes - A favorite text editor or IDE - [JDK 6][jdk] or later - - [Maven 3.0][mvn] or later + - [Gradle 1.7+][gradle] or [Maven 3.0+][mvn] [jdk]: http://www.oracle.com/technetwork/java/javase/downloads/index.html +[gradle]: http://www.gradle.org/ [mvn]: http://maven.apache.org/download.cgi How to complete this guide @@ -39,7 +40,7 @@ To **skip the basics**, do the following: Set up the project ------------------ -First you set up a basic build script. You can use any build system you like when building apps with Spring, but the code you need to work with [Maven](https://maven.apache.org) and [Gradle](http://gradle.org) is included here. If you're not familiar with either, refer to [Building Java Projects with Maven](/guides/gs/maven) or [Building Java Projects with Gradle](/guides/gs/gradle/). +First you set up a basic build script. You can use any build system you like when building apps with Spring, but the code you need to work with [Gradle](http://gradle.org) and [Maven](https://maven.apache.org) is included here. If you're not familiar with either, refer to [Building Java Projects with Gradle](/guides/gs/gradle/) or [Building Java Projects with Maven](/guides/gs/maven). ### Create the directory structure @@ -50,68 +51,44 @@ In a project directory of your choosing, create the following subdirectory struc └── java └── hello -### Create a Maven POM +### Create a Gradle build file -`pom.xml` -```xml - - - 4.0.0 +`build.gradle` +```gradle +buildscript { + repositories { + maven { url "http://repo.springsource.org/libs-snapshot" } + mavenLocal() + } +} - org.springframework - gs-batch-processing - 0.1.0 +apply plugin: 'java' +apply plugin: 'eclipse' +apply plugin: 'idea' - - org.springframework.boot - spring-boot-starter-parent - 0.5.0.BUILD-SNAPSHOT - - - - - org.springframework.boot - spring-boot-starter-batch - - - org.hsqldb - hsqldb - - +jar { + baseName = 'gs-batch-processing' + version = '0.1.0' +} - - - - maven-compiler-plugin - 2.3.2 - - - +repositories { + mavenCentral() + maven { url "http://repo.springsource.org/libs-snapshot" } +} - - - spring-snapshots - http://repo.springsource.org/libs-snapshot - true - - +dependencies { + compile("org.springframework.boot:spring-boot-starter-batch:0.5.0.BUILD-SNAPSHOT") + compile("org.hsqldb:hsqldb") + testCompile("junit:junit:4.11") +} - - - spring-snapshots - http://repo.springsource.org/libs-snapshot - true - - - - +task wrapper(type: Wrapper) { + gradleVersion = '1.7' +} ``` This guide is using [Spring Boot's starter POMs](/guides/gs/spring-boot/). -Note to experienced Maven users who are unaccustomed to using an external parent project: you can take it out later, it's just there to reduce the amount of code you have to write to get started. - ### Create business data Typically your customer or a business analyst supplies a spreadsheet. In this case, you make it up. @@ -451,49 +428,63 @@ For demonstration purposes, there is code to create a `JdbcTemplate`, query the Now that your `Application` class is ready, you simply instruct the build system to create a single, executable jar containing everything. This makes it easy to ship, version, and deploy the service as an application throughout the development lifecycle, across different environments, and so forth. -Add the following configuration to your existing Maven POM: +Add the following configuration to your existing Gradle build file: -`pom.xml` -```xml - - hello.Application - +`build.gradle` +```groovy +buildscript { + ... + dependencies { + classpath("org.springframework.boot:spring-boot-gradle-plugin:0.5.0.BUILD-SNAPSHOT") + } +} - - - - org.springframework.boot - spring-boot-maven-plugin - - - +apply plugin: 'spring-boot' ``` -The `start-class` property tells Maven to create a `META-INF/MANIFEST.MF` file with a `Main-Class: hello.Application` entry. This entry enables you to run it with `mvn spring-boot:run` (or simply run the jar itself with `java -jar`). - -The [Spring Boot maven plugin][spring-boot-maven-plugin] collects all the jars on the classpath and builds a single "über-jar", which makes it more convenient to execute and transport your service. +The [Spring Boot gradle plugin][spring-boot-gradle-plugin] collects all the jars on the classpath and builds a single "über-jar", which makes it more convenient to execute and transport your service. +It also searches for the `public static void main()` method to flag as a runnable class. Now run the following command to produce a single executable JAR file containing all necessary dependency classes and resources: ```sh -$ mvn package +$ ./gradlew build ``` -[spring-boot-maven-plugin]: https://github.com/SpringSource/spring-boot/tree/master/spring-boot-tools/spring-boot-maven-plugin +Now you can run the JAR by typing: + +```sh +$ java -jar build/libs/gs-batch-processing-0.1.0.jar +``` + +[spring-boot-gradle-plugin]: https://github.com/SpringSource/spring-boot/tree/master/spring-boot-tools/spring-boot-gradle-plugin > **Note:** The procedure above will create a runnable JAR. You can also opt to [build a classic WAR file](/guides/gs/convert-jar-to-war/) instead. Run the batch job ------------------- -Run your batch job using the spring-boot plugin at the command line: +Run your batch job at the command line: ```sh -$ mvn spring-boot:run +$ ./gradlew clean build && java -jar build/libs/gs-batch-processing-0.1.0.jar ``` The job prints out a line for each person that gets transformed. After the job runs, you can also see the output from querying the database. +```sh +Converting (firstName: Jill, lastName: Doe) into (firstName: JILL, lastName: DOE) +Converting (firstName: Joe, lastName: Doe) into (firstName: JOE, lastName: DOE) +Converting (firstName: Justin, lastName: Doe) into (firstName: JUSTIN, lastName: DOE) +Converting (firstName: Jane, lastName: Doe) into (firstName: JANE, lastName: DOE) +Converting (firstName: John, lastName: Doe) into (firstName: JOHN, lastName: DOE) +Found in the database. +Found in the database. +Found in the database. +Found in the database. +Found in the database. +``` + Summary ------- diff --git a/initial/build.gradle b/initial/build.gradle index eaa5a04b3e..5a891ae850 100644 --- a/initial/build.gradle +++ b/initial/build.gradle @@ -3,9 +3,6 @@ buildscript { maven { url "http://repo.springsource.org/libs-snapshot" } mavenLocal() } - dependencies { - classpath("org.springframework.boot:spring-boot-gradle-plugin:0.5.0.BUILD-SNAPSHOT") - } } apply plugin: 'java'