Maven basics 4

Source: Internet
Author: User

We will create a web project:

1. Create a project MVN archetype: Create-dgroupid = org. sonatype. mavenbook. ch05-dartifactid = simple-webapp-dpackagename = org. sonatype. mavenbook-darchetypeartifactid = Maven-Archetype-Web

The red part indicates that the project is a web project. The result is: <packaging> war </packaging> In the Pom. xml file.

2. You can use the maven jetty plug-in to run your web application in Maven. Therefore, you need to configure the maven jetty plug-in the Pom. xml file of the project.

<Build> <br/> <finalname> simple-webapp </finalname> <br/> <plugins> <br/> <plugin> <br/> <groupid> Org. mortbay. jetty </groupid> <br/> <artifactid> Maven-jetty-plugin </artifactid> <br/> </plugin> <br/> </plugins> <br/> </build>

Use MVN jetty: run the project started jetty server (run MVN compile to compile your project before starting the jetty plug-in)

Http: // localhost: 8080/simple-webapp/view the default index. jsp page. Hello world. The index. jsp is located under webapp.

Add the package and servlet. However, compiling with MVN compile will fail, because the project does not have a J2EE library currently!

To compile a servlet, we need to add the servlet API as the project dependency. Servlet specification description is a jar file that can be downloaded from the site of Sun Microsystems to http://java.sun.com/products/servlet/download.html. After downloading the JAR file, you need to install it in ~ /. M2/Repository Maven local repository.

But now we don't have to worry about it: we can use Apache Geronimo. The configuration is as follows:

<Dependency> <br/> <groupid> Org. apache. geronimo. specs </groupid> <br/> <artifactid> geronimo-servlet_2.4_spec </artifactid> <br/> <version> 1.1.1 </version> <br/> <scope> provided </Scope> <br/> </dependency>

It is also worth noting that our dependency uses the provided range. This range indicates that the mavenjar file has been "supplied" by the container and therefore does not need to be included in the war.

<Dependency> <br/> <groupid> Org. apache. geronimo. specs </groupid> <br/> <artifactid> geronimo-jsp_2.0_spec </artifactid> <br/> <version> 1.1 </version> <br/> <scope> provided </Scope> <br/> </dependency>

After adding the servlet specification dependency, run MVN clean install and then run MVN jetty: Run.

Run servlet...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.