基于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 [email protected]: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