以下是我搭建的第一个maven web工程,在eclipse里本地开发环境能正常的跑,有一天,同事叫我部署他的一个maven web工程到远程服务器上,本以为像动态web工程一样,右键工程export成war包,直接扔到tomcat的webapps下即可。可是,另我们都失望的,程序跑起来了,也不报错,访问就是出现404错误!以下是我本人整理的资料,希望日后能让各位新手少踩坑!!
1:构建maven web工程
一直下一步,知道finished。第一个坑:刚建立的工程没有main/java,以及test/java目录。
奇怪了,开发工具没有给我们自动生成对应的目录???
后面也是在网友的帮助下,才知道如何解决该问题。
解决方法:右键工程,切换到bulid classpath下 ,其中main/java和test/java的状态为missing,选中其中一个,点击up或者down,确定后就会生成对应的目录。
现在正确生成了相关目录,在此,只是为了演示部署到远程tomcat过程,所以在main/java目录下不写任何代码,因为eclipse里生成的代码中index.jsp默认有hello world输出,我们只要访问该页面即可。
第二个坑:直接右键工程export成war包,放到远程tomcat的webapps下是运行不起来的。
右键run –>Maven bulid会出现如下错误:
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.157 s
[INFO] Finished at: 2017-11-24T09:15:02+08:00
[INFO] Final Memory: 6M/244M
[INFO] ------------------------------------------------------------------------
[ERROR] No goals have been specified for this build. You must specify a valid lifecycle phase or a goal in the format <plugin-prefix>:<goal> or <plugin-group-id>:<plugin-artifact-id>[:<plugin-version>]:<goal>. Available lifecycle phases are: validate, initialize, generate-sources, process-sources, generate-resources, process-resources, compile, process-classes, generate-test-sources, process-test-sources, generate-test-resources, process-test-resources, test-compile, process-test-classes, test, prepare-package, package, pre-integration-test, integration-test, post-integration-test, verify, install, deploy, pre-clean, clean, post-clean, pre-site, site, post-site, site-deploy. -> [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/NoGoalSpecifiedException
还必须在pom.xml文件的bulid下加上<defaultGoal>compile</defaultGoal>
<build>
<finalName>helloworld</finalName>
<defaultGoal>compile</defaultGoal>
</build>
这样下来才能bulid 成功!
经过这样后,我们要执行右键工程run as–>maven install,控制台会有生成war包的路径,这样在本地生成的war包放到远程tomcat下是可以正常访问的。