bkielczewski / example-spring-mvc-jetty

79 stars 49 forks source link

Embedded Jetty and Gradle #1

Open JazzyJes opened 10 years ago

JazzyJes commented 10 years ago

Hello, First off, great example.

I am trying the fine approach into my preexisting project. I start the main() and encounter an issue when attempting to set the classpath resource to "webapp" (uncommented in code snippet below) which clearly is caused due to the webapp not existing inside the executing directory of that jetty launcher...

The difference with my project setup is that I use Gradle instead of Maven and that I therefore don't use maven-shade-plugin, but rather simply include the whole resources folder to my resulting .jar file.

When I start the EmbeddedJetty, then the new folder /bin folder is created under: com/acme/myproject and it has no trace of the webapp folder.

ServletContextHandler contextHandler = new ServletContextHandler();
    contextHandler.setErrorHandler(null);
    contextHandler.setContextPath(CONTEXT_PATH);
    contextHandler.addServlet(new ServletHolder(new DispatcherServlet(context)), MAPPING_URL);
    contextHandler.addEventListener(new ContextLoaderListener(context));
    // PROBLEM: file:/home/jes/Development/git/foobar/bin/ is the working dir while i need to have it to ./../src/main/resource. How to?
    // FIX contextHandler.setResourceBase(new
    // ClassPathResource("webapp").getURI().toString());
    return contextHandler;

I can't seem to understand something crucial about my setup attempt :( Is there a way to tweak the working directory of the EmbdeddeJetty main() method so that it isn't set to /bin? or is there a way join the whole thing?!

bkielczewski commented 10 years ago

Hello, thank you. Honestly I have no hands on experience in doing it the way you do, without seeing this Gradle script you're using I'm not even sure I get what you're doing. I don't even remember having seen /bin created, are you sure it's from Jetty?

The way it works with Maven is that everything gets packaged into a JAR, src/main/webapp included, so it's available on the classpath on /webapp path once the JAR is run. The shade plugin just adds the other deps into the JAR but has nothing to do with /webapp itself.

So, based on intuition only, I'd first examine the created JAR to see /webapp is there. You can even package the example project from Maven and compare.