第一种是使用内嵌的Tomcat插件
在pom.xml中添加插件:
<plugin> <groupId>org.codehaus.cargo</groupId> <artifactId>cargo-maven2-plugin</artifactId> <configuration> <container> <containerId>tomcat6x</containerId> <type>remote</type> </container> <configuration> <type>runtime</type> <properties> <cargo.tomcat.manager.url> http://localhost:8080/manager </cargo.tomcat.manager.url> <!-- 这是第一步添加的用户 --> <cargo.remote.username>admin</cargo.remote.username> <cargo.remote.password>admin</cargo.remote.password> </properties> </configuration> <deployer> <type>remote</type> <deployables> <deployable> <groupId>xxx</groupId> <artifactId>xxx</artifactId> <type>war</type> </deployable> </deployables> </deployer> </configuration> </plugin>
启动命令:tomcat:run -Dmaven.tomcat.port=80 //端口参数可以省略,默认8080
第二种是使用外部的 Tomcat 来启动
1右击新建的maven工程,打开工程属性,选中Project Facets,在右边选中java和Dynamic Web Module(先选java),点击OK,这样,工程就变成一个web工程了。
2打开工程属性,选中左边的Deploymemnt Assemly(web工程才有这个选项)。
3删除Source为/WebContent的那行,该行没用。
4添加web的根目录: Add -> 选中Folder -> Next -> 选中src/main/webapp -> Finish;
5添加依赖包:Add -> 选中Java Build Path Entries -> Next -> 选中Maven Dependencies -> Finish。
6发布到window ->preferences -> server -> runtime environment里的tomcat。