Exception in running java application from eclipse using gradle as build tool

I have an application which is a web application but I am running it with tomcat embedded. I have created following class to start tomcat :

package com.app.sample;
  import java.io.File;
  import org.apache.catalina.Context;
import org.apache.catalina.Engine;
import org.apache.catalina.Host;
import org.apache.catalina.Wrapper;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.startup.Embedded;
  public class ServerLauncher {
   private static final String DEFAULT_ENGINE = "default";
 private static final String DEFAULT_HOST = "localhost";
 private static final String WEB_APPS_NAME = "WebContent";
 private static final String DOC_BASE = ".";
   private Embedded embedded;
 private String catalinaHome;
   public ServerLauncher() {
  // Register a shutdown hook to do a clean shutdown
  Runtime.getRuntime().addShutdownHook(new Thread() {
   @Override
   public void run() {
    stopServer();
   }
  });
 }
   private void init() throws Exception {
  File home = new File(".").getCanonicalFile();
  catalinaHome = home.getAbsolutePath();
    embedded = new Embedded();
  embedded.setCatalinaHome(catalinaHome);
    // Create an Engine
  Engine engine = embedded.createEngine();
  engine.setName(DEFAULT_ENGINE);
  engine.setDefaultHost(DEFAULT_HOST);
  embedded.addEngine(engine);
    // Create a Host
  File webAppsLocation = new File(home, WEB_APPS_NAME);
  Host host = embedded.createHost(DEFAULT_HOST, webAppsLocation.getAbsolutePath());
  engine.addChild(host);
    // Add the context
  File docBase = new File(webAppsLocation, DOC_BASE);
  Context context = createContext("/gdigui", docBase.getAbsolutePath());
  host.addChild(context);
    // Create a connector that listens on all addresses
  // on port 5050
  Connector connector = embedded.createConnector((String) null, 8080, false);
  connector.setURIEncoding("UTF-8");
      // Wire up the connector
  embedded.addConnector(connector);
   }
   private Context createContext(final String path, final String docBase) {
  // Create a Context
  Context context = embedded.createContext(path, docBase);
  context.setParentClassLoader(this.getClass().getClassLoader());
    // Create a default servlet
  Wrapper servlet = context.createWrapper();
  servlet.setName("default");
  servlet.setServletClass("org.apache.catalina.servlets.DefaultServlet");
  servlet.setLoadOnStartup(1);
  servlet.addInitParameter("debug", "0");
  servlet.addInitParameter("listings", "false");
  context.addChild(servlet);
  context.addServletMapping("/", "default");
    // Create a handler for jsps
  Wrapper jspServlet = context.createWrapper();
  jspServlet.setName("jsp");
  jspServlet.setServletClass("org.apache.jasper.servlet.JspServlet");
  jspServlet.addInitParameter("fork", "false");
  jspServlet.addInitParameter("xpoweredBy", "false");
  jspServlet.setLoadOnStartup(2);
  context.addChild(jspServlet);
  context.addServletMapping("*.jsp", "jsp");
  context.addServletMapping("*.jspx", "jsp");
    // Set seme default welcome files
  context.addWelcomeFile("index.html");
  context.addWelcomeFile("index.htm");
  context.addWelcomeFile("index.jsp");
  context.setSessionTimeout(30);
    // Add some mime mappings
  context.addMimeMapping("html", "text/html");
  context.addMimeMapping("htm", "text/html");
  context.addMimeMapping("gif", "image/gif");
  context.addMimeMapping("jpg", "image/jpeg");
  context.addMimeMapping("png", "image/png");
  context.addMimeMapping("js", "text/javascript");
  context.addMimeMapping("css", "text/css");
  context.addMimeMapping("pdf", "application/pdf");
    return context;
 }
   public void startServer() throws Exception {
  init();
  embedded.start();
 }
   public void stopServer() {
  if (embedded != null) {
   try {
    System.out.println("Shutting down MyServer...");
    embedded.stop();
    System.out.println("MyServer shutdown.");
   } catch (Exception e) {
    // No need to do anything
   }
  }
 }
   public static void main(final String args[]) throws Exception {
    ServerLauncher server = new ServerLauncher();
  server.startServer();
    // This code is just to prevent the sample
  // application from terminating
  synchronized (server) {
   server.wait();
  }
 }
}

I have all required libraries in WEB-INF/lib folder and in build.gradle as dependency. Below is my build.gradle file :

apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'maven'
  group ='biz.neustar'
sourceCompatibility = 1.6
targetCompatibility = 1.6
archivesBaseName ='gdigui'
defaultTasks "clean","assemble","createDist"
sourceSets.main.java.srcDirs = ["JavaSource"]
ext.deployHome = relativePath("$buildDir/deploy")
repositories {
maven { url "http://stencdvvap8:8080/nexus/content/groups/public/" } }
dependencies {
 compile fileTree(dir: 'WebContent/WEB-INF/lib', includes: ['*.jar'])
 providedCompile "org.apache.tomcat:coyote:6.0.36",'org.apache.tomcat:jasper:6.0.36'
}
  war {
 from 'WebContent' // adds a file-set to the root of the archive
 destinationDir = file("$deployHome/build")
 doLast{
  copy{
   println "Copying property files."
   from('conf'){ include "**/*.properties" }
   into "$deployHome/config"
  }
 }
}
  compileJava{
 doFirst{
  copy{
   println "Copying property files."
   from(sourceSets.main.java.srcDirs){ include "**/*.properties" }
   into sourceSets.main.output.classesDir
  }
 }
}
  configurations.all {
 resolutionStrategy {
  cacheDynamicVersionsFor 0, 'seconds'
  cacheChangingModulesFor 0, 'seconds'
 }
}
  task createDist(type : Tar){
 from "$deployHome"
 archiveName = "gdi_gui_1.6.2-snapshot.tar.gz"
 compression = Compression.GZIP
}

When I try to run this ServerLauncher class from eclipse , I am getting below exception :

INFO: Starting tomcat server Apr 12, 2013 5:43:35 PM org.apache.catalina.core.StandardEngine start INFO: Starting Servlet Engine: Apache Tomcat/6.0.36 Apr 12, 2013 5:43:35 PM org.apache.catalina.loader.WebappClassLoader validateJarFile INFO: validateJarFile(/home/amit/java_projects/SampleApp/WebContent/./WEB-INF/lib/servlet-api.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class Apr 12, 2013 5:43:35 PM org.apache.catalina.startup.ContextConfig defaultWebConfig INFO: No default web.xml Exception in thread “main” java.lang.NoSuchMethodError: javax.servlet.ServletContext.getContextPath()Ljava/lang/String;

at org.apache.catalina.core.StandardHost$MemoryLeakTrackingListener.lifecycleEvent(StandardHost.java:616)

at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:142)

at org.apache.catalina.core.StandardContext.start(StandardContext.java:4700)

at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)

at org.apache.catalina.core.StandardHost.start(StandardHost.java:840)

at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)

at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)

at org.apache.catalina.startup.Embedded.start(Embedded.java:825)

at com.app.sample.ServerLauncher.startServer(ServerLauncher.java:113)

at com.app.sample.ServerLauncher.main(ServerLauncher.java:131)

I also tried to debug the code and the code is completing inti() method call but it is failing in embedded.start() call. It seems that gradle is excluding servlet-api.jar from eclipse runtime classpath.

I don’t want use gradle application plugin as I need to debug the code a lot and eclipse debug is preferred.

Thanks in advance.

I’m running into this same problem. Did you ever find the solution?