JAVA工程部署方法

来源:互联网 发布:js new 对象 编辑:程序博客网 时间:2024/06/15 10:58

  

 项目部署方法:
    方法一:
          使用ECLIPS来部署项目,见项目直接通过ECLIPS发布到TOMCATE 中;
    方法二:
         使用build.xml,将项目通过它部署到TOMCATE中去;

        如:

    <?xml version="1.0" encoding="UTF-8" ?>
       <project name="Struts deploy" default="deploy" basedir=".">
       <property environment="env"/>
 <property file="build.properties" />
 <property name="tomcat.home" value="${env.TOMCAT_HOME}"/>
 <property name="deploy.dir" value="${tomcat.home}/webapps/${app.name}"/>
 <!-- Directory to hold compiled file -->
 <property name="build.dir" value="${basedir}/build"/>
 <!-- Source file directory -->
 <property name="source.dir" value="${basedir}/src"/>
 <!-- Directory to hold global shared library, should public to app server -->
 <property name="sharelib.dir" value="${basedir}/share"/>
 <!-- Directory to hold distributable package -->
 <property name="dist.dir" value="${basedir}/dist"/>
 <!-- Directory to hold web file, such as jsp, js, image -->
 <property name="web.dir" value="${basedir}/web"/>
 <!-- Directory to hold web system configuration, seldom change -->
 <property name="webinf.dir" value="${basedir}/web-inf"/>
 <!-- Directory to hold application configuration, frequent change -->
 <property name="config.dir" value="${basedir}/config"/>
 <!-- Directory to hold compile depended library, only for compilation -->
 <property name="dependency.dir" value="${basedir}/dependency"/>
 <!-- Directory to hold resource -->
 <property name="resource.dir" value="${basedir}/resource"/>
 <!-- Sub directory name to hold string resource -->
 <property name="resource.string" value="message"/>
 
 <!-- Global classpath -->
 <path id="cp">
  <fileset dir="${dependency.dir}" includes="*.jar"/>
  <fileset dir="${webinf.dir}/lib" includes="*.jar"/>
  <fileset dir="${sharelib.dir}" includes="*.jar"/>
 </path>

 <!-- TOMCAT_HOME must be existed -->
 <target name="check" unless="env.TOMCAT_HOME">
  <fail message="You must set TOMCAT_HOME environment variable!"/>
 </target>

 <!-- Tomcat prepare task, manually execute it if needed -->
 <target name="tomcat_prepare" if="env.TOMCAT_HOME">
  <copy todir="${tomcat.home}/common/lib">
   <fileset dir="${sharelib.dir}">
    <include name="**/*.jar" />
   </fileset>
  </copy>
 </target>

 <target name="clean">
  <delete dir="${build.dir}" />
  <delete dir="${deploy.dir}" />
  <delete dir="${dist.dir}" />
 </target>

 <!-- Preparation task -->
 <target name="prepare" depends="check">
  <mkdir dir="${build.dir}" />
  <mkdir dir="${deploy.dir}" />
  <mkdir dir="${deploy.dir}/WEB-INF" />
  <mkdir dir="${deploy.dir}/WEB-INF/classes" />
  <delete>
   <fileset dir="${build.dir}" includes="**/*.properties" />
  </delete>
 </target>

 <!-- Compile java source and properties file -->
 <target name="compile" depends="prepare">
  <javac encoding="${encoding.default}" srcdir="${source.dir}" destdir="${build.dir}" includes="**/*.java" debug="on" debuglevel="source,lines" classpathref="cp" />
  <!-- copy resource that attached to java source -->
  <copy todir="${build.dir}" includeemptydirs="false">
   <fileset dir="${source.dir}" excludes="**/*.java" />
  </copy>

  <native2ascii encoding="${encoding.default}" src="${resource.dir}/${resource.string}" dest="${build.dir}" includes="**/*" />
  <copy todir="${build.dir}" includeemptydirs="false">
   <fileset dir="${resource.dir}" excludes="${resource.string}/**/*" />
  </copy>
 </target>
 
 <!-- Deploy only web page to destination -->
 <target name="web" depends="prepare">
  <copy todir="${deploy.dir}" includeemptydirs="false">
   <fileset dir="${web.dir}" includes="**/*" />
  </copy>
 </target>

 <!-- Deploy web app to destination -->
 <target name="Webrebuild" depends="compile">
  <copy todir="${deploy.dir}/WEB-INF/classes" includeemptydirs="false">
   <fileset dir="${build.dir}" includes="**/*" />
  </copy>
  
  <copy todir="${deploy.dir}" includeemptydirs="false">
   <fileset dir="${web.dir}" includes="**/*" />
  </copy>
 </target>
 
 <!-- Deploy web app to destination -->
 <target name="deploy" depends="compile">
  <copy todir="${deploy.dir}/WEB-INF">
   <fileset dir="${webinf.dir}" includes="**/*" />
   <fileset dir="${config.dir}" includes="**/*" />
  </copy>

  <copy todir="${deploy.dir}/WEB-INF/classes" includeemptydirs="false">
   <fileset dir="${build.dir}" includes="**/*" />
  </copy>
  
  <copy todir="${deploy.dir}" includeemptydirs="false">
   <fileset dir="${web.dir}" includes="**/*" />
  </copy>
 </target>

 <!-- Package web app for distribution -->
 <target name="dist" depends="compile">
  <mkdir dir="${dist.dir}" />
  <war warfile="${dist.dir}/${app.name}.war" webxml="${config.dir}/web.xml" >
   <fileset dir="${web.dir}" />
   <classes dir="${build.dir}" />
   <webinf dir="${webinf.dir}" />
   <webinf dir="${config.dir}" excludes="web.xml" />
  </war>
 </target>
