myeclipse搭建maven web工程

时间:2022-03-21 20:12:51

最近利用myeclipse 搭建maven web工程时,使用IDE自动创建的目录结构,然后被狠狠的坑了一把,即maven 打包和IDE部署到tomcat 目录时没有把web.xml文件部署进去,下面演示整个过程:

第一步:利用IDE创建maven web 工程,操作步骤如下:

  • File- > New -> Maven Project 弹出 New Maven Project 对话框,选择保存文件的位置后,点击 Next;
  • myeclipse搭建maven web工程
  • 在maven 模型中,Filter:webapp,找到创建maven 创建web 工程的模型,即选择:maven-archetype-webapp,点击Next:

  • myeclipse搭建maven web工程
  • 填写必要的信息,点击Finish:
  • myeclipse搭建maven web工程
  • 有IDE以 maven-archetype-webapp 模型创建的maven web工程完成,其目录结构如下:
  • myeclipse搭建maven web工程

有源文件夹和测试文件夹,它们又分为代码文件夹和资源文件夹,如没有这些文件夹的话需要手动补全。

右击项目->Properties->MyEclipse->Deployment  Assembly/Project Facets 查看项目的部署设置和项目属下,如下图:

myeclipse搭建maven web工程


myeclipse搭建maven web工程

但pom.xml 里面没有设置build 插件及对应的设置时,这个maven web 工程 是没有任何问题,可以在Tomcat中正常运行。

但当在pom.xml 中设置如下时:

  <build>
<finalName>yt-web-demo</finalName>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
<compilerArguments>
<verbose />
<bootclasspath>${java.home}/lib/rt.jar;${java.home}/lib/jce.jar</bootclasspath>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<warSourceDirectory>webapp</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>

<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.tld</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.tld</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>

这里时一些maven web的基础设置,假如现在给这个工程加上基本的spring mvc设置,并设置一个controller,然后用myeclipse直接部署到tomcat 下面发现spring mvc controller 不能访问(404错误),接下来用maven build 这个工程,查看结果发现build 的结果中,WEB-INF目录下面没有web.xml文件,

myeclipse搭建maven web工程

这里清晰的看到build成功了,但是WEB-INF 下面只有classes 文件,于是我就去百度了下,发现有人遇到这样的问题,需要在build的maven-war-plugin 中指定web.xml的位置,于是我增加一个设置:

<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<warSourceDirectory>webapp</warSourceDirectory>
<webXml>webapp/WEB-INF/web.xml</webXml>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configura

这里我指定了web.xml 文件的位置,然后在用maven build, 发现build 失败了,其结果如下:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building yt-web-demo Maven Webapp 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ yt-web-demo ---
[debug] execute contextualize
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ yt-web-demo ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ yt-web-demo ---
[debug] execute contextualize
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ yt-web-demo ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.10:test (default-test) @ yt-web-demo ---
[INFO] Surefire report directory: F:\dubbo-project\yt-web-demo\target\surefire-reports

-------------------------------------------------------
T E S T S
-------------------------------------------------------

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-war-plugin:2.3:war (default-war) @ yt-web-demo ---
[INFO] Packaging webapp
[INFO] Assembling webapp [yt-web-demo] in [F:\dubbo-project\yt-web-demo\target\yt-web-demo]
[INFO] Processing war project
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.285s
[INFO] Finished at: Mon Jul 31 09:18:06 CST 2017
[INFO] Final Memory: 12M/216M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.3:war (default-war) on project yt-web-demo: The specified web.xml file 'F:\dubbo-project\yt-web-demo\webapp\WEB-INF\web.xml' does not exist -> [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/MojoFailureException

这里说在工程下面没有web.xml文件,但是不是有啊,我看了下刚才在pom.xml中指定的web.xml 文件的位置,是:<webXml>webapp/WEB-INF/web.xml</webXml>,在对照项目web.xml文件的位置:src\main\webapp\WEB-INF\web.xml ,我擦,这个位置不对啊,于是我把整个webapp 剪切到项目根目录下:

myeclipse搭建maven web工程

然后build就成功了,在查看build 的文件:myeclipse搭建maven web工程

这里可以看到WEB-INF 下面的web.xml 文件又出现了,把项目的war 包丢到tomcat 下面跑起来,可以看到spring mvc 正常运行了,问题解决。