windows 环境下 eclipse + maven + tomcat 的 hello world 创建和部署

主要记录自己一个新手用 eclipse + maven + tomcat 搭建 hello world 的过程,以及遇到的问题。讲真都是自己通过百度和谷歌一步步搭建的项目,没问过高手,也没高手可问,由于之前只接触过 .net 和 android 的开发,要知道.net 的开发环境 vs 和 android 的 studio 都是开箱即用,没搭建过 java 的开发环境,遇到几个好致命的问题,花了两天时间不断的google、baidu、尝试才处理掉,差点放弃了。难以想象,如果没有互联网,得交多少学费才能学会搭建一个项目框架。

以下图的项目名 “hellowork” 单词拼错了,应该是 helloworld , 但不影响演示,就不做修改,重新截图麻烦呀,理解理解。

配置开发环境

  1. 下载 JDK 安装;

  2. 下载 Tomcat 安装;

  3. 打开环境变量配置界面

  4. 添加以下环境变量:

JAVA_HOME:

 C:\Program Files\Java\jdk1.8.0_131

CLASSPATH:

 .;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar;C:\Program Files\Apache Software Foundation\Tomcat 8.5\lib\servlet-api.jar;

在 PATH 后面添加

;%JAVA_HOME%\bin;

注意 C:\Program Files\Java 是你的 JDK 安装所在路径, C:\Program Files\Apache Software Foundation\Tomcat 8.5 是 Tomcat 安装所在的路径,还有标点符号也要;

  1. 安装Eclipse 安装, 下载 j2ee 版本 ;

创建 Hello World 项目

  1. 在 eclipse 打开菜单 File -> New -> Maven Project, 将会出现以下界面

直接点 Next

选择 "maven-archetypes-webapp", 然后 Next

输入 group id, artifact id, 然后 Finish

项目创建完毕,但很多情况下,还有其他问题,导致项目还不能编译通过,下面是我遇到过的问题及解决方案;

项目 Error 及解决方案

错误一: The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path index.jsp, 如下图

解决方案:

  1. 添加运行环境: eclipse -> Window -> Preferences, 如下图添加自己安装的 tomcat 版本

  2. 点击 Next, 如下图,指定 Tomcat 所在的路径

  3. 指定 JRE, 在下拉中选择安装的 jre 版本

  1. 如果下拉中没有 jdk 文件夹下的 jre,则点击“Installed JREs...”, 添加已安装的 JRE

选择 Standard VM, 点 Next,指点 JRE 所在的路径,然后 Finish

  1. 添加成功后

  2. 然后再右击项目 -> Properties, 进行以下操作

选择 Apache Tomcat v8.5, (v8.5自己安装的 tomcat 版本)

然后再到 Order and Export 栏位勾选刚添加的库: Apache Tomcat v8.5[Apache Tomcat v8.5]

确定后,错误提示消失了

运行项目

  1. 运行前,先添加服务器

    点击上图的“No servers are available. Click this link to create a new server”,

选择自己安装的 tomcat 版本,我的是Tomcat v8.5 Server, 然后点击 Finish, 可看到在左边的工程里多出了一个 Servers 项目,同样在底部的 Servers 看到刚才添加的服务器

  1. 右击项目“hellowork”, 选择 Run as -> Run On Server, 弹出以下界面,点击 Finish

正常在浏览器看到 Hello World。 到这里我发现自己打错了 hello world, 项目名写成了 hellowork [尴尬]

  1. 有可能出现端口被占用的错误"Several ports (8005, 8080, 8009) required by Tomcat v8.5 Server at localhost are already in use. The server may already be running in another process, or a system process may be using the port. To start this server you will need to stop the other process or change the port number(s).", 不能同时运行两个 tomcat, 先把运行中的 tomcat 停掉

  1. 如果问题还在,进入 cmd 输入 netstat -ao 查看系统端口被占用的信息


上图看到 8005 端口被 13116 这进程占用了, 打开“任务管理器”找到该进程


原来是 javaw 占用了,把它结束掉,再运行就可以了。

编译项目

  1. 右击 helloworld 项目,选择 Run As -> Maven Build, 会弹出以下界面

在 Goals 中输入 clean package , 然后点击 Run, 首次运行可能有些库要下载,如果网速慢,会等上 20 分钟;

如果报以下错误:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test) on project hellowork: Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test failed: Unable to load the mojo 'test' in the plugin 'org.apache.maven.plugins:maven-surefire-plugin:2.12.4'. A required class is missing: org/apache/maven/plugin/surefire/SurefireReportParameters
[ERROR] -----------------------------------------------------
[ERROR] realm =    plugin>org.apache.maven.plugins:maven-surefire-plugin:2.12.4
[ERROR] strategy = org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy
[ERROR] urls[0] = file:/C:/Users/Grissom/.m2/repository/org/apache/maven/plugins/maven-surefire-plugin/2.12.4/maven-surefire-plugin-2.12.4.jar
[ERROR] urls[1] = file:/C:/Users/Grissom/.m2/repository/org/apache/maven/surefire/surefire-booter/2.12.4/surefire-booter-2.12.4.jar
[ERROR] urls[2] = file:/C:/Users/Grissom/.m2/repository/org/apache/maven/surefire/surefire-api/2.12.4/surefire-api-2.12.4.jar
[ERROR] urls[3] = file:/C:/Users/Grissom/.m2/repository/org/apache/maven/surefire/maven-surefire-common/2.12.4/maven-surefire-common-2.12.4.jar
[ERROR] urls[4] = file:/C:/Users/Grissom/.m2/repository/org/apache/commons/commons-lang3/3.1/commons-lang3-3.1.jar
[ERROR] urls[5] = file:/C:/Users/Grissom/.m2/repository/org/apache/maven/shared/maven-common-artifact-filters/1.3/maven-common-artifact-filters-1.3.jar
[ERROR] urls[6] = file:/C:/Users/Grissom/.m2/repository/org/codehaus/plexus/plexus-utils/3.0.8/plexus-utils-3.0.8.jar
[ERROR] urls[7] = file:/C:/Users/Grissom/.m2/repository/org/apache/maven/reporting/maven-reporting-api/2.0.9/maven-reporting-api-2.0.9.jar
[ERROR] urls[8] = file:/C:/Users/Grissom/.m2/repository/org/apache/maven/plugin-tools/maven-plugin-annotations/3.1/maven-plugin-annotations-3.1.jar
[ERROR] Number of foreign imports: 1
[ERROR] import: Entry[import  from realm ClassRealm[maven.api, parent: null]]
[ERROR] 
[ERROR] -----------------------------------------------------: org.apache.maven.plugin.surefire.SurefireReportParameters
[ERROR] -> [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/PluginContainerException


解决方案: 在 pom.xml 中的 build 改为:


	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>2.4.2</version>
				<configuration>
					<skipTests>true</skipTests>
				</configuration>
			</plugin>
		</plugins>
	</build>

即跳过测试。

编译成功后,Console 显示的信息

部署到 Tomcat

编译好的 war 文件默认放在项目的 target 文件夹下(上图中的 [INFO] Building war: C:\Users\Grissom\Documents\workspace\hellowork\target\hellowork-0.0.1-SNAPSHOT.war),把它考到 Tomcat 的 \webapps 目录下,并改名成 hellowork.war , 重启 Tomcat,在浏览器访问 http://localhost:8080/hellowork/

posted @ 2017-05-06 15:55  Grissom007  阅读(7819)  评论(1编辑  收藏  举报