Question

How can I serve static content with maven jetty plugin (7.x)

Thanks

Était-ce utile?

La solution 2

I have such a configuration at my jetty.xml. I have just wanted to updated my question.

 <Set name="handler">
     <New class="org.eclipse.jetty.server.handler.HandlerList">
        <Set name="handlers">
           <Array type="org.eclipse.jetty.server.Handler">
              <Item>
                 <New class="org.eclipse.jetty.servlet.ServletContextHandler">
                    <Set name="contextPath">/static</Set>
                    <Set name="resourceBase">${static-resources-path}</Set>
                    <Call name="addServlet">
                       <Arg>org.eclipse.jetty.servlet.DefaultServlet</Arg>
                       <Arg>/</Arg>
                    </Call>
                 </New>
              </Item>
           </Array>
        </Set>
     </New>
  </Set>

Autres conseils

put your static contents under any folder below /yourStaticApp/src/main/webapp -- say under /yourStaticApp/src/main/webapp/static. When you will run Jetty these will be available as http://host:port/contextRoot/static/fileName.ext


Hmmm, unsure, if that's possible. Eclipse Jetty Maven plugin documents a way to configure static source location, which boils down to the alternate location of webapps mentioned above.

 ...
 <plugin>
    ...
    <configuration>
      <webAppSourceDirectory>${basedir}/src/staticfiles</webAppSourceDirectory>
      ...
    </configuration>
    ...
  </plugin>
  ...

As the doc points out:

<webAppSourceDirectory>–By default, this is set to ${basedir}/src/main/webapp. If your static sources are in a different location, set this parameter accordingly.

refer: http://wiki.eclipse.org/Jetty/Feature/Jetty_Maven_Plugin


Update: On some more reseach I found out that you can actually point the location of webdefault.xml from in Jetty-maven plugin; and in webdefault.xml you can configure the static content location.

In your Jetty Maven configuration, point the location of wendefault.xml

  <plugin>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>maven-jetty-plugin</artifactId>
    <configuration>
     ...
      <defaultsDescriptor>/my/path/to/webdefault.xml</defaultsDescriptor>
     ...
    </configuration>
  </plugin>

Now, with webdefault.xml in your hand you can put the configuration mentioned here: http://docs.codehaus.org/display/JETTY/Static+Content -- except the package Names has been changed from org.mortbay.jetty... to org.eclipse.jetty... see below:

<Configure class="org.eclipse.jetty.servlet.Context">
  <Set name="contextPath">/javadoc</Set>
  <Set name="resourceBase"><SystemProperty name="jetty.home" default="."/>/javadoc/</Set>
  <Call name="addServlet">
    <Arg>org.eclipse.jetty.servlet.DefaultServlet</Arg>
    <Arg>/</Arg>
  </Call>
</Configure>

refer: http://wiki.eclipse.org/Jetty/Reference/webdefault.xml

I haven't tested/used the above. But let me know, if you get this working. Or if anything else needed to get this done.

This is a config which works for me, using the resourceBase and contextPath values on the JettyWebAppContext

<plugin>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <version>9.4.7.v20170914</version>
    <configuration>
        <scanIntervalSeconds>60</scanIntervalSeconds>
        <webApp>
            <contextPath>/app</contextPath>
        </webApp>
        <contextHandlers>
            <contextHandler implementation="org.eclipse.jetty.maven.plugin.JettyWebAppContext">
                <contextPath>/images</contextPath>
                <resourceBase>./../../env/localhost/config/images</resourceBase>
            </contextHandler>
        </contextHandlers>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top