使用ant编译打包、部署简单的javaweb项目 --01

目录结构:


D:\code\kongee\ant_web_hello
λ tree /f
卷 软件 的文件夹 PATH 列表
卷序列号为 000D-CD8E
D:.
│  .classpath
│  .project
│  build.xml
│
├─.settings
│      .jsdtscope
│      org.eclipse.jdt.core.prefs
│      org.eclipse.wst.common.component
│      org.eclipse.wst.common.project.facet.core.xml
│      org.eclipse.wst.jsdt.ui.superType.container
│      org.eclipse.wst.jsdt.ui.superType.name
│
├─src
│  └─com
│      └─laolang
│          └─servlet
│                  HelloServlet.java
│
└─WebContent
    │  hello.jsp
    │
    ├─META-INF
    │      MANIFEST.MF
    │
    └─WEB-INF
        │  web.xml
        │
        └─lib

D:\code\kongee\ant_web_hello


代码:


com.laolang.servlet.HelloServlet.java


package com.laolang.servlet;

import java.io.IOException;

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

/**
 * Servlet implementation class HelloServlet
 */
public class HelloServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HelloServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setAttribute("hello", "world");
		RequestDispatcher dis = request.getRequestDispatcher("hello.jsp");
		dis.forward(request, response);
	}

}


hello.jsp



<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Hello:${hello }</h1>
</body>
</html>


web.xml



<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>ant_web_hello</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>HelloServlet</display-name>
    <servlet-name>HelloServlet</servlet-name>
    <servlet-class>com.laolang.servlet.HelloServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>HelloServlet</servlet-name>
    <url-pattern>/hello.do</url-pattern>
  </servlet-mapping>
</web-app>


build.xml



<?xml version="1.0" encoding="UTF-8"?>
<project name="ant_web_hello">
	<!-- java源代码目录 -->
	<property name="src.dir" location="src" />
	<!-- 构建目录 -->
	<property name="build.dir" location="build" />
	<!-- class文件目录 -->
	<property name="build.classes" location="${build.dir}/classes" />
	<!-- 打包目录 -->
	<property name="build.war" location="${build.dir}/war" />

	<!-- tomcat根目录 -->
	<property name="tomcat.home" location="D:\program\program\java\tomcat\tomcat" />
	<!-- <property name="tomcat.lib" location="${tomcat.home}/lib" /> -->
	<!-- web 应用的名字,也是打包后war的名字 -->
	<property name="web.name" value="anthello" />
	<!-- web 根目录 -->
	<property name="web.root" value="webContent" />
	<property name="web.WEB-INF" location="${web.root}/WEB-INF" />
	<property name="web.lib" location="${web.WEB-INF}/lib" />
	<!-- 加载环境变量 -->
	<property environment="env" />

	<!-- 定义编译时的classpath -->
	<path id="compile.path">
		<fileset dir="${web.lib}" includes="*.lib">
		</fileset>
		<fileset dir="${env.tomcat_home}/lib">
			<include name="**/*.jar" />
		</fileset>
	</path>

	<target name="init" description="初始化">
		<mkdir dir="${build.dir}" />
		<mkdir dir="${build.classes}" />
		<mkdir dir="${build.war}" />
		<echo>初始化工作结束!</echo>
	</target>

	<target name="compile" depends="init" description="编译">
		<javac destdir="build/classes" srcdir="src" includeantruntime="false" fork="true">
			<compilerarg line="-encoding UTF-8 " />
			<classpath refid="compile.path" />
		</javac>
		<echo message="编译完成!" />
	</target>


	<target name="war" depends="compile" description="打包war文件">
		<war destfile="${build.war}/${web.name}.war">
			<fileset dir="${web.root}" includes="**/*.*" />
			<lib dir="${web.lib}" />
			<webinf dir="${web.WEB-INF}" />
			<classes dir="${build.classes}" />
		</war>
		<echo>打包完成!</echo>
	</target>

	<target name="deploy" depends="war" description="发布">
		<copy todir="${env.tomcat_home}/webapps">
			<fileset dir="${build.war}" includes="*.war" />
		</copy>
		<echo>已发布到Tomcat!</echo>
	</target>

	<target name="clean" description="清理">
		<delete dir="${build.dir}" />
		<delete dir="${env.tomcat_home}/webapps/${web.name}"/>
		<delete file="${env.tomcat_home}/webapps/${web.name}.war"/>
		<echo>清理完成!</echo>
	</target>

	<target name="testenv" description="测试环境变量">
		<echo>JAVA_HOME:${env.JAVA_HOME}</echo>
		<echo>CLASSPATH:${env.CLASSPATH}</echo>
		<echo>tomcat_home:${env.tomcat_home}</echo>
	</target>


