基于Maven的web项目的标准项目结构
user-project
user-core
user-service
user-web
src
main
java
Java源代码包括Servlet等
webapp //名字必须是webapp且在src目录下
WEB-INF
web.xml
javascript
js文件
css
css文件
html
html文件
target
classes
pom.xml
在下面的叙述中,没有依照上面的标准结构,而是使用Intellij Idea创建基于Maven的web项目时,创建的目录结构
user-project
user-core
user-service
user-web
src
main
java
Java源代码包括Servlet等
web//名称任意,放在跟src平级目录下
WEB-INF
web.xml
javascript
js文件
css
css文件
html
html文件
target
classes
pom.xml
说明:
1. user-web是maven模块,它的src/main目录下包含了一个web目录,web目录下包含WEB-INF/web.xml这个web工程的标准结构(不包含classes和lib),在web目录下可以建立javascript,css和html这些webui元素的目录或者直接在web目录下放置js,html和css文件,差别在于代码中引用这些文件的路径不一样
2. 为什么基于Maven的标准web项目,web目录会放置到src/main目录下? 这是相对于maven-jetty-plugin而言的,因为maven-jetty-plugin默认是到src/main/webapp寻找web-app(webAppSourceDirectory默认是src/main/webapp),所以,默认情况下是把webapp放置到src/main目录下
maven-jetty-plugin
使用maven-jetty-plugin可以使得我们实现在开发过程过程中以debug方式启动,进行调试和修改的热替换,因此,我们在开发过程中通常使用maven-jetty-plugin来启动我们的web项目
maven-jetty-plugin的地址http://www.eclipse.org/jetty/documentation/current/jetty-maven-plugin.html
在user-web模块的pom.xml中添加如下配置:
<build> <plugins> <!--jetty plugin to manage embedded jetty--> <!--No goal is specified--> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> <version>6.1.26</version> <configuration> <connectors> <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector"> <port>8668</port> <maxIdleTime>30000</maxIdleTime> </connector> </connectors> <webAppSourceDirectory>${project.basedir}/web </webAppSourceDirectory> <contextPath>/user</contextPath> </configuration> </plugin> </plugins> </build>
运行jetty:run之后,jetty的启动日志:
/software/devsoftware/jdk1.7.0_55/bin/java -Dmaven.home=/software/devsoftware/apache-maven-3.2.1 -Dclassworlds.conf=/software/devsoftware/apache-maven-3.2.1/bin/m2.conf -Didea.launcher.port=7534 -Didea.launcher.bin.path=/software/devsoftware/idea-IU-135.690/bin -Dfile.encoding=UTF-8 -classpath /software/devsoftware/apache-maven-3.2.1/boot/plexus-classworlds-2.5.1.jar:/software/devsoftware/idea-IU-135.690/lib/idea_rt.jar com.intellij.rt.execution.application.AppMain org.codehaus.classworlds.Launcher -Didea.version=13.1.2 org.mortbay.jetty:maven-jetty-plugin:6.1.7:run [INFO] Scanning for projects... [INFO] [INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building user.web 1.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] >>> maven-jetty-plugin:6.1.7:run (default-cli) @ user.web >>> [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ user.web --- [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:compile (default-compile) @ user.web --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ user.web --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory ~/development/learnmaven/user.manager/user.web/src/test/resources [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ user.web --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] <<< maven-jetty-plugin:6.1.7:run (default-cli) @ user.web <<< [INFO] [INFO] --- maven-jetty-plugin:6.1.7:run (default-cli) @ user.web --- [INFO] Configuring Jetty for project: user.web [INFO] Webapp source directory = ~/development/learnmaven/user.manager/user.web/web [INFO] web.xml file = ~/development/learnmaven/user.manager/user.web/web/WEB-INF/web.xml [INFO] Classes = ~/development/learnmaven/user.manager/user.web/target/classes [INFO] Logging to org.slf4j.impl.SimpleLogger(org.mortbay.log) via org.mortbay.log.Slf4jLog [INFO] Context path = /user [INFO] Tmp directory = determined at runtime [INFO] Web defaults = org/mortbay/jetty/webapp/webdefault.xml [INFO] Web overrides = none [INFO] Webapp directory = ~/development/learnmaven/user.manager/user.web/web [INFO] Starting jetty 6.1.7 ... [INFO] jetty-6.1.7 [INFO] No Transaction manager found - if your webapp requires one, please configure one. [INFO] Started SelectChannelConnector@0.0.0.0:8668 [INFO] Started Jetty Server
从启动日志中可以看到:
1.Jetty已经启动,并且在8668端口监听
2.~/development/learnmaven/user.manager/user.web/web/WEB-INF目录下仅有1个web.xml文件,并没有classes和lib目录,那么Jetty到哪里找项目的classes?从日志中,我们看到有条日志信息:Classes = ~/development/learnmaven/user.manager/user.web/target/classes,表示jetty把这个目录作为classpath的一部分将项目的classes加入进来,而不是通过WEB-INF/classes。
日志输出中,也包括了web.xml文件的路径web.xml file =~/development/learnmaven/user.manager/user.web/web/WEB-INF/web.xml。
3.项目中把web目录放置在项目跟目录下(跟src同级),而Jetty默认要找的路径是src/main/webapp,因此,需要在Jetty插件中指定web应用的目录,如日志:
Webapp source directory = ~/development/learnmaven/user.project/user.web/web
访问web页面
1.http://localhost:8668/user/index.jsp
2.http://localhost:8668/user/servlets/userservlet
相关推荐
最新maven视频教程加源码笔记,内容完整有保证,是学习maven的不可多得的教程。
云的学习笔记-云的学习笔记系统-云的学习笔记系统源码-云的学习笔记管理系统-云的学习笔记管理系统java代码-云的学习笔记系统设计与实现-基于ssm的云的学习笔记系统-基于Web的云的学习笔记系统设计与实现-云的学习...
Maven 构建SpringMVC项目学习笔记 一、背景介绍 二、构建工程 2.1、用maven插件构建项目框架 2.2、完善项目 2.3、将工程变成web工程 三、工程SpringMVC配置 3.1 赋予工程的springmvc特性 3.2、添加Maven依赖 四、...
可能是web应用的文档根目录,或者还放一些应用服务器的配置文件。在一个Java 项目中,Java类放在src/main/java下面,而classpath资源文件放在src/main/resources下面。 /main/java /src/test/ 我们...
云的学习笔记-云的学习笔记系统-云的学习笔记系统源码-云的学习笔记管理系统-云的学习笔记管理系统java代码-云的学习笔记系统设计与实现-基于ssm的云的学习笔记系统-基于Web的云的学习笔记系统设计与实现-云的学习...
尚硅谷学习笔记+代码,Maven 教程 Maven 是 Apache 软件基金会组织维护的一款专门为 Java 项目提供构建和依赖管理支持的工具 1、构建 Java 项目开发过程中,构建指的是使用 『原材料生产产品』 的过程。 原材料 Java...
学习maven笔记,尚不能实现自动发布,parent工程package之后,把module项目的war手工发布到tomcat的webapp下。
50页文档带你解读maven详细,开发工具myeclipse ,带你从0到1,详细学习maven的部署使用命令,开发工具集成打包等等!
学习crm的javaweb项目文章由B站动力节点相关课程视频整理而成,不作为基础入门教程,只作为备忘的学习笔记。
SpringMVC详细学习笔记,有关源码的提取,有关实例代码的提取,有关maven的配置,web.xml的配置,还有高阶采用注解来代替配置文件的类的书写方法。
springboot学习笔记 spring基础 Spring概述 Spring的简史 xml配置 注解配置 java配置 Spring概述 Spring的模块 核心容器CoreContainer Spring-Core Spring-Beans ...
Java Web的在线商城项目实战视频教程 包含:源码+笔记+视频 使用技术Maven SSM Oracle ,本人亲自录制