this is a cheatsheet that guides the reader through creating a simple hello world web application and deploying the app on heroku with the leanest possible set of steps. it assumes you have eclipse, gradle, git, and the heroku toolbelt installed on your development workstation.
setup
create project directory under workspace
- cd [workspace]
- mkdir [project name]
create build.gradle
- cd [project name]
- touch build.gradle
- open build.gradle and add:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.3.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
jar {
baseName = 'hello'
version = '0.1.0'
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile("org.springframework.boot:spring-boot-starter-web") {
exclude module: "spring-boot-starter-tomcat"
}
compile("org.springframework.boot:spring-boot-starter-jetty")
compile("org.springframework.boot:spring-boot-starter-actuator")
testCompile("junit:junit")
}
task wrapper(type: Wrapper) {
gradleVersion = '2.11'
}
- run ‘gradle wrapper’
- run ‘gradlew build’
you may see an exception here. just ignore it.
- run ‘gradlew eclipse’
import into eclipse
- open eclipse
- select file->import
- select general->existing projects into workspace
- browse for root directory [workspace]/[project name]
- click ‘finish’
- create directory ‘[project name]/src/main/java’
- create directory ‘[project name]/src/main/resources’
- create directory ‘[project name]/src/test/java’
- create directory ‘[project name]/src/test/resources’
- rerun ‘gradle eclipse’
create an app
create Application.class
- right-click ‘src/main/java’
- select ‘new->class’
- under ‘Package’ type ‘org.mike’
- under ‘Name’ type ‘Application’
- click ‘Finish’
- add the following to the application class file:
package org.mike;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
create controller
- right-click ‘src/main/java’
- select ‘new->class’
- under ‘Package’ type ‘org.mike.controllers’
- under ‘Name’ type ‘HelloController’
- click ‘Finish’
- add the following to the controller class file:
package org.mike.controllers;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
@RestController
public class HelloController {
@RequestMapping("/hello")
public String index() {
return "Hello Heroku!";
}
}
run the app from eclipse
- right-click Application.class and select ‘Run As->Java Application’
- in browser, visit: http://localhost:8080/hello
git
- cd [workspace]/[project name]
- run ‘git init’ (initializes new git repo)
- touch .gitignore (files for git to ignore)
- add to .gitignore:
/build/
/bin/
/.gradle/
/.settings/
- run ‘git add -A’ (add all not-ignored files to source control and stage them)
- run ‘git commit -m “Initial commit”’ (commit or ‘check-in’ all files in staging area)
heroku
create new heroku app and procfile
- cd [workspace]/[project name]
- run ‘heroku login’
- run ‘heroku create’ (this creates a new ‘app’ on heroku)
- add ‘stage’ task to build.gradle
task stage {
dependsOn build
}
- create file named ‘Procfile’ under [workspace]/[project name] (tells heroku how to run app)
- add to Procfile:
web: java -Dserver.port=$PORT -jar build/libs/units-0.1.0.jar
- create file named ‘Procfile.windows’ under [workspace]/[project name]
- add to Procfile.windows:
web: java -Dserver.port=%PORT% -jar build/libs/units-0.1.0.jar
run app locally
- run ‘gradlew stage’
- run ‘heroku local web -f Procfile.windows’ (starts app locally using windows Procfile)
- in browser, visit: http://localhost:8080/hello
run app remotely
- run ‘git add -A’
- run ‘git commit -m “Added heroku support”’
- run ‘git push heroku master’ (push branch ‘master’ to heroku remote)
- in browser, visit: http://[heroku domain]/hello
et voila!