Question

I have a Google App Engine (1.8.5) project. I would like to serve up static Javascript & CSS via Webjars. However, I keep getting HTTP 404 errors. How do I make my Webjars files accessible?

My src/main/webapp/WEB-INF/appengine-web.xml per Google's documentation:

<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
    ...
    <static-files>
        <include path="/resources/**" />
        <include path="/webjars/**" />
    </static-files>
</appengine-web-app> 

My src/main/webapp/index.html, referencing the Webjars-provided Bootstrap CSS file:

<html>
<head>
    <link rel="stylesheet" href="webjars/bootstrap/3.0.0/css/bootstrap.min.css">
</head>
...
</html>

Snippets of pom.xml:

    <dependency>
        <groupId>com.google.appengine</groupId>
        <artifactId>(lots)</artifactId>
        <version>1.8.5</version>
    </dependency>
    <dependency>
        <groupId>com.google.inject</groupId>
        <artifactId>guice</artifactId>
        <version>4.0-beta</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <scope>provided</scope>
        <version>3.1.0</version>
    </dependency>
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>bootstrap</artifactId>
        <version>3.0.0</version>
    </dependency>

This is my first GAE posted question, so I'm not sure what additional info to provide--be gentle, SO.

Was it helpful?

Solution

Unfortunately, Webjars requires a Servlet 3.x container, but the Google App Engine is currently still only on version 2.5.

You might want to star this issue in the GAE issues list.

OTHER TIPS

I finally got a workaround to actually work! I did it with RESTlet. I think you could do it in a non-restlet app and just use restlet for this bit.

If you do use restlet, simply stick this into your router:

final Component component = new Component();
component.getClients().add(Protocol.CLAP);
final Directory dir = new Directory(component.getContext(), "clap://class/META-INF/resources/webjars");
dir.setDeeplyAccessible(true);
this.attach("/webjars", dir);
//access via http://example.com/webjars/jquery/1.9.0/jquery.min.js

If you don't use restlet, you'll need to add a restlet servlet adapter to your web.xml, create a restlet application and router with the above in. And it'd only be a few extra lines of code!

more info on my blog: http://demeranville.com/using-webjars-without-servlet-3-on-google-app-engine-gae/

You can do that easily. Also on Servlet 2.x container.

Register our webjars-servlet-2.x Maven dependency in your web application:

<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>webjars-servlet-2.x</artifactId>
    <version>1.1</version>
</dependency>

Register WebjarsServlet in your web.xml:

<!--Webjars Servlet-->
<servlet>
    <servlet-name>WebjarsServlet</servlet-name>
    <servlet-class>org.webjars.servlet.WebjarsServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>WebjarsServlet</servlet-name>
    <url-pattern>/webjars/*</url-pattern>
</servlet-mapping>

By default the Webjars resources will be cached by your browser. If for whatever reason you need to disable the cache, you can do so by using the disableCache configuration property like this:

<!--Webjars Servlet-->
<servlet>
    <servlet-name>WebjarsServlet</servlet-name>
    <servlet-class>org.webjars.servlet.WebjarsServlet</servlet-class>
    <init-param>
        <param-name>disableCache</param-name>
        <param-value>true</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>WebjarsServlet</servlet-name>
    <url-pattern>/webjars/*</url-pattern>
</servlet-mapping>

More info look at http://www.webjars.org/documentation#servlet2

Looks like webjars now provides legacy support for servlet 2 http://www.webjars.org/documentation#servlet2

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top