Google AppEngine with Gradle & IntelliJ
Hi There, after a lot of thought i decided to write my very first post on this topic. Why you ask? here is a small story,
I started developing applications with Google AppEngine (now called AppEngine standard Environment) 4 years back and being a beginner i found Eclipse and Google Eclipse Plugin are very easy to use and adopted it.
As time pass and application complexity increases, i started to feel this toolset is not the better way to develop and started facing lot of problems, some of them are,
- Dependency Management, yea all the dependent jar needs to be manually downloaded and kept under ‘war/libs’ folder, and to update this libraries, have to repeat the same process again.
- Multiple Module, with AppEngine got new feature of having multiple modules under same application, this structure is of no use for code reuse in another module, and EAR structure in eclipse adds even more complexity
Finally decided to look for other alternatives and found out IntelliJ & Gradle sounds great, But found no good beginner guide or tutorials, and learnt to use this in a hard way, years passed and still dint find any good articles for using AppEngine with Gradle in IntelliJ
Okay enough of story, let see how to develop an simple HelloWorld AppEngine Java application using gradle and IntelliJ IDEA, you can find the complete source code here, HelloWorld App.
First you need IntelliJ IDEA Community Edition, this works best for my use, if you don’t have download here, https://www.jetbrains.com/idea/download/
The installation of IntelliJ is pretty straight forward, so you should be fine installing it, i will leave that to you. Once installed, launch it you should see a screen like below after default setup. Click ‘Create New Project’ button in the welcome screen.
Next, Choose Gradle from left menu, select Java 1.7 as Project SDK, (note: if java is not installed in your machine, go ahead and install it first), then Click Next to continue.
In the Next screen, fill in the GroupId and ArtifactId of the project, in this case hello world, then Click Next
In the next screen, i recommend to have settings like below, use default gradle wrapper provided by IntelliJ, then Click Next > Finish to create the project.
Once its created you should see a project structure like in the left, gradle users standard maven java project structure.
Wondering where is war (in gradle is called webapp) and WEB-INF folder, by default it wont create those folder, so go ahead and create those folders.
Alright Next up, we need to include dependencies such as servlet-api, appengine libraries, etc. So we are going to list those dependencies in build.gradle file, replace your build.gradle file content with below,
Lets break down what we are doing here, since this is a java web application project, we are apply gradle plugins ‘java’ and ‘war’, which will add appropriate tasks.
apply plugin: 'java'
apply plugin: 'war'
Next up, we are adding a plugin called gradle-appengine-plugin, this plugins provides tasks for running, uploading and managing AppEngine projects. To use the App Engine plugin, include in your build script:
apply plugin: 'appengine'
For more, read out the plugin documentation in GitHub
And in the dependencies section, we are adding servlet-api and JUnit as our dependencies. Next we need to add web.xml to our project.
here, we just added a servlet HelloWorldServlet and mapped it “/” path.
Then add appengine-web.xml file, which is required for appengine sdk, here you can set your application id, version and other appengine related configurations.
Thats it, all the pieces are in place, with that lets try running this application. To start the appengine devserver, from the terminal (shortly i will tell how to create run configuration for ease use), run gradle task like below,
gradle appengineRun
the ‘appengineRun’ task will start the devserver, by default in port 8080. If everything works well, the devserver should be started and you could see in the console like below,
Mac:HelloWorldGAE Ramesh$ gradle appengineRun
:appengineDownloadSdk
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:war UP-TO-DATE
:appengineExplodeApp UP-TO-DATE
:appengineRun
Executing
Listening for transport dt_socket at address: 8079
Jan 25, 2017 4:08:23 PM com.google.apphosting.utils.jetty.JettyLogger info
INFO: Logging to JettyLogger(null) via com.google.apphosting.utils.jetty.JettyLogger
Jan 25, 2017 10:38:24 AM com.google.apphosting.utils.jetty.JettyLogger info
INFO: jetty-6.1.x
Jan 25, 2017 10:38:24 AM com.google.apphosting.utils.jetty.JettyLogger info
INFO: Started SelectChannelConnector@localhost:8080
Jan 25, 2017 10:38:24 AM com.google.appengine.tools.development.JettyContainerService startHotDeployScanner
INFO: Full scan of the web app in place every 3s.
Jan 25, 2017 10:38:24 AM com.google.appengine.tools.development.AbstractModule startup
INFO: Module instance default is running at http://localhost:8080/
Jan 25, 2017 10:38:24 AM com.google.appengine.tools.development.AbstractModule startup
INFO: The admin console is running at http://localhost:8080/_ah/admin
Jan 25, 2017 4:08:24 PM com.google.appengine.tools.development.DevAppServerImpl doStart
INFO: Dev App Server is now running
Go ahead, and type http://localhost:8080 in your browser, you should see Hello, world,
Yeaaah!! it works!
Creating Run Configuration:
Everytime running the task from terminal is not efficient, so lets create a run configuration to start appengine devserver,
From the toolbar, click “Select Run/Debug Configuration” menu then Click “Edit Configuration”
Then in the Rub/Debug popup, Click “+” icon from the top left, from the dropdown menu, choose “Groovy”
In the Next window, setup configurations like below,
For Scrip Path: choose location of the build.gradle file in HelloWorld project,
For Module: choose current project
Then in Under Before launch: click “+” icon and choose “Run Gradle task”.
You should see a screen like above, here choose current project, and in the
Tasks: enter appengineRun, thats it. Click Ok. Finally Click Apply and Ok
Now you should be seeing a run configuration in the toolbar, Click “Run” to start the devserver.
I Feel this setup is much more better than traditional eclipse with sdk setup.
Thanks for reading!! Code Better!