Convert guide to gradle

This commit is contained in:
Greg Turnquist 2013-08-16 13:36:29 -05:00
parent 42242c179b
commit 90339b8288
3 changed files with 83 additions and 82 deletions

View File

@ -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 <firstName: JILL, lastName: DOE> in the database.
Found <firstName: JOE, lastName: DOE> in the database.
Found <firstName: JUSTIN, lastName: DOE> in the database.
Found <firstName: JANE, lastName: DOE> in the database.
Found <firstName: JOHN, lastName: DOE> in the database.
```
Summary
-------

141
README.md
View File

@ -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:
<a name="scratch"></a>
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
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
`build.gradle`
```gradle
buildscript {
repositories {
maven { url "http://repo.springsource.org/libs-snapshot" }
mavenLocal()
}
}
<groupId>org.springframework</groupId>
<artifactId>gs-batch-processing</artifactId>
<version>0.1.0</version>
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>0.5.0.BUILD-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
</dependency>
</dependencies>
jar {
baseName = 'gs-batch-processing'
version = '0.1.0'
}
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
</plugin>
</plugins>
</build>
repositories {
mavenCentral()
maven { url "http://repo.springsource.org/libs-snapshot" }
}
<repositories>
<repository>
<id>spring-snapshots</id>
<url>http://repo.springsource.org/libs-snapshot</url>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
dependencies {
compile("org.springframework.boot:spring-boot-starter-batch:0.5.0.BUILD-SNAPSHOT")
compile("org.hsqldb:hsqldb")
testCompile("junit:junit:4.11")
}
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<url>http://repo.springsource.org/libs-snapshot</url>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
</project>
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
<properties>
<start-class>hello.Application</start-class>
</properties>
`build.gradle`
```groovy
buildscript {
...
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:0.5.0.BUILD-SNAPSHOT")
}
}
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
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 <firstName: JILL, lastName: DOE> in the database.
Found <firstName: JOE, lastName: DOE> in the database.
Found <firstName: JUSTIN, lastName: DOE> in the database.
Found <firstName: JANE, lastName: DOE> in the database.
Found <firstName: JOHN, lastName: DOE> in the database.
```
Summary
-------

View File

@ -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'