用Maven构建Java Web开发环境(Jetty容器)之二

本文接上一篇[url=http://sarin.iteye.com/blog/784275]第一部分[/url]继续来介绍。
目前为止我们还是手工命令行方式执行程序的,没有和IDE结合,其实Maven天生就对Eclipse做了集成,我们使用mvn eclipse:eclipse就得到了一个Eclipse的项目结构,在Eclipse中使用import功能就能直接导入到IDE中了。我们来看一下这个过程:
[img]http://dl.iteye.com/upload/attachment/330787/4930d78c-45e0-391f-9698-b26e75ab8bee.jpg[/img]
此时的demo就是Eclipse项目格式的了,出现了.project和.classpath文件。我们在Eclipse中引入这个项目,此时的Eclipse没有安装Maven插件,不能自动运行Maven命令,我们来安装Maven的Eclipse插件M2E。
[img]http://dl.iteye.com/upload/attachment/330789/2adb5bf7-6d68-335e-b01f-7aa494ab3f4c.jpg[/img]
在Eclipse的Install New Software中直接选择安装即可,非常简单。下面我们来创建Web项目并导入Eclipse中,在Jetty容器中运行程序。首先执行mvn archetype:generate命令创建。
[img]http://dl.iteye.com/upload/attachment/330791/fa5d1bab-8001-374f-a73d-5a4992b9de26.jpg[/img]
可以看到,刚创建的web项目结构包含了resources目录,而没有java代码目录,我们需要手工创建,在Eclipse中创建source folder,路径为src/main/java/src,现在我们得到如下一个项目结构,新建一个Servlet用于测试。
[img]http://dl.iteye.com/upload/attachment/330793/95c14bc9-8512-3acd-823c-0e9753442a5d.jpg[/img]
此时,项目中没有Servlet的依赖,需要添加,我们使用m2eclipse插件来直接添加依赖,如下所示:
[img]http://dl.iteye.com/upload/attachment/330797/bb5427fb-172a-3288-99ff-452ab492df3b.jpg[/img]
相应的XML为:

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<type>jar</type>
<scope>compile</scope>
</dependency>

下面就可以编写Servlet了,很简单,就输出HelloWorld吧。

package org.ourpioneer.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloWorldServlet extends HttpServlet {

protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
this.process(request, response);
}

protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
this.process(request, response);
}

private void process(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
String title="Webapp Demo";
out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">");
out.println("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
out.println("<head>");
out.println("<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\" />");
out.println("<title>" + title + "</title>");
out.println("<body>");
out.println("<h1>Hello World!</h1>");
out.println("</body>");
out.println("</html>");
}
}

然后不能忘了在web.xml中配置这个Servlet,这里是Servlet 2.5的规范,不是Servlet 3,不能用注解。这也很简单。

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<display-name>Archetype Created Web Application</display-name>

<servlet>
<servlet-name>helloworld</servlet-name>
<servlet-class>org.ourpioneer.servlets.HelloWorldServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>helloworld</servlet-name>
<url-pattern>/helloworld</url-pattern>
</servlet-mapping>
</web-app>

程序都有了,剩下就是运行了,Maven既然天生和Jetty是一对儿,这里我们就使用Jetty吧,在Maven中配置Jetty,首先是webdefault.xml要准备好,它是配置Jetty的,这个可以从Jetty的包中找到,并复制到resources下,这里多说一点,默认Jetty运行时是锁定JS/CSS等静态文件的,如果想在Jetty运行时也能修改它们,要在webdefault.xml中修改如下设置:

<init-param>
<param-name>useFileMappedBuffer</param-name>
<param-value>false</param-value>
</init-param>

Jetty也准备了,运行命令是jetty:run,这要在Maven中设置,那么需要在pom.xml中加入Jetty的插件的设置信息。这里直接贴出其整体构建信息。

<build>
<finalName>webapp</finalName>
<sourceDirectory>src/main/java/src</sourceDirectory>
<testSourceDirectory>src/test</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>utf-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>7.1.6.v20100715</version>
<configuration>
<stopKey>stop</stopKey>
<stopPort>5599</stopPort>
<webAppConfig>
<contextPath>/</contextPath>
<defaultsDescriptor>src/main/resources/webdefault.xml</defaultsDescriptor>
</webAppConfig>
<scanIntervalSeconds>0</scanIntervalSeconds>
<connectors>
<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
<port>80</port>
<maxIdleTime>60000</maxIdleTime>
</connector>
</connectors>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.7</version>
<configuration>
<addVersionToProjectName>false</addVersionToProjectName>
<useProjectReferences>false</useProjectReferences>
<encoding>UTF-8</encoding>
<wtpmanifest>false</wtpmanifest>
<wtpapplicationxml>true</wtpapplicationxml>
<wtpversion>1.5</wtpversion>
<additionalBuildcommands>
<buildcommand>org.eclipse.jdt.core.javabuilder</buildcommand>
<buildcommand>org.eclipse.wst.common.project.facet.core.builder</buildcommand>
<buildcommand>org.eclipse.wst.validation.validationbuilder</buildcommand>
</additionalBuildcommands>

<additionalProjectnatures>
<nature>org.springframework.ide.eclipse.core.springnature</nature>
<nature>org.maven.ide.eclipse.maven2Nature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
</additionalProjectnatures>
<classpathContainers>
<classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINER</classpathContainer>
</classpathContainers>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1-beta-1</version>
<configuration>
<warName>webapp</warName>
</configuration>
</plugin>
</plugins>
</build>

此时,更新一下Maven依赖,它们就都自动下载到本地了,到这个过程结束,我们就可以在Eclipse中配置Debug运行了。配置很简单,如下。
[img]http://dl.iteye.com/upload/attachment/330799/f4da7c4b-d6b6-304f-9df8-cecae7d17e87.jpg[/img]
这是Debug模式运行,Run模式下是一样的,用Debug模式可以在Eclipse中断点运行程序,非常便于调试。下面我们就让它跑起来吧。运行命令是jetty:run,Base directory配置是:${workspace_loc:/应用名},启动调试,看到如下信息,Jetty就成功启动了。
[img]http://dl.iteye.com/upload/attachment/330801/72b68f26-4776-3308-ba67-d427ea91bf4f.jpg[/img]
这里我们使用了80端口,配置方式在pom.xml中,上面的代码已经体现了。在浏览器中访问地址如下:http://localhost/helloworld,之后,我们就看到了效果。
[img]http://dl.iteye.com/upload/attachment/330803/6d621bfe-0628-3c96-a6f7-44f4a9f0e12a.jpg[/img]
本文系作者本人的实践和探索,希望对使用者有用,欢迎交流。
(全篇完)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值