Start JEE Project with Gradle

#1 Setup JEE Project with Gradle
  • Create an empty folder
  • mkdir jee-test
  • Go to the new empty folder and set your JEE project with gradle, you can install gradle if you dont have it.
  • gradle init --type java-application 
  • Create the missed up folders with 
  • mkdir -p  src/main/webapp/{css,js,images}
#2 Open Eclipse
  • Start eclipse JEE, make sure you setup your eclipse like in link to setup eclipse
  • Now go to Menu: "File → Import" and choose "Existing Gradle Project"
  • Choose your source code directory and click on Finish
  • Open build.gradle clean it and make some changes and save
  • 
     plugins {
         id 'java'
         id 'war'
         id 'org.gretty' version '2.3.1'
     }
     dependencies {
         providedCompile 'javax.servlet:javax.servlet-api:4.0.1'
     }
     gretty {
       httpPort = 8080
       contextPath = '/'
     }
     repositories {
        jcenter()
     }
    

#3 Clean up and add some Resources
  • Go into src/main/java and delete jee.test.App.java, you can keep the package name
  • Go into src/ and delete the whole test directory as shown in the screenshot
  • Open Project properties "CMD+I" or Right click on your Project and choose "Properties"
  • Navigate to Project Facets and click on convert to faceted form...
  • Now choose all the Project Facets as shown in the screenshot bellow, and click on "Further configuration available..."
  • After clicking on "Further configuration available..." Enter "src/main/webapp" in the Content directory filed, and click OK
  • Now click on Apply and Close
  • Right Click on your Project and choose "Gradle → Refresh Gradle Project"
  • Now add a new JSP "index.jsp" to your webapp folder
  • 
    <%@ page language="java" contentType="text/html; charset=UTF-8"
     pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Home Page</title>
    </head>
    <body>
     <%
      for (int i = 0; i < 5; i++) {
     %>
     <h1>Hello from JSP</h1>
     <%
      }
     %>
    </body>
    </html>
    
  • Add a new Servlet, from the Button "Add Servlet"
  • Since we don't care which type of requests we receive (GET,POST, ...), we will only implement the service method.
  • Start your Application Server with ./gradlew appRun
  • Open at http://localhost8080

Comments