how to run a web service maven project on jetty 8

2019-07-23 16:23发布

问题:

I have a REST web service which I created previously and deployed in Tomcat7. I wanted to deploy it on Jetty, as advised in a previous question, I made a Maven project and copied my files there and configured the dependencies and I can run Maven install from eclipse successfully.

This is my build part in POM.xml:

 <build>
    <plugins>
        <plugin>
             <groupId>org.mortbay.jetty</groupId>
             <artifactId>maven-jetty-plugin</artifactId>
        </plugin>

        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <dependentWarExcludes></dependentWarExcludes>
                <webappDirectory>
                    WebContent
                </webappDirectory>

            </configuration>
        </plugin>
    </plugins>
</build> 

I followed instructions here to use maven jetty plug-in (I am not sure if this is required). The question here is how can I deploy my maven project in jetty from eclipse so that i can go to http:localhost:8080/myproject for example and see my project working?

EDIT:

The way I used to run the service on Tomcat was by right click on the project and click on Run As -> Run on server (which is configured in eclipse servers)

I am using eclipse Indigo, Maven 3 and jetty 8. Also I used jersey for the web service.

回答1:

Here, you are mixing two different things:

  1. Run your Web application using a Eclipse server plugin as Tomcat plugin for Eclipse.
  2. Run your Web application using a Maven plugin as Jetty Maven plugin. There are also Maven plugins for Tomcat.

Having said that, take a look to this answer where it is described how to configure Eclipse to run Maven plugins (it could change depending on Eclipse version but idea would be similar)

Edited:

If you just want to run jetty from Maven, just use the following command line:

mvn jetty:run

But, in any case, I recommend that you indicate the Maven jetty plugin version in the pom file (Maven will warn you if you don't).

Edited 2

First, update your jetty maven plugin version:

<plugin>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <version>8.0.0.M2</version>
</plugin>

Second. You are working with two plugins, one for building the war file (maven-war-plugin) and another one for running your application on jetty (jetty-maven-plugin). Take in mind that jetty thinks your project has a standard maven project structure, it means, it will look for your web app content in /src/main/webapp but it looks like is not here. In your war plugin configuration, you specify that your web content is in WebContent, so tell jetty plugin that directory is there, as well as you are telling it to war plugin:

<plugin>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <version>8.0.0.M2</version>
    <configuration>
        <webAppSourceDirectory>/WebContent</webAppSourceDirectory>             
        <webXml>/over/here/web.xml</webXml>
        <jettyEnvXml>/src/over/here/jetty-env.xml</jettyEnvXml>
        <classesDirectory>/somewhere/else</classesDirectory>
    <configuration>
</plugin>

See documentation. Of course, it is better to work with a standard maven project structure so you do not need to tell jetty where things are.