新建maven项目
archetypes选择webapp
刚建好项目时jsp文件报错,因为没有相应API支持,只要引入相应依赖即可
创建完整目录结构如下(保证main和test目录下都有各自的java与resources目录)
确保项目输出文件路径正确: Build Path --> Configure Build Path(查看四个Output folder是否都是 项目/target/classes)
右击项目 Properties --> Project Facets 选中Dynamic Web Module, 将项目转换成web项目
更改部署配置 Properties -->Deployment Assembly ,将test部分目录移除
使用jetty容器插件装载web,首先对pom.xml进行插件添加
通过jetty:run指令进行Maven Build时报错
[ERROR] Failed to execute goal org.eclipse.jetty:jetty-maven-plugin:9.4.8.v20171121:run (default-cli)
on project mavendemo: 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
机智的我重启了下eclipse就OK了[INFO] Scavenging every 660000ms
[INFO] Started o.e.j.m.p.JettyWebAppContext@7cf162bc{/,file:///D:/eclipse-workspace/mavendemo/mavendemo/src/main/webapp/,AVAILABLE}{file:///D:/eclipse-workspace/mavendemo/mavendemo/src/main/webapp/}
[INFO] Started ServerConnector@249e0271{HTTP/1.1,[http/1.1]}{0.0.0.0:8080}
[INFO] Started @4952ms
[INFO] Started Jetty Server
接下来配置自动启动服务功能,在打包项目时,自动调用run启动jetty插件
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.4.8.v20171121</version>
<executions>
<execution>
<!-- 在打包成功后使用jetty:run来运行jetty服务 -->
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
以上配置,在调用package命令时会启动jetty
使用Tomcat作为web容器时,只需将 jetty 插件的定位换成 Tomcat 插件的定位即可
访问方式跟jetty有点不一样,jetty不需要填入项目名
----------------------------------------------------
发现一件尴尬的事情:
直接通过Tomcat:run运行,访问项目报错
最后附上我的pom
<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.joey.mavendemo</groupId>
<artifactId>mavendemo</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>mavendemo Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>mavendemo</finalName>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<!-- 在打包成功后使用tomcat:run来运行tomcat服务 -->
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
完~