日期:2014-05-18  浏览次数:20703 次

如何制作war程序?
如何打包java web程序?

------解决方案--------------------
比较好的做法是写一个ant脚本来做这个,不过思路是一样的,就是把你手工作的东西程序化了。
------解决方案--------------------
XML code
<?xml version="1.0" encoding="GB2312"?>
<project name="jboss" basedir="." default="ear">
  <property file="./build.properties" />
  <property environment="env" />
  <property name="project.root" value="." />
  <property name="project.lib.dir" value="${project.root}/lib" />
  <property name="src.dir" value="${project.root}/src" />
  <property name="src.java.dir" value="${src.dir}/java" />
  <property name="src.config.dir" value="${src.dir}/config" />
  <property name="src.jsp.dir" value="${project.root}/webroot" />
  <property name="dist.dir" value="${project.root}/dist" />
  <property name="deploy.exploded.dir" value="${dist.dir}/project/${project.name}-exploded-ear" />
  <property name="deploy.ear.dir" value="${dist.dir}/project" />

  <property name="build.dir" value="${project.root}/build" />

  <path id="master-classpath">
     <fileset file="${j2ee.jar}" />
     <fileset dir="${project.lib.dir}" />
  </path>

  <target name="clean" description="清空所有输出文件包括build和部署目录">
    <delete dir="${build.dir}/**" />
    <delete dir="${deploy.exploded.dir}" />
    <delete dir="${deploy.exploded.dir}/${project.name}.ear" />
  </target>

  <target name="compile" description="开始编译">
    <mkdir dir="${build.dir}" />
    <javac destdir="${build.dir}" target="1.5" debug="true" deprecation="false" optimize="false" failonerror="true">
      <src path="${src.dir}" />
      <classpath refid="master-classpath" />
    </javac>
  </target>

  <target name="ear" depends="exploded-ear" description="开始打包EAR">
    <jar jarfile="${deploy.ear.dir}/${project.name}-ejb.jar" basedir="${deploy.exploded.dir}/${project.name}-ejb.jar" />
    <jar jarfile="${deploy.ear.dir}/${project.name}-ejb.war" 
    basedir="${deploy.exploded.dir}/${project.name}.war" />
    <jar jarfile="${deploy.ear.dir}/${project.name}-ejb.ear">
      <fileset dir="${deploy.exploded.dir}">
        <exclude name="APP-INF/classes/context-${project.name}.properties" />
        <include name="APP-INF/**" />
        <include name="META-INF/**" />
      </fileset>
      <fileset dir="${deploy.ear.dir}">
        <include name="${project.name}-ejb.jar" />
        <include name="${project.name}.war" />
      </fileset>
    </jar>
  </target>

  <target name="exploded-ear" depends="clean,compile">
    <mkdir dir="${deploy.exploded.dir}/APP-INF/classes" />
    <mkdir dir="${deploy.exploded.dir}/APP-INF/lib" />
    <mkdir dir="${deploy.exploded.dir}/META-INF" />
    <mkdir dir="${deploy.exploded.dir}/${project.name}-ejb.jar/META-INF" />
    <mkdir dir="${deploy.exploded.dir}/${project.name}.war/WEB-INF/lib" />
    <mkdir dir="${deploy.exploded.dir}/${project.name}.war/WEB-INF/classes" />
    <copy todir="${deploy.exploded.dir}/${project.name}-ejb.jar/META-INF&qu