<!--[if !supportLists]-->1. <!--[endif]-->在MyEclipse中配置tomcat(注意:tomcat server必须选择为Enable,否则不可用)
<!--[if !vml]--><!--[endif]-->(图19)
------------------------------------------------------------4.示例------------------------------------------------------------
<!--[if !supportLists]-->1. <!--[endif]-->现在就使用上面的配置创建我们的第一个maven webapp工程吧,打开MyEclipse的File->New->Others->Maven->Maven Project
<!--[if !vml]--><!--[endif]-->(图20)
<!--[if !vml]--><!--[endif]-->(图21)
Catalog为All Catalogs,mabaen-archetype-webapp
<!--[if !vml]--><!--[endif]-->(图22)
Group Id和Artifact Id及Version都是Maven的几个关键元素,这三个元素定义了项目的基本坐标,在Maven的世界,任何的jar、pom或者war都是以基于这些基本的坐标进行区分的。
groupId定义了项目属于哪个组(可以理解为项目的package),artifactId定义了当前Maven项目在组中唯一的ID(可以理解为项目名称),version指定了项目当前的版本。
<!--[if !vml]--><!--[endif]-->(图23)
<!--[if !supportLists]-->2. <!--[endif]-->了解MyEclipse中项目的结构,web项目必须在src\main\webapps下面才可以成功编译和部署。
<!--[if !vml]--><!--[endif]-->(图24)
<!--[if !supportLists]-->3. <!--[endif]-->修改index文件,做一个简单的标示。成功运行项目后,页面将显示为如下信息
<!--[if !vml]--><!--[endif]-->(图25)
<!--[if !supportLists]-->4. <!--[endif]-->pom.xml文件是maven的核心(pom:Project Object Model),在该文件中可以加载任何项目所需的插件,系统会自动将这些项目所需的插件从maven repository center下载下来。因为要希望把项目部署到外部tomcat下面,所以此处需要加入tomcat-maven插件,以及希望启动的tomcat配置
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat6-maven-plugin</artifactId>
<version>2.0-beta-1</version>
<configuration>
<url>http://localhost:6080/manager</url>
<server>tomcat6</server>
<port>6080</port>
<path>/MyFirstMaven</path>
</configuration>
</plugin>
</plugins><!--[if !vml]--><!--[endif]-->(图26)
<!--[if !supportLists]-->5. <!--[endif]-->运行工程,选择工程名邮件Run As->maven clean,会将之前编译的内容清除重新编译。
<!--[if !vml]--><!--[endif]-->(图27)
然后执行maven bulid,并在goals中输入package tomcat6:deploy
<!--[if !vml]--><!--[endif]-->(图28)
先查看一下(图28)run之前tomcat\webapps目录下的内容
<!--[if !vml]--><!--[endif]-->(图29)
Run(图28),当MyEclipse中的console控制台输出的内容为BUILD SUCCESS时,就表示部署成功了
<!--[if !vml]--><!--[endif]-->(图30)
(图28)Run之后tomcat\webapps目录下的内容,多了MyFirstMaven和MyFirstMaven.war两个文件,表示项目发布成功
<!--[if !vml]--><!--[endif]-->(图31)
MyEclipse->项目右键->Run As->Maven Build,输入tomcat6:run,就可启动tomcat。
<!--[if !vml]--><!--[endif]-->(图32)
出现如下信息表示tomcat启动成功
<!--[if !vml]--><!--[endif]-->(图33)
在浏览器中输入http://localhost:6080/MyFirstMaven并访问
<!--[if !vml]--><!--[endif]-->(图34)
Ok,maven webapp项目实例完成。
------------------------------------------------5.问题解答--------------------------------------------------------
在maven build工程的时候,大多数资料都是显示tomcat:deploy等等,于是一开始也是按照tomcat:deploy格式输入的,结果会产生大量的报错,而且启动的tomcat端口是8080,是eclipse自带的tomcat,而不是我们设置的6080端口的tomcat。错误信息如下:
[DEBUG] (f) charset = ISO-8859-1
[DEBUG] (f) contextFile = D:\eclipse\workspaces\MyFirstMaven\target\MyFirstMaven\META-INF\context.xml
[DEBUG] (f) ignorePackaging = false
[DEBUG] (f) mode = war
[DEBUG] (f) packaging = war
[DEBUG] (f) path = /MyFirstMaven
[DEBUG] (f) update = false
[DEBUG] (f) url = http://localhost:8080/manager
[DEBUG] (f) version = 1.1
[DEBUG] (f) warFile = D:\eclipse\workspaces\MyFirstMaven\target\MyFirstMaven.war
[DEBUG] -- end configuration --
[INFO] Deploying war to http://localhost:8080/MyFirstMaven
[DEBUG] No server specified for authentication - using defaults
[INFO] ------------------------------------------------------------------------
[INFO]BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.797s
[INFO] Finished at: Wed Mar 13 11:24:37 CST 2013
[INFO] Final Memory: 6M/12M
[INFO] ------------------------------------------------------------------------
[ERROR]Failed to execute goal org.codehaus.mojo:tomcat-maven-plugin:1.1:deploy (default-cli) on project MyFirstMaven: Cannot invoke Tomcat manager: Server returned HTTP response code: 400 for URL: http://localhost:8080/manager/deploy?path=%2FMyFirstMaven&war= ->[Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.codehaus.mojo:tomcat-maven-plugin:1.1:deploy (default-cli) on project MyFirstMaven: Cannot invoke Tomcat manager
at org.apache.maven.lifecycle.internal.MojoExecutor.execute