Maven_3_webAPP

Posted charles999

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Maven_3_webAPP相关的知识,希望对你有一定的参考价值。

maven中央仓库地址:http://mvnrepository.com 
搜索servlet,点击Java Servlet API
         Jetty :: Jetty Maven Plugin
Servlet: http://mvnrepository.com/artifact/javax.servlet/javax.servlet-api/3.1.0
*******************************************
创建web项目的过程: 1、使用maven创建一个web类型的maven项目。

选择maven-archetype-webapp
2、添加servlet插件的依赖pom.xml。
创建好的web项目目录,其中的jsp报错,是因为没有添加servlet API的原因,需要在pom中添加相关依赖。
maven中央仓库地址:http://mvnrepository.com 搜索servlet,点击Java Servlet API
Servlet: http://mvnrepository.com/artifact/javax.servlet/javax.servlet-api/3.1.0
servlet依赖如下:添加到pom.xml scope依赖范围为provided,只在编译时和测试时运行。
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
    <scope>provided</scope>
</dependency>
3、添加src源文件夹。maven规范必须的src/main/java 和 src/test/java
Java Resources-new-Source Folder:project+folder(src/main/java 和 src/test/java)
  无法添加folder的解决办法:
  3.1.删除Java build path 中存在的目录(实际不存在)
    使用maven新建类目录是,报错The folder is already a source folder.的解决办法
  
  3.2.使用navigator视图做成
    打开navigator视图方法:windows-Show View - Navigator
  eclipse创建maevn web项目,在选择maven_archetype_web原型后,默认只有src/main/resources这个Source Floder。
    按照maven目录结构,添加src/main/java、src/test/java等Source Floder时,会报The folder is already a source folder的错误。
    解决办法:用Navigator视图,直接在src/main目录下建立java目录。
    分析原因: 项目属性->Java Build Path->Source,会看到src/main/java, src/test/java已存在,但是Missing。
所以只需要创建目录,Source Floder就出现了。

 

 检查classes文件输出路径 :Build Path-Configure Build Path-Source

  Output folder webappDemo target/classes

 

 把项目转化为web项目:

  项目-right click-properties-Project Facets-勾选Dynamic Web Module-OK

 

 修改部署时的默认配置:

  项目-right click-properties-Deployment Assembly 把test的相关Source都remove掉

 

 使用package打包 ,拷贝到web容器就可以访问了

此处使用jetty作为web容器,添加jetty插件到pom.xml(插件在中央仓库中搜索 Jetty :: Jetty Maven Plugin

http://mvnrepository.com/artifact/org.eclipse.jetty/jetty-maven-plugin/9.4.6.v20170531 使用最新版本

<!-- https://mvnrepository.com/artifact/org.eclipse.jetty/jetty-maven-plugin -->
<dependency>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <version>9.4.6.v20170531</version>
</dependency>

)

pom.xml

<project xmlns="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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.imooc.webappDemo</groupId>
  <artifactId>webappDemo</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>webappDemo Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.10</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>webappDemo</finalName>
    <plugins>
      <plugin>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <version>9.4.6.v20170531</version>
      </plugin>
    </plugins>
  </build>
</project>

想在打包时运行可改为:

      <plugin>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <version>9.4.6.v20170531</version>
        <executions>
          <execution>
              <phase>package</phase>
          </execution>
        </executions>
      </plugin>

 启动:run as -Maven build-Goals输入jetty:run

控制台输出(去掉了downloading的info):[INFO] Started Jetty Server

 

