2

I want to create a simple java servlet in intellij IDEA.

I saw this page about how to do so,

But how can I make this web-project also a gradle project?

I want to evolve my servlet and add dependencies

I want to runt he servlet later and be able to debug it with breakpoints

Elad Benda2
  • 13,852
  • 29
  • 82
  • 157
  • I want to host it on the web, actually as a chron job on app-engine – Elad Benda2 Sep 30 '16 at 15:49
  • So use something a lot higher-level, such as Spring MVC, so you aren't spending all your time fiddling with the infrastructure instead of the logic. – chrylis -cautiouslyoptimistic- Sep 30 '16 at 15:53
  • Regardless of whatever you're using (Spring MVC, Servlets etc.). Check out: http://stackoverflow.com/questions/26745541/best-way-to-add-gradle-support-to-intellij-project to add gradle support to your IntelliJ project. – Harmelodic Sep 30 '16 at 16:01
  • I would suggest one bite at a time to eat an elephant.1.create servlet (no matter where) 2.Run it in IDEA 3.Add grade support 4.Whatever you want... – Sundararaj Govindasamy Sep 30 '16 at 19:13

1 Answers1

6

First use IntelliJ to create a new Gradle Project. Second create a standard project structure for a webapp like:

src/main/java/yourPackage/yourServlet.java
src/main/webapp/WEB-INF/web.xml
src/main/webapp/index.html

Add the following to your gradle.build file:

apply plugin: 'jetty'
//also applies plugin: 'war'
//and this also applies plugin: 'java'

repositories{
    mavenCentral()
}

dependencies {
    compile 'javax.servlet:javax.servlet-api:3.1.0'
}

Now you can just build your project with the gradle (or gradlew if you use the wrapper) build task and run it with the jettyRun task. If you don't want to use jetty, you can use the war plugin without the jetty plugin and deploy your generated war file on every server you want. The war file will be located in projectRoot/build/libs

See also the user guide of gradle: Chapter 47. Web Application Quickstart

hetec
  • 319
  • 2
  • 5