Web、Java源代码、资源文件在Maven工程中的文件路径

来源:互联网 发布:qq音乐mac无法登陆 编辑:程序博客网 时间:2024/05/22 08:09
首先,我还是强烈建议使用 Maven的默认路径: src/main/webapp、src/main/java、src/test/java、src/main/resources,这样无需在pom.xml配置,也减少了很多出错的可能。移植也简单。Maven的缺省路径详见: https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html

有关Java EE开发的资源很少能讲到一些约定的细节,比如Web文件资源在Maven工程中的文件路径。Maven中的默认路径是: src/main/webapp,这个路径就是放置你的jsp文件。下面一般有两个目录META-INF和WEB-INF。默认的路径是无需在Maven的pom.xml配置文件中指定的。

但是如果一些项目不使用默认路径,这样需要在pom.xml中添加      
  1. <plugin>
  2.         <artifactId>maven-war-plugin</artifactId>
  3.         <version>3.0</version>
  4.         <configuration>
  5.           <warSourceDirectory>WebContent</warSourceDirectory>
  6.           <failOnMissingWebXml>false</failOnMissingWebXml>
  7.         </configuration>
  8. </plugin>
这样,web文件的路径在工程的WebContent目录,详细的说明见: http://maven.apache.org/plugins/maven-war-plugin/war-mojo.html

Java 源代码文件资源在Maven工程中的默认路径是: src/main/java,这个路径就是放置你的Java源代码文件。默认的路径是无需在Maven的pom.xml配置文件中指定的。如果需要指定路径,可以在pom.xml中添加:
  1. <build>
  2.     <sourceDirectory>src</sourceDirectory>
  3.   </build>

资源文件的缺省路径为src/main/resources,这样Maven在打包成war文件的时候,会将src/main/resources的资源文件复制到class目录。
因此对于Spring mvc项目,servlet的配置文件springmvc-context.xml缺省会放置在 src/main/resources/springmvc-context.xml。对应的的web.xml指定的<param-value>classpath:springmvc-context.xml</param-value中,servlet的配置文件springmvc-context.xml 需要从 class目录下查找。之前,我也是一直对这句话看不太懂。了解Maven的缺省目录和打包行为之后,才看懂这句话(下文的第6行)

  1. <servlet>
  2.     <servlet-name>xxxx</servlet-name>
  3.     <servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class>
  4.     <init-param>
  5.       <param-name>contextConfigLocation</param-name>
  6.       <param-value>classpath:springmvc-context.xml</param-value>
  7.     </init-param>
  8.     <load-on-startup>1</load-on-startup>
  9.   </servlet>
0 0
原创粉丝点击