Posted In: Build Tools, Gradle

Gradle + Eclipse + Servlet3 + Jetty – Best way to create Servlet3 Gradle project using eclipse

Download example source and eclipse project

Steps

1. Choose http://download.eclipse.org/releases/neon
Then select Buildship gradle plug-in and install

2. Restart eclipse. Now choose New -> Other -> Gradle

3. Add project name

4. Select Gradle Wrapper

5. Click finish

6. Now add following to build.gradle

//Use Gretty plugin for war. Jetty plugin will soon be removed from Gradle
//After apply plugin: ‘java’
apply plugin: ‘war’
apply from: ‘https://raw.github.com/akhikhl/gretty/master/pluginScripts/gretty.plugin’

//Inside repositories
mavenLocal()
mavenCentral()

//Inside dependencies
providedCompile group: ‘javax.servlet’, name: ‘javax.servlet-api’, version: ‘3.0.1’
compile ‘org.apache.logging.log4j:log4j-api:2.7’
compile ‘org.apache.logging.log4j:log4j-core:2.7’

7. Refresh

8. Create following two folders

Under src/main create webapp folder
Under src/main/webapp create WEB-INF folder

9. Create Servlet

@WebServlet(“/Servlet2”)
public class Servlet2 extends HttpServlet {
………

10. Go to Gradle Tasks and run build

In case you get JAVA_HOME or error like Could not find tools.jar. Set JAVA_HOME in following tasks
Project Name – build
Project Name – init
Project Name – wrapper

11. Open Terminal in Eclipse. You can directly open command prompt or console.

In case you are not getting Terminal View

12. run gradle command

In case you get ‘gradle’ is not recognized as an internal or external command error. set PATH on windows or export path on linux.
For me it is available here – set PATH=C:\Users\trupti\.gradle\wrapper\dists\gradle-2.14.1-bin\2r579t5wehc7ew5kc8vfqezww\gradle-2.14.1\bin

In case you get ERROR: JAVA_HOME is not set and no ‘java’ command could be found in your PATH.
set JAVA_HOME=C:\JDK18

Now you should get BUILD SUCCESSFUL

13. run gradle appRun command

E:\workspace_neon\Example201714>gradle appRun
:prepareInplaceWebAppFolder UP-TO-DATE
:createInplaceWebAppFolder
:compileJava
:processResources UP-TO-DATE
:classes
:prepareInplaceWebAppClasses
:prepareInplaceWebApp
:appRun
09:36:22 INFO Jetty 9.2.15.v20160210 started and listening on port 8080
09:36:22 INFO Example201714 runs at:
09:36:22 INFO http://localhost:8080/Example201714
Press any key to stop the server.
> Building 87% > :appRun

14. Test by running in browser

http://localhost:8080/Example201714/Servlet2

Download example source and eclipse project

Tags: ,

by , on January 31st, 2017

  • Categories