How to convert a MAVEN project to a Java Web project

Source: Internet
Author: User
Tags java web maven central

In the previous article, how to use idea to create a MAVEN project: http://www.cnblogs.com/lay2017/p/8468510.html, this article will use the previous build of the MAVEN project

Let's take a look at how to convert a MAVEN project to a Java Web project

First we'll adjust the MAVEN project to the directory structure of the Web project

Follow the three steps:

    1. In the main directory, add the WebApp directory
    2. Under the WebApp directory, add the Web-inf directory
    3. Add the Web. xml file under the Web-inf directory

At this point the idea gives a hint

Idea has identified that in addition to the current use of the web framework (that is, the servlet framework), some configuration is required to be used. Click the "Configure" button to see a confirmation box and click "OK" to turn the current project into a Web project

Add code to Web. xml

<?XML version= "1.0" encoding= "Utf-8"?><!--The Servlet3.0 framework is used here, so adding the Web-app code to Web. XML is as follows -<Web-appxmlns= "Http://java.sun.com/xml/ns/javaee"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"version= "3.0">    <!--in fact, using the Servlet3.0 framework, you can omit the Web. xml file because the servlet does not need to be configured in Web. XML, but only by Java annotations. -</Web-app>

Add a maven dependency to the Java Web in the Pom.xml file, giving the full code here

<?XML version= "1.0" encoding= "UTF-8"?><Projectxmlns= "http://maven.apache.org/POM/4.0.0"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">    <modelversion>4.0.0</modelversion>    <groupId>Cn.lay</groupId>    <Artifactid>Chapter1</Artifactid>    <version>1.0-snapshot</version>    <!--The Web project needs to be packaged into a war package, so it needs to be configured as a jar package by default -    <Packaging>War</Packaging>    <Properties>        <!--Unified Source code, or use MAVEN to compile the source code when the opportunity to have a relevant warning. Are generally encoded using the UTF-8 -        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>    </Properties>    <!--Next, add the required dependencies for the Java Web: Servlets, JSPs, JSTL, etc. -    <Dependencies>        <!--Servlet -        <Dependency>            <groupId>Javax.servlet</groupId>            <Artifactid>Javax.servlet-api</Artifactid>            <version>3.1.0</version>            <Scope>Provided</Scope>        </Dependency>        <!--JSP -        <Dependency>            <groupId>javax.servlet.jsp</groupId>            <Artifactid>Jsp-api</Artifactid>            <version>2.2</version>            <Scope>Provided</Scope>        </Dependency>        <!--JSTL -        <Dependency>            <groupId>Javax.servlet</groupId>            <Artifactid>Jstl</Artifactid>            <version>1.2</version>            <Scope>Runtime</Scope>        </Dependency>    </Dependencies>    <!--JDK version of unified source and compiled output -    <Build>        <Plugins>            <!--all plugin need to be added to the Build/plugins tab, and plugins and dependencies come from the MAVEN central repository: http://search.maven.org/ -            <plugin>                <groupId>Org.apache.maven.plugins</groupId>                <Artifactid>Maven-compiler-plugin</Artifactid>                <version>3.3</version>                <Configuration>                    <Source>1.8</Source>                    <Target>1.8</Target>                </Configuration>            </plugin>            <!--Optional plugin to skip unit tests when MAVEN is packaged -            <plugin>                <groupId>Org.apache.maven.plugins</groupId>                <Artifactid>Maven-surefire-plugin</Artifactid>                <version>2.18.1</version>                <Configuration>                    <skiptests>True</skiptests>                </Configuration>            </plugin>        </Plugins>    </Build></Project>

It should be stated that:

    1. The "three coordinates" (groupid\artifactid\version) that Maven relies on must provide
    2. If some dependencies only need to be involved in compiling, without packaging (for example, Tomcat comes with a jar package for the servlet and JSP), it can set its scope to provided.
    3. If some dependencies are only required by the runtime, but do not need to participate in compilation (for example, Jstl jar packages), you can set their scope to runtime.

Above, a MAVEN project has been converted to a MAVEN-based Java Web project.

Of course, idea can also create a MAVEN-based Java Web project directly from the prototype, without the hassle. But creating a MAVEN project and converting it to a Java Web project might give you a little more idea of how the project will be built.

How to convert a MAVEN project to a Java Web project

Related Article

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.