`

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

阅读更多

目前为止我们还是手工命令行方式执行程序的,没有和IDE结合,其实Maven天生就对Eclipse做了集成,我们使用mvn eclipse:eclipse就得到了一个Eclipse的项目结构,在Eclipse中使用import功能就能直接导入到IDE中了。我们来看一下 这个过程:

    此时的demo就是Eclipse项目格式的了,出现了.project和.classpath文件。我们在Eclipse中引入这个项目,此时的 Eclipse没有安装Maven插件,不能自动运行Maven命令,我们来安装Maven的Eclipse插件M2E。

    在Eclipse的Install New Software中直接选择安装即可,非常简单。下面我们来创建Web项目并导入Eclipse中,在Jetty容器中运行程序。首先执行mvn archetype:generate命令创建。

    可以看到,刚创建的web项目结构包含了resources目录,而没有java代码目录,我们需要手工创建,在Eclipse中创建source folder,路径为src/main/java/src,现在我们得到如下一个项目结构,新建一个Servlet用于测试。

    此时,项目中没有Servlet的依赖,需要添加,我们使用m2eclipse插件来直接添加依赖,如下所示:

    相应的XML为:

Xml代码
  1. < dependency >   
  2.         < groupId > javax.servlet </ groupId >   
  3.         < artifactId > servlet-api </ artifactId >   
  4.         < version > 2.5 </ version >   
  5.         < type > jar </ type >   
  6.         < scope > compile </ scope >   
  7. </ dependency >   
    <dependency>
    		<groupId>javax.servlet</groupId>
    		<artifactId>servlet-api</artifactId>
    		<version>2.5</version>
    		<type>jar</type>
    		<scope>compile</scope>
    </dependency>


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

Java代码
  1. package  org.ourpioneer.servlets;  
  2.   
  3. import  java.io.IOException;  
  4. import  java.io.PrintWriter;  
  5.   
  6. import  javax.servlet.ServletException;  
  7. import  javax.servlet.http.HttpServlet;  
  8. import  javax.servlet.http.HttpServletRequest;  
  9. import  javax.servlet.http.HttpServletResponse;  
  10.   
  11. public   class  HelloWorldServlet  extends  HttpServlet {  
  12.   
  13.     protected   void  doGet(HttpServletRequest request,  
  14.             HttpServletResponse response) throws  ServletException, IOException {  
  15.         this .process(request, response);  
  16.     }  
  17.   
  18.     protected   void  doPost(HttpServletRequest request,  
  19.             HttpServletResponse response) throws  ServletException, IOException {  
  20.         this .process(request, response);  
  21.     }  
  22.   
  23.     private   void  process(HttpServletRequest request,  
  24.             HttpServletResponse response) throws  ServletException, IOException {  
  25.         response.setContentType("text/html;charset=utf-8" );  
  26.         response.setCharacterEncoding("utf-8" );  
  27.         PrintWriter out = response.getWriter();  
  28.         String title="Webapp Demo" ;  
  29.         out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">" );  
  30.         out.println("<html xmlns=\"http://www.w3.org/1999/xhtml\">" );  
  31.         out.println("<head>" );  
  32.         out.println("<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\" />" );  
  33.         out.println("<title>"  + title +  "</title>" );  
  34.         out.println("<body>" );  
  35.         out.println("<h1>Hello World!</h1>" );  
  36.         out.println("</body>" );  
  37.         out.println("</html>" );  
  38.     }  
  39. }  
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代码
  1. <? xml   version = "1.0"   encoding = "UTF-8" ?>   
  2. < web-app   version = "2.5"   xmlns = "http://java.sun.com/xml/ns/javaee"   
  3.     xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"   
  4.     xsi:schemaLocation ="http://java.sun.com/xml/ns/javaee   
  5.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">   
  6.   
  7.     < display-name > Archetype Created Web Application </ display-name >   
  8.   
  9.     < servlet >   
  10.         < servlet-name > helloworld </ servlet-name >   
  11.         < servlet-class > org.ourpioneer.servlets.HelloWorldServlet </ servlet-class >   
  12.     </ servlet >   
  13.   
  14.     < servlet-mapping >   
  15.         < servlet-name > helloworld </ servlet-name >   
  16.         < url-pattern > /helloworld </ url-pattern >   
  17.     </ servlet-mapping >   
  18. </ web-app >   
<?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中修改如下设置:

Xml代码
  1. < init-param >   
  2.   < param-name > useFileMappedBuffer </ param-name >   
  3.   < param-value > false </ param-value >   
  4. </ init-param >   
    <init-param>
      <param-name>useFileMappedBuffer</param-name>
      <param-value>false</param-value>
    </init-param>


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

Xml代码
  1. < build >   
  2.     < finalName > webapp </ finalName >   
  3.     < sourceDirectory > src/main/java/src </ sourceDirectory >   
  4.     < testSourceDirectory > src/test </ testSourceDirectory >   
  5.     < plugins >   
  6.         < plugin >   
  7.             < groupId > org.apache.maven.plugins </ groupId >   
  8.             < artifactId > maven-compiler-plugin </ artifactId >   
  9.             < version > 2.0.2 </ version >   
  10.             < configuration >   
  11.                 < source > 1.6 </ source >   
  12.                 < target > 1.6 </ target >   
  13.                 < encoding > utf-8 </ encoding >   
  14.             </ configuration >   
  15.         </ plugin >   
  16.         < plugin >   
  17.             < groupId > org.apache.maven.plugins </ groupId >   
  18.             < artifactId > maven-resources-plugin </ artifactId >   
  19.             < configuration >   
  20.                 < encoding > UTF-8 </ encoding >   
  21.             </ configuration >   
  22.         </ plugin >   
  23.         < plugin >   
  24.             < groupId > org.mortbay.jetty </ groupId >   
  25.             < artifactId > jetty-maven-plugin </ artifactId >   
  26.             < version > 7.1.6.v20100715 </ version >   
  27.             < configuration >   
  28.                 < stopKey > stop </ stopKey >   
  29.                 < stopPort > 5599 </ stopPort >   
  30.                 < webAppConfig >   
  31.                     < contextPath > / </ contextPath >   
  32.                     < defaultsDescriptor > src/main/resources/webdefault.xml </ defaultsDescriptor >   
  33.                 </ webAppConfig >   
  34.                 < scanIntervalSeconds > 0 </ scanIntervalSeconds >   
  35.                 < connectors >   
  36.                     < connector   implementation = "org.eclipse.jetty.server.nio.SelectChannelConnector" >   
  37.                         < port > 80 </ port >   
  38.                         < maxIdleTime > 60000 </ maxIdleTime >   
  39.                     </ connector >   
  40.                 </ connectors >   
  41.             </ configuration >   
  42.         </ plugin >   
  43.         < plugin >   
  44.             < groupId > org.apache.maven.plugins </ groupId >   
  45.             < artifactId > maven-eclipse-plugin </ artifactId >   
  46.             < version > 2.7 </ version >   
  47.             < configuration >   
  48.                 < addVersionToProjectName > false </ addVersionToProjectName >   
  49.                 < useProjectReferences > false </ useProjectReferences >   
  50.                 < encoding > UTF-8 </ encoding >   
  51.                 < wtpmanifest > false </ wtpmanifest >   
  52.                 < wtpapplicationxml > true </ wtpapplicationxml >   
  53.                 < wtpversion > 1.5 </ wtpversion >   
  54.                 < additionalBuildcommands >   
  55.                     < buildcommand > org.eclipse.jdt.core.javabuilder </ buildcommand >   
  56.                     < buildcommand > org.eclipse.wst.common.project.facet.core.builder </ buildcommand >   
  57.                     < buildcommand > org.eclipse.wst.validation.validationbuilder </ buildcommand >   
  58.                 </ additionalBuildcommands >   
  59.   
  60.                 < additionalProjectnatures >   
  61.                     < nature > org.springframework.ide.eclipse.core.springnature </ nature >   
  62.                     < nature > org.maven.ide.eclipse.maven2Nature </ nature >   
  63.                     < nature > org.eclipse.wst.common.project.facet.core.nature </ nature >   
  64.                     < nature > org.eclipse.jdt.core.javanature </ nature >   
  65.                     < nature > org.eclipse.wst.common.modulecore.ModuleCoreNature </ nature >   
  66.                 </ additionalProjectnatures >   
  67.                 < classpathContainers >   
  68.                     < classpathContainer > org.eclipse.jdt.launching.JRE_CONTAINER </ classpathContainer >   
  69.                 </ classpathContainers >   
  70.             </ configuration >   
  71.         </ plugin >   
  72.         < plugin >   
  73.             < groupId > org.apache.maven.plugins </ groupId >   
  74.             < artifactId > maven-war-plugin </ artifactId >   
  75.             < version > 2.1-beta-1 </ version >   
  76.             < configuration >   
  77.                 < warName > webapp </ warName >   
  78.             </ configuration >   
  79.         </ plugin >   
  80.     </ plugins >   
  81. </ build >   
	<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运行了。配置很简单,如下。

    这是Debug模式运行,Run模式下是一样的,用Debug模式可以在Eclipse中断点运行程序,非常便于调试。下面我们就让它跑起来吧。运行命令 是jetty:run,Base directory配置是:${workspace_loc:/应用名},启动调试,看到如下信息,Jetty就成功启动了。

    这里我们使用了80端口,配置方式在pom.xml中,上面的代码已经体现了。在浏览器中访问地址如下:http://localhost/helloworld,之后,我们就看到了效果。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics