Skip to content

How to start a new backend web project in Eclipse with Gradle?

I have recently pivoted into backends and I have to create a backend for a web application. It will be with the following config:

  • IDE: Eclipse
  • Build Tool: Gradle (or Maven)
  • Jakarta EE 9
  • REST Implementation: Jersey 3
  • Jakarta Servlet: 5.0
  • Server: Tomcat 10
  • Language: Java 11
  • Dynamic Web Module Version: 5.0

I tried creating with the Dynamic Web Project and Gradle Project in Eclipse and by reading the guide here but am unable to correctly get all the features properly. I would like a step-by-step guide on how to do this.

Also, I am unsure whether to use Gradle or Maven for this. I have experience with Gradle as I have made Android apps but all the tutorials for Jersey use Maven.

Answer

It doesn’t really matter whether you use Maven or Gradle: both will do the job. However I would advice against using Jakarta EE 9 for now: the Eclipse plugins still have some quirks when dealing with it. E.g. you can set the Servlet API for an Eclipse project to 5.0, but Eclipse will refuse to deploy it on a server.

To startup with Jersey you just need to:

  1. Create a Dynamic Web Project (version 4.0) and create a web.xml like this:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
    id="WebApp_ID" version="4.0">
    <display-name>gradle-jersey</display-name>
    <!-- No class name, Jersey will pick it up -->
    <servlet>
        <servlet-name>javax.ws.rs.core.Application</servlet-name>
    </servlet>
    <servlet-mapping>
        <servlet-name>javax.ws.rs.core.Application</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>
  1. In the project’s contextual menu run Configure > Add gradle nature (I assume you have installed the Buildship plugin),
  2. Create a build.gradle file with content:
plugins {
    id 'war'
}
repositories {
    mavenCentral()
}
dependencies {
    implementation group: 'org.glassfish.jersey.containers', name: 'jersey-container-servlet', version: '2.34'
    implementation group: 'org.glassfish.jersey.inject', name: 'jersey-hk2', version: '2.34'
}
eclipse.wtp.facet {
    // Change the version of the Dynamic Web Module facet
    facet name: 'jst.web', version: '4.0'
    def oldJstWebFacet = facets.findAll {
        it.name == 'jst.web' && it.version == '2.4'
    }
    facets.removeAll(oldJstWebFacet)
    // Add the JAX-RS (REST Web Services) facet
    facet name: 'jst.jaxrs', version: '2.1'
}
  1. In the project’s contextual menu run Gradle > Refresh Gradle Project,
  2. Eclipse should now have Gradle’s dependencies in its Build path…,
  3. You can create a simple JAX-RS resource:
@Path(value = "/hello")
public class Hello {

   @GET
   public String greet() {
      return "Hello world!";
   }
}
  1. You can use “Run > Run on a server” to run the project. Your resource will be under the http://localhost:8080/<project_name>/hello URL.