使用 Maven 构建 Web 项目

使用 Maven 构建 Web 项目

创建一个Web项目

  1. cd 工作目录执行mvn archetype:generate

    E:\workspace_maven>mvn archetype:generate 
    
  2. 输入web过滤

    Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 703: web
    66: remote -> org.codehaus.mojo.archetypes:webapp-javaee7 (Archetype for a web application using Java EE 7.)
    
  3. 选着的66个

    Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): : 66 
    
  4. 选着版本,输入2

    Choose org.codehaus.mojo.archetypes:webapp-javaee7 version:
    1: 1.0
    2: 1.1
    Choose a number: 2: 2
    Define value for property 'groupId': : com.andean
    Define value for property 'artifactId': : helloweb
    Define value for property 'version':  1.0-SNAPSHOT: :
    Define value for property 'package':  com.andean: :
    Confirm properties configuration:
    groupId: com.andean
    artifactId: helloweb
    version: 1.0-SNAPSHOT
    package: com.andean
    Y: : Y
    
  5. 生成helloweb项目(E:\workspace_maven\helloweb)
  6. 打包E:\workspace_maven\helloweb>mvn package
  7. 生成target目录 (E:\workspace_maven\helloweb\target)
  8. 把E:\workspace_maven\helloweb\target\helloweb-1.0-SNAPSHOT.war 拷贝到 F:\apache-tomcat-8.0.22\webapps 目录里
  9. 运行Tomcat(F:\apache-tomcat-8.0.22\bin\startup.bat)
  10. 在浏览器打开项目(http://localhost:8080/helloweb-1.0-SNAPSHOT/)

使用 Tomcat 插件运行 Web 项目

  1. 编辑E:\workspace_maven\helloweb\pom.xml (http://tomcat.apache.org/maven-plugin.html)

     <plugin>
      <groupId>org.apache.tomcat.mavengroupId>
      <artifactId>tomcat8-maven-pluginartifactId>
      <version>2.2version>
     plugin>
    
  2. 运行mvn tomcat:run

    E:\workspace_maven\helloweb>mvn tomcat:run
    
  3. 打开浏览器输入http://localhost:8080/helloweb/

使用 Jetty 插件运行 Web 项目

  1. 编辑E:\workspace_maven\helloweb\pom.xml (http://wiki.eclipse.org/Jetty/Feature/Jetty_Maven_Plugin)

    <plugin>
      <groupId>org.mortbay.jettygroupId>
      <artifactId>jetty-maven-pluginartifactId>
    plugin>
    
  2. 运行mvn jetty:run

    E:\workspace_maven\helloweb>mvn jetty:run
    
  3. 打开浏览器输入http://localhost:8080/

添加 J2EE 依赖

  1. 编辑E:\workspace_maven\helloweb\pom.xml

    <dependencies>
      <dependency>
        <groupId>javaxgroupId>
        <artifactId>javaee-web-apiartifactId>
        <version>7.0version>
        <scope>providedscope>
      dependency>
      <dependency>
        <groupId>javax.servletgroupId>
        <artifactId>javax.servlet-apiartifactId>
        <version>3.0.1version>
        
      dependency>
      <dependency>
        <groupId>javax.servlet.jspgroupId>
        <artifactId>jsp-apiartifactId>
        <version>2.1version>
        
      dependency>
      <dependency>
        <groupId>jstlgroupId>
        <artifactId>jstlartifactId>
        <version>1.2version>
        
      dependency>
    dependencies>
  2. 运行 ‘mvn clean package’

    E:\workspace_maven\helloweb>mvn clean package

    E:\workspace_maven\helloweb\target\helloweb-1.0-SNAPSHOT\WEB-INF\lib 目录里会有:javax.servlet-api-3.0.1.jar,jsp-api-2.1.jar,jstl-1.2.jar

创建 JSP 和 Servlet

  1. 在E:\workspace_maven\helloweb\src\main\webapp 目录里创建index.jsp

    E:\workspace_maven\helloweb\src\main\webapp>touch index.jsp
    
  2. 编辑index.jsp
  3. 在E:\workspace_maven\helloweb\src\main\java\com\andean目录里创建HelloServlet.java

    E:\workspace_maven\helloweb\src\main\java\com\andean>touch HelloServlet.java
    
  4. 在 E:\workspace_maven\helloweb\src\main\webapp 里创建WEB-INF 文件夹
  5. 在 E:\workspace_maven\helloweb\src\main\webapp\WEB-INF目录里创建web.xml

    E:\workspace_maven\helloweb\src\main\webapp\WEB-INF>touch web.xml
    
  6. 编辑web.xml

    
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_1.xsd"
    version="3.1" >
    <servlet>
       <servlet-name>HelloServletservlet-name>
       <servlet-class>com.andean.HelloServletservlet-class>
    servlet>
    <servlet-mapping>
       <servlet-name>HelloServletservlet-name>
       <url-pattern>/helloServleturl-pattern>
    servlet-mapping>
    
    web-app>
  7. 运行 ‘mvn clean package’

    E:\workspace_maven\helloweb>mvn clean package
    
  8. 运行mvn jetty:run

    E:\workspace_maven\helloweb>mvn jetty:run
    
  9. 打开浏览器输入http://localhost:8080/helloServlet
  10. 源码

在 Eclipse 中使用 Maven 构建 Web 项目

使用 Maven 构建 Web 项目_第1张图片

使用 Maven 构建 Web 项目_第2张图片

使用 Maven 构建 Web 项目_第3张图片

使用 Maven 构建 Web 项目_第4张图片

使用 Maven 构建 Web 项目_第5张图片

使用 Maven 构建 Web 项目_第6张图片

使用 Maven 构建 Web 项目_第7张图片

使用 Maven 构建 Web 项目_第8张图片

步骤9

你可能感兴趣的:(Java)