</project>


运行效果:



D:\code\kongee\ant_web_hello
λ ant -projecthelp
Buildfile: D:\code\kongee\ant_web_hello\build.xml

Main targets:

 clean    清理
 compile  编译
 deploy   发布
 init     初始化
 testenv  测试环境变量
 war      打包war文件

D:\code\kongee\ant_web_hello
λ ant deploy
Buildfile: D:\code\kongee\ant_web_hello\build.xml

init:
    [mkdir] Created dir: D:\code\kongee\ant_web_hello\build
    [mkdir] Created dir: D:\code\kongee\ant_web_hello\build\classes
    [mkdir] Created dir: D:\code\kongee\ant_web_hello\build\war
     [echo] 初始化工作结束!

compile:
    [javac] Compiling 1 source file to D:\code\kongee\ant_web_hello\build\classes
     [echo] 编译完成!

war:
      [war] Building war: D:\code\kongee\ant_web_hello\build\war\anthello.war
     [echo] 打包完成!

deploy:
     [copy] Copying 1 file to D:\program\program\java\tomcat\tomcat\webapps
     [echo] 已发布到Tomcat!

BUILD SUCCESSFUL
Total time: 7 seconds

D:\code\kongee\ant_web_hello




在firefox中的效果:

使用ant编译打包、部署简单的javaweb项目 --01

%tomcat_home%/webapps


D:\program\program\java\tomcat\tomcat\webapps      
λ dir                                              
 驱动器 D 中的卷是 软件                                     
 卷的序列号是 000D-CD8E                                  
                                                   
 D:\program\program\java\tomcat\tomcat\webapps 的目录 
                                                   
2015/11/04  17:06    <DIR>          .              
2015/11/04  17:06    <DIR>          ..             
2015/11/04  17:06    <DIR>          anthello       
2015/11/04  17:06             2,481 anthello.war   
2015/08/19  20:20    <DIR>          docs           
2015/08/19  20:20    <DIR>          examples       
2015/08/19  20:20    <DIR>          host-manager   
2015/10/18  14:22    <DIR>          jc_my_1_1      
2015/10/18  14:21         3,714,226 jc_my_1_1.war  
2015/08/19  20:20    <DIR>          manager        
2015/10/18  14:35    <DIR>          mytest_one     
2015/10/18  14:20    <DIR>          myweb          
2015/08/19  20:20    <DIR>          ROOT           
               2 个文件      3,716,707 字节             
              11 个目录 107,363,000,320 可用字节          
                                                   
D:\program\program\java\tomcat\tomcat\webapps



清理:


D:\code\kongee\ant_web_hello
λ ant clean
Buildfile: D:\code\kongee\ant_web_hello\build.xml

clean:
   [delete] Deleting directory D:\code\kongee\ant_web_hello\build
   [delete] Deleting directory D:\program\program\java\tomcat\tomcat\webapps\anthello
   [delete] Deleting: D:\program\program\java\tomcat\tomcat\webapps\anthello.war
     [echo] 清理完成!

BUILD SUCCESSFUL
Total time: 2 seconds

D:\code\kongee\ant_web_hello
λ



D:\program\program\java\tomcat\tomcat\webapps             
λ dir                                                     
 驱动器 D 中的卷是 软件                                            
 卷的序列号是 000D-CD8E                                         
                                                          
 D:\program\program\java\tomcat\tomcat\webapps 的目录        
                                                          
2015/11/04  17:10    <DIR>          .                     
2015/11/04  17:10    <DIR>          ..                    
2015/08/19  20:20    <DIR>          docs                  
2015/08/19  20:20    <DIR>          examples              
2015/08/19  20:20    <DIR>          host-manager          
2015/10/18  14:22    <DIR>          jc_my_1_1             
2015/10/18  14:21         3,714,226 jc_my_1_1.war         
2015/08/19  20:20    <DIR>          manager               
2015/10/18  14:35    <DIR>          mytest_one            
2015/10/18  14:20    <DIR>          myweb                 
2015/08/19  20:20    <DIR>          ROOT                  
               1 个文件      3,714,226 字节                    
              10 个目录 107,363,033,088 可用字节                 
                                                          
D:\program\program\java\tomcat\tomcat\webapps             
λ




下一步:

1、加入对mysql的控制,在安装必要的软件和设置了相关的环境变量之后 ,可以用一个ant命令就完成对数据库的初始化,对源文件的编译,打包并发布web项目

2、加入测试,但是在打包的时候,过滤测试产生的文件




你可能感兴趣的:(使用ant编译打包、部署简单的javaweb项目 --01)