Git Product home page Git Product logo

devcenter-gradle's Introduction

This quickstart will get you going with Java apps on the Heroku Cedar stack using the Gradle build system and Jetty embedded web server.

Sample code is available on github along with this article. Edits and enhancements are welcome. Just fork the repository, make your changes and send us a pull request.

Prerequisites

  • Basic Java knowledge, including an installed version of the JVM.
  • Basic Gradle knowledge, including an installed version of Gradle (1.0-milestone-5 or later).
  • Your application must run on the OpenJDK version 6.
  • A Heroku user account. Signup is free and instant.

Local Workstation Setup

We'll start by setting up your local workstation with the Heroku command-line client and the Git revision control system; and then logging into Heroku to upload your ssh public key. If you've used Heroku before and already have a working local setup, skip to the next section.

If you have... Install with...
Mac OS X Download OS X package
Windows Download Windows .exe installer
Ubuntu Linux apt-get repository
Other Tarball (add contents to your $PATH)

Once installed, you'll have access to the heroku command from your command shell. Log in using the email address and password you used when creating your Heroku account:

:::term
$ heroku login
Enter your Heroku credentials.
Email: [email protected]
Password: 
Could not find an existing public key.
Would you like to generate one? [Yn] 
Generating new SSH public key.
Uploading ssh public key /Users/adam/.ssh/id_rsa.pub

Press enter at the prompt to upload your existing ssh key or create a new one, used for pushing code later on.

Write Your App

We will be creating a completely standard Java application that serves web requests using the Servlet API and the embedded Jetty web server.

First create a class that implements a simple Servlet. For the purpose of this example, we'll also create the main method in this class.

src/main/java/HelloWorld.java

:::java
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.*;

public class HelloWorld extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        resp.getWriter().print("Hello from Java!\n");
    }

    public static void main(String[] args) throws Exception{
        Server server = new Server(Integer.valueOf(System.getenv("PORT")));
        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
        context.setContextPath("/");
        server.setHandler(context);
        context.addServlet(new ServletHolder(new HelloWorld()),"/*");
        server.start();
        server.join();   
    }
}

Set up the build

Create a Gradle build file in your project root. The stage task will be run by Heroku to build your app:

build.gradle

apply plugin:'java'
apply plugin:'application'

mainClassName = "HelloWorld"
applicationName = "app"

repositories {
    mavenCentral()
}

dependencies {
    compile 'org.eclipse.jetty:jetty-servlet:7.4.5.v20110725'
    compile 'javax.servlet:servlet-api:2.5'
}

task stage(dependsOn: ['clean', 'installApp'])

Prevent build artifacts from going into revision control by creating this file:

.gitignore

:::term
build
.gradle

Build Your App

Build your app locally:

:::term
$ gradle stage

Declare Process Types With Foreman/Procfile

To run your web process, you need to declare what command to use. We'll use Procfile to declare how our web process type is run. The application plugin takes care of generating a run script, build/install/app/bin/app, which we'll use to start the web app.

Here's what the Procfile looks like:

:::term
web: ./build/install/app/bin/app

Now that you have a Procfile, you can start your application with Foreman:

:::term
$ foreman start
11:01:26 web.1     | started with pid 68657
11:01:27 web.1     | 2011-10-13 11:01:27.033:INFO:oejs.Server:jetty-7.5.3.v20111011
11:01:27 web.1     | 2011-10-13 11:01:27.229:INFO:oejsh.ContextHandler:started o.e.j.s.ServletContextHandler{/,null}
11:01:27 web.1     | 2011-10-13 11:01:27.269:INFO:oejs.AbstractConnector:Started [email protected]:5000 STARTING

Your app will come up on port 5000, the default port set by Foreman. Test that it's working with curl or a web browser, then Ctrl-C to exit.

Store Your App in Git

We now have the three major components of our app: build configuration and dependencies in build.gradle, process types in Procfile, and our application source in src/main/java/HelloWorld.java. Let's put it into Git:

:::term
$ git init
$ git add .
$ git commit -m "init"

Deploy to Heroku/Cedar

Create the app on the Cedar stack:

:::term
$ heroku create -s cedar
Creating evening-sky-2099... done, stack is cedar
http://evening-sky-2099.herokuapp.com/ | [email protected]:evening-sky-2099.git

Deploy your code:

:::term
$ git push heroku master
Counting objects: 18, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (10/10), 1.59 KiB, done.
Total 10 (delta 2), reused 0 (delta 0)

-----> Heroku receiving push
-----> Java/Gradle app detected
-----> Installing gradle-1.0-milestone-5..... done
       (Use the Gradle Wrapper if you want to use a different gradle version)
-----> executing gradle -I /tmp/asdre342qfad/opt/init.gradle stage
       :compileJava
       Download http://s3pository.heroku.com/jvm/org/eclipse/jetty/jetty-servlet/7.5.3.v20111011/jetty-servlet-7.5.3.v20111
       ...
       :processResources UP-TO-DATE
       :classes
       :jar
       :startScripts
       :installApp
   
       BUILD SUCCESSFUL
   
       Total time: 20.534 secs
-----> Discovering process types
       Procfile declares types -> web
-----> Compiled slug size is 936K
-----> Launching... done, v5
       http://blazing-planet-4614.herokuapp.com deployed to Heroku

Now, let's check the state of the app's processes:

:::term
$ heroku ps
Process       State               Command
------------  ------------------  ------------------------------
web.1         up for 1m           ./build/install/app/bin/app

The web process is up. Review the logs for more information:

:::term
$ heroku logs
2011-10-13T18:06:23+00:00 heroku[api]: Deploy 7c70d13 by [email protected]
2011-10-13T18:06:23+00:00 heroku[api]: Release v5 created by [email protected]
2011-10-13T18:06:24+00:00 heroku[slugc]: Slug compilation finished
2011-10-13T18:06:43+00:00 heroku[web.1]: Unidling
2011-10-13T18:06:43+00:00 heroku[web.1]: State changed from down to created
2011-10-13T18:06:43+00:00 heroku[web.1]: State changed from created to starting
2011-10-13T18:06:44+00:00 heroku[web.1]: Starting process with command `./build/install/app/bin/app`
2011-10-13T18:06:45+00:00 app[web.1]: 2011-10-13 18:06:45.198:INFO:oejs.Server:jetty-7.5.3.v20111011
2011-10-13T18:06:45+00:00 app[web.1]: 2011-10-13 18:06:45.258:INFO:oejsh.ContextHandler:started o.e.j.s.ServletContextHandler{/,null}
2011-10-13T18:06:45+00:00 app[web.1]: 2011-10-13 18:06:45.293:INFO:oejs.AbstractConnector:Started [email protected]:27922 STARTING
2011-10-13T18:06:46+00:00 heroku[web.1]: State changed from starting to up

Looks good. We can now visit the app with heroku open.

devcenter-gradle's People

Contributors

bigdaz avatar jsimone avatar ryanbrainard avatar sclasen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

devcenter-gradle's Issues

References to Maven/Appassembler

The section "Declare Process Types With Foreman/Procfile" talks about the Maven appassembler plugin instead of Gradle's "application" plugin. The steps are correct, but the intro text needs to be updated.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.