</project>

 

 

当部署成功后我们回在控制台是上看到 :

  

Buildfile: f:/workspace2/road/build.xml
check:
prepare:
   [delete] Deleting 3 files from F:/workspace2/road/build
compile:
    [javac] Compiling 1 source file to F:/workspace2/road/build
     [copy] Copying 1 file to F:/workspace2/road/build
[native2ascii] Converting 2 files from F:/workspace2/road/resource/message to F:/workspace2/road/build
deploy:
     [copy] Copying 3 files to D:/Tomcat5.0/webapps/road22/WEB-INF/classes
     [copy] Copying 1527 files to D:/Tomcat5.0/webapps/road22
BUILD SUCCESSFUL
Total time: 51 seconds

 


  方法三:
        在tomcate/conf/server.xml 中写上 
       <Context docBase="F:/workspace2/webwork/webapps/portlet/src/webapp" path="/context_path"  debug="1" reloadable="true"/>
         解释说明“
         <Context
                docBase="项目的绝对路径/项目名/webapp"
                path="/随意起名字"          但发布成功后要输入地址要上是:http://localhost:8080/随意起的名字
                debug="1"
                reloadable="true"/>
   

   写入的位置如:
 
      <Host name="localhost" debug="0" appBase="webapps"
        unpackWARs="true" autoDeploy="true"
        xmlValidation="false" xmlNamespaceAware="false">
        <Context docBase="F:/workspace2/webwork/webapps/portlet/src/webapp" path="/context_path"  debug="1" reloadable="true"/>
     </Host>

 

 


//*************************************************************//

  如果你知道某个项目在哪台电脑上,也可以运行对方的项目:
      方法:
       在多写一个<host>,可同时部署多个
    如:
    <Host name="www.shouhu.com" debug="0" appBase="webapps"
       unpackWARs="true" autoDeploy="true"
       xmlValidation="false" xmlNamespaceAware="false">
       <Context docBase="F:/workspace2/webwork/webapps/portlet/src/webapp" path="/context_path"  debug="1" reloadable="true"/>
    </Host>
   
  那么这个时候在本机的地址栏中输入相应的项目名即可运行对方电脑上的项目;
 在TOMCATE加载时,会自动的加载server.xml中的<host>,自动的找到www.shouhu.com,对应的ip。接着连接上对方的项目