ZOFTINO.COM android and web dev tutorials

Java Application Development using Eclipse and Gradle

In this tutorial, details about developing Java jar and web applications in Eclipse using Gradle as build tool are provided. It also covers details about deploying java web application to tomcat.

Table of Contents

Install Eclipse Gradle Plug-in

First install latest Java SDK and Eclipse on your machine. Then install eclipse gradle plug-in if it is not already installed. To do that, go to Help menu and then click Eclipse Market Place menu item. In the eclipse market place window, search for gradle and click install button which is part of Buildship Gradle Integration 2.0 search results item.

Eclipse gradle plug-in installation

Create Java Project using Gradle

Then create java project. To do that, go to file menu, click new and then other menu items, in the new project wizards window, expand gradle and select gradle project, click next, again click next in the following screen, enter project name in the next screen, and click finish.

eclipse new gradle project

Following images shows the structure of java project created in eclipse using gradle.

eclipse gradle java project structure

Build Jar using Gradle in Eclipse

After adding classes to src/main/java folder, do a build. By default, the gradle build file build.gradle contains necessary configuration to build java application. If you want to use any libraries in your project, add them to build.gradle file.

Here is the contents of build.gradle file. Plug-in java-library takes care of compiling and creating jar file.

java-library plugin to add support for Java Library
apply plugin: 'java-library'

repositories {
    // for resolving your dependencies.

dependencies {
    // This dependency is exported to consumers
    api 'org.apache.commons:commons-math3:3.6.1'

    // This dependency is used internally
    implementation 'com.google.guava:guava:23.0'

    // Use JUnit test framework
    testImplementation 'junit:junit:4.12'

Then to compile, test and package the project, click Run menu, then click Run Configurations, and double click Gradle Project option.

eclipse gradle create run configuration

In the run configuration window, enter configuration name, select working directory by clicking workspace button, enter build as gradle task, and click apply and run.

eclipse gradle run configuration

Once gradle build is done, you can find compiled classes and jar file in build folder under the project root.

eclipse gradle build jar

Creating Web Application Project

To create web application project in eclipse using gradle, you need to first create java application following the steps provided in the previous section.

Once java project has been created, open build.gradle file and remove apply plugin: 'java-library' add following gradle plugin entries.

apply plugin: 'java'
apply plugin: 'war'

Then add servelet dependency by adding following servlet dependency entry to build.gradle file.

compile group: 'javax.servlet', name: 'javax.servlet-api', version: '4.0.1'

After adding the dependency entry to build.gradle file, refresh the project by right clicking the project, then clicking Gradle in the context menu and then clicking Refresh Gradle Project.

Then add webapp folder under src/main folder and add WEB-INF folder under webapp. Now you can add java, resources and jsp files like you do in java web applications.

For example, here is a servlet from our example web application.

public class DealsController extends HttpServlet {

    public void doGet(HttpServletRequest request,HttpServletResponse response)
        throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    out.println("flat 90% off on jewellery, ");
    out.println("upto 40% off on mobiles");

To build web application, you need to open gradle-tasks eclipse view. To open it, click Window menu, click Show View and then Other menu items to get show view window.

gradle eclipse views

Then expand gradle folder in the widow, select gradle tasks and then click open. You can see the gradle tasks and gradle executions views at the bottom of eclipse.

gradle tasks eclipse view

Then right click build item which exists under build folder in gradle tasks view and click Run Gradle Tasks menu item.

run gradle build tasks in eclipse

Here is the output from running the gradle build.

JVM Arguments: None
Program Arguments: None
Build Scans Enabled: false
Offline Mode Enabled: false
Gradle Tasks: build

:processResources NO-SOURCE
:processTestResources NO-SOURCE

4 actionable tasks: 4 executed

After successful run of the gradle build, it will create war file in build directory.

Deploying War to Tomcat Using Gradle Tomcat Plug-in

You can deploy the war to tomcat using eclipse tomcat plug-in, for details see deploying war to tomcat from eclipse tutorial.

To deploy web application to tomcat using gradle tomcat plug-in, we need to add tomcat plug-in and related dependencies to build.gradle file. You can find latest version of tomcat embed dependencies here.

Here is the complete build.gradle file after adding tomcat dependencies.

 apply plugin: 'java'
 apply plugin: 'war'
 apply plugin: 'com.bmuschko.tomcat'
 apply plugin: 'eclipse-wtp'

 repositories {

 buildscript {
  repositories {
  dependencies {
    classpath 'com.bmuschko:gradle-tomcat-plugin:2.5'

 dependencies {
    implementation 'com.google.guava:guava:23.0'
    testImplementation 'junit:junit:4.12'
    compile group: 'javax.servlet', name: 'javax.servlet-api', version: '4.0.1'
	def tomcatVersion = '8.0.53'
    tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}",

Now to deploy the web application to tomcat, go to run configurations and enter tomcatRun task as shown in the following window and then click apply and ok.

eclipse gradle war deployment to tomcat using gradle tomcat plugin

Here is the output from tomcatRun gradle task. It prints url to access your web app running locally on tomcat in eclipse.

:compileJava UP-TO-DATE
:processResources NO-SOURCE
:classes UP-TO-DATE
Started Tomcat Server
The Server is running at http://localhost:8080/deals

Our example servlet can be accessed using http://localhost:8080/deals/latest.

For building jar and web projects using maven, see using maven in eclipse and running maven web application on tomcat in eclipse.