众所周知,ant是java部署开发工具,以前看过的一些使用方法都是有ant安装文件的,今天看到rickard oberg(Jboss 构架师)使用的 ant包使用方法,特地拿出来分享一下。
1 开发目录结构:
src目录:
main目录:存放可执行文件,注意,文件全部按包结构位置,放置到位
lib目录:存放jar文件,例如rmi应用中经常用到的:Dynaserver.jar(简易web服务器);jndi.jar(jndi库);rmiregistry.jar(rmi库);providerutil.jar(jndi库)
resources:存放一个配置文件,例如:jndi.properties;system.properties;最后一起放到包里
etc目录:存放配置文件。例如manifest.mf,主要存放jar文件的配置文件
build目录:存放build.xml,build.bat
lib目录:存放ant包,例如:jaxp.jar;ant.jar;parser.jar
dist目录:结果包,最后打好的包,存放在这里
build目录:用来存放,为了打包而存在的中间代码文件等
bin目录:存放启动用,批处理文件,例如,启动服务器,启动客户的批处理
2 build.xml结构
<?xml version="1.0"?>
<!-- ======================================================================= -->
<!-- HelloApplet build file -->
<!-- ======================================================================= -->
<project name="HelloApplet" default="main" basedir="../..">
<path id="project.class.path">
<fileset dir="${basedir}/lib"/>
<fileset dir="${basedir}/src/lib"/>
<pathelement location="${basedir}/build/classes" />
</path>
<target name="init">
<property name="Name" value="HelloApplet"/>
<property name="name" value="helloapplet"/>
<property name="version" value="1.0"/>
<property name="lib.dir" value="${basedir}/lib"/>
<property name="src.dir" value="${basedir}/src"/>
<property name="build.dir" value="${basedir}/build"/>
<property name="dist.dir" value="${basedir}/dist"/>
</target>
<!-- =================================================================== -->
<!-- Prepares the directories -->
<!-- =================================================================== -->
<target name="prepare" depends="init">
<mkdir dir="${build.dir}"/>
<mkdir dir="${build.dir}/classes"/>
<mkdir dir="${build.dir}/lib"/>
<mkdir dir="${build.dir}/bin"/>
<mkdir dir="${dist.dir}"/>
<mkdir dir="${dist.dir}/lib"/>
<mkdir dir="${dist.dir}/src"/>
</target>
<!-- =================================================================== -->
<!-- Compiles the source code -->
<!-- =================================================================== -->
<target name="compile" depends="prepare">
<javac srcdir="${src.dir}/main"
destdir="${build.dir}/classes"
debug="on"
deprecation="off"
optimize="off">
<classpath refid="project.class.path" />
</javac>
<rmic base="${build.dir}/classes"
classname="masteringrmi.helloapplet.server.HelloWorldImpl"
stubVersion="1.2">
<classpath refid="project.class.path" />
</rmic>
</target>
<!-- =================================================================== -->
<!-- Creates the jar archives -->
<!-- =================================================================== -->
<target name="jar" depends="compile">
<copy todir="${build.dir}/classes">
<fileset dir="${src.dir}/resources"/>
</copy>
<jar jarfile="${build.dir}/bin/server.jar"
basedir="${build.dir}/classes"
manifest="${src.dir}/etc/server.mf"
/>
</target>
<!-- =================================================================== -->
<!-- Creates the binary structure -->
<!-- =================================================================== -->
<target name="main" depends="jar">
<copy todir="${dist.dir}/lib">
<fileset dir="${src.dir}/lib"/>
</copy>
</target>
关于以上ant文件的含义,可以参考网上的一些资料
3build.bat内容
@echo off
rem ----- Verify and Set Required Environment Variables -----------------------
if not "%JAVA_HOME%" == "" goto gotJavaHome
echo You must set JAVA_HOME to point at your Java Development Kit distribution
goto cleanup
:gotJavaHome
set _CP_=..\..\lib\ant.jar
set _CP_=%_CP_%;..\..\lib\parser.jar
set _CP_=%_CP_%;..\..\lib\jaxp.jar
%JAVA_HOME%\bin\java.exe -classpath "%JAVA_HOME%\lib\tools.jar;%_CP_%;%CLASSPATH%" org.apache.tools.ant.Main %1 %2 %3 %4 %5
:cleanup
4system.properties/jndi.properties
都是运行时配置文件,其中jndi.properties会自动加载,内容如:
java.naming.factory.initial=com.sun.jndi.rmi.registry.RegistryContextFactory
java.naming.provider.url=rmi://localhost
system.properties需通过程序调用
System.getProperties().load(
Thread.currentThread().
getContextClassLoader().
getResourceAsStream("system.properties"));