[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] Building webappDemo Maven Webapp 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] >>> jetty-maven-plugin:9.4.6.v20170531:run (default-cli) > test-compile @ webappDemo >>>
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ webappDemo ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ webappDemo ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ webappDemo ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ webappDemo ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] <<< jetty-maven-plugin:9.4.6.v20170531:run (default-cli) < test-compile @ webappDemo <<<
[INFO] 
[INFO] 
[INFO] --- jetty-maven-plugin:9.4.6.v20170531:run (default-cli) @ webappDemo ---
[INFO] Configuring Jetty for project: webappDemo Maven Webapp
[INFO] webAppSourceDirectory not set. Trying src\\main\\webapp
[INFO] Reload Mechanic: automatic
[INFO] Classes = D:\\java\\workspace\\webappDemo\\target\\classes
[INFO] Logging initialized @58387ms to org.eclipse.jetty.util.log.Slf4jLog
[INFO] Context path = /
[INFO] Tmp directory = D:\\java\\workspace\\webappDemo\\target\\tmp
[INFO] Web defaults = org/eclipse/jetty/webapp/webdefault.xml
[INFO] Web overrides =  none
[INFO] web.xml file = file:///D:/java/workspace/webappDemo/src/main/webapp/WEB-INF/web.xml
[INFO] Webapp directory = D:\\java\\workspace\\webappDemo\\src\\main\\webapp
[INFO] jetty-9.4.6.v20170531
[INFO] Scanning elapsed time=169ms
[INFO] DefaultSessionIdManager workerName=node0
[INFO] No SessionScavenger set, using defaults
[INFO] Scavenging every 660000ms
[INFO] Started o.e.j.m.p.JettyWebAppContext@3cf7298d{/,file:///D:/java/workspace/webappDemo/src/main/webapp/,AVAILABLE}{file:///D:/java/workspace/webappDemo/src/main/webapp/}
[INFO] Started ServerConnector@a098d76{HTTP/1.1,[http/1.1]}{0.0.0.0:8080}
[INFO] Started @59879ms
[INFO] Started Jetty Server

访问http://localhost:8080/成功

 <plugin>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <version>9.4.6.v20170531</version>
        <executions>
          <execution>
              <phase>package</phase>
              <!-- 打包成功后,使用jetty:run来运行jetty服务 -->
              <goals>
                  <goal>run</goal>
              </goals>
          </execution>
        </executions>
      </plugin>

验证Goals:clean package

(先停止服务,因为端口被占用

[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] Building webappDemo Maven Webapp 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] >>> jetty-maven-plugin:9.4.6.v20170531:run (default-cli) > test-compile @ webappDemo >>>
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ webappDemo ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ webappDemo ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ webappDemo ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ webappDemo ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] <<< jetty-maven-plugin:9.4.6.v20170531:run (default-cli) < test-compile @ webappDemo <<<
[INFO] 
[INFO] 
[INFO] --- jetty-maven-plugin:9.4.6.v20170531:run (default-cli) @ webappDemo ---
[INFO] Configuring Jetty for project: webappDemo Maven Webapp
[INFO] webAppSourceDirectory not set. Trying src\\main\\webapp
[INFO] Reload Mechanic: automatic
[INFO] Classes = D:\\java\\workspace\\webappDemo\\target\\classes
[INFO] Logging initialized @3490ms to org.eclipse.jetty.util.log.Slf4jLog
[INFO] Context path = /
[INFO] Tmp directory = D:\\java\\workspace\\webappDemo\\target\\tmp
[INFO] Web defaults = org/eclipse/jetty/webapp/webdefault.xml
[INFO] Web overrides =  none
[INFO] web.xml file = file:///D:/java/workspace/webappDemo/src/main/webapp/WEB-INF/web.xml
[INFO] Webapp directory = D:\\java\\workspace\\webappDemo\\src\\main\\webapp
[INFO] jetty-9.4.6.v20170531
[INFO] Scanning elapsed time=112ms
[INFO] DefaultSessionIdManager workerName=node0
[INFO] No SessionScavenger set, using defaults
[INFO] Scavenging every 600000ms
[INFO] Started o.e.j.m.p.JettyWebAppContext@52fc5eb1{/,file:///D:/java/workspace/webappDemo/src/main/webapp/,AVAILABLE}{file:///D:/java/workspace/webappDemo/src/main/webapp/}
[INFO] Jetty server exiting.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.840 s
[INFO] Finished at: 2017-06-30T11:15:11+08:00
[INFO] Final Memory: 16M/169M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.eclipse.jetty:jetty-maven-plugin:9.4.6.v20170531:run (default-cli) on project webappDemo: Failure: Address already in use: bind -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

 

 

使用Tomcat座web容器方法:需要一个maven 插件

  进入tomcat 官网http://tomcat.apache.org/maven-plugin-2.2/

 参照例子:

  <groupId>org.apache.tomcat.maven</groupId>

  <artifactId>tomcat7-maven-plugin</artifactId>

  <version>2.2</version>

<plugin>
        <!--  <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <version>9.4.6.v20170531</version>-->
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>
        <executions>
          <execution>
              <phase>package</phase>
              <!-- 打包成功后,使用jetty:run来运行jetty服务 -->
              <goals>
                  <goal>run</goal>
              </goals>
          </execution>
        </executions>
      </plugin>

启动Goals:clean package

信息: Starting Servlet Engine: Apache Tomcat/7.0.47
六月 30, 2017 11:30:42 上午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["http-bio-8080"]

 

 

查看http://localhost:8080/webappDemo/成功

4、修改jer的版本。

以上是关于Maven_3_webAPP的主要内容,如果未能解决你的问题,请参考以下文章

maven-webapp项目pom.xml配置

阶段2 JavaWeb+黑马旅游网_15-Maven基础_第3节 maven标准目录结构和常用命令_07maven常用命令

MyBatis学习总结_08_Mybatis3.x与Spring4.x整合

阶段3 3.SpringMVC·_04.SpringMVC返回值类型及响应数据类型_1 搭建环境

[AndroidStudio]_[初级]_[配置自动完成的代码片段]

VSCode 配置 用户自定义代码片段 自定义自动代码补充