Showing posts with label Eclipse. Show all posts
Showing posts with label Eclipse. Show all posts

Sunday, August 7, 2016

cheatsheet: from zero to ‘hello heroku’ using spring boot

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

  1. cd [workspace]
  2. mkdir [project name]

create build.gradle

  1. cd [project name]
  2. touch build.gradle
  3. 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'
}

  1. run ‘gradle wrapper’
  2. run ‘gradlew build’
    you may see an exception here. just ignore it.
  3. run ‘gradlew eclipse’

import into eclipse

  1. open eclipse
  2. select file->import
  3. select general->existing projects into workspace
  4. browse for root directory [workspace]/[project name]
  5. click ‘finish’
  6. create directory ‘[project name]/src/main/java’
  7. create directory ‘[project name]/src/main/resources’
  8. create directory ‘[project name]/src/test/java’
  9. create directory ‘[project name]/src/test/resources’
  10. rerun ‘gradle eclipse’

create an app

create Application.class

  1. right-click ‘src/main/java’
  2. select ‘new->class’
  3. under ‘Package’ type ‘org.mike’
  4. under ‘Name’ type ‘Application’
  5. click ‘Finish’
  6. 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

  1. right-click ‘src/main/java’
  2. select ‘new->class’
  3. under ‘Package’ type ‘org.mike.controllers’
  4. under ‘Name’ type ‘HelloController’
  5. click ‘Finish’
  6. 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

  1. right-click Application.class and select ‘Run As->Java Application’
  2. in browser, visit: http://localhost:8080/hello

git

  1. cd [workspace]/[project name]
  2. run ‘git init’ (initializes new git repo)
  3. touch .gitignore (files for git to ignore)
  4. add to .gitignore:
/build/ 
/bin/ 
/.gradle/ 
/.settings/
  1. run ‘git add -A’ (add all not-ignored files to source control and stage them)
  2. run ‘git commit -m “Initial commit”’ (commit or ‘check-in’ all files in staging area)

heroku

create new heroku app and procfile

  1. cd [workspace]/[project name]
  2. run ‘heroku login’
  3. run ‘heroku create’ (this creates a new ‘app’ on heroku)
  4. add ‘stage’ task to build.gradle
task stage {
  dependsOn build
}
  1. create file named ‘Procfile’ under [workspace]/[project name] (tells heroku how to run app)
  2. add to Procfile:
web: java -Dserver.port=$PORT -jar  build/libs/units-0.1.0.jar
  1. create file named ‘Procfile.windows’ under [workspace]/[project name]
  2. add to Procfile.windows:
web: java -Dserver.port=%PORT% -jar  build/libs/units-0.1.0.jar

run app locally

  1. run ‘gradlew stage’
  2. run ‘heroku local web -f Procfile.windows’ (starts app locally using windows Procfile)
  3. in browser, visit: http://localhost:8080/hello

run app remotely

  1. run ‘git add -A’
  2. run ‘git commit -m “Added heroku support”’
  3. run ‘git push heroku master’ (push branch ‘master’ to heroku remote)
  4. in browser, visit: http://[heroku domain]/hello
et voila!

Monday, March 12, 2007

My Development Environment

This is my development evironment:

  • Eclipse 3.2.2 - Eclipse is a really good IDE with a wealth of plugins. Netbeans has good JSP/Servlet/EJB wizards and integration with Glassfish, but Eclipse foundation has countered that with their own Web Tools Platform (WTP) (http://www.eclipse.org/webtools/main.php). The only problem is that it is a project to install and configure WTP with an existing Eclipse installation. Better to start with the complete bundled package.
  • Glassfish v1 - I switched from Apache to Glassfish due to problems with Ajax on Apache 5.5. So far I am happy with it, but beware the autodeploy mechanism. Some old Javascript became stuck in one of the directories, and it was very difficult to figure out why I could not update it. Eventually, I found the directory with the old .js file and deleted it. Now I just use asadmin deploydir and undeploy.
  • Apache 5.5 - I have used Apache since the beginning. There is a good Eclipse plugin that made servlet development so much easier. The reason I started something else was becuase of the Ajax problem. Maybe v6 will clear up the problem.
  • JDK 6.0 - Just keeping up with the latest.