对于只有2G内存的电脑,Eclipse显然有些吃不消,于是我便想如何在不使用IDE的情况下,直接编译J2EE项目,最终我选择使用Ant。Ant是什么?Apache Ant是一个将软件编译、测试、部署等步骤联系在一起加以自动化的一个工具,大多用于Java环境中的软件开发。
我默认已安装了Ant(安装方式附后)
一个标准的j2ee项目目录为(项目名为:rt_gauge):
方式1:仅仅需要tomcat外部运行j2ee项目。那么仅仅只需要将项目编译一下即可(将bulid.xml放置在项目文件下,其余参看build.xml注释):
bulid.xml
<?xml version="1.0" encoding="UTF-8"?> <project basedir="." default="pub" name="rt_gauge"> <target name="init" description="设置编译 需要的路径变量"> <property name="name" value="rt_gauge"/> <property name="src" value="${basedir}/src"/> <property name="webapp" value="${basedir}/WebContent"/> <property name="classes" value="${basedir}/WebContent/WEB-INF/classes"/> <property name="tomcatlibs" value="D:/tomcat7.0.42/lib"/> <property name="lib" value="${basedir}/WebContent/WEB-INF/lib"/> <path id="classpath"> <fileset dir="${lib}"> <include name="**/*.jar"/> </fileset> <fileset dir="${tomcatlibs}"> <include name="**/*.jar"/> </fileset> </path> </target> <target name="prepare" depends="init" description="创建class路径"> <delete dir="${classes}"/> <mkdir dir="${classes}"/> </target> <target name="build" depends="prepare" description="编译 java 文件"> <javac encoding="utf-8" srcdir="${src}" destdir="${classes}" includeantruntime="false"> <classpath refid="classpath"/> </javac> </target> <target name="pub" depends="build" description="将*.xml-*.properties文件拷入相应classes目录"> <copy todir="${classes}"> <fileset dir="${src}"> <include name="*.xml"/> </fileset> </copy> <copy todir="${classes}"> <fileset dir="${src}"> <include name="*.properties"/> </fileset> </copy> </target> </project>
然后在cmd命令行中切换到bulid.xml目录下,执行ant命令即可,如下:
小提示:可以将命令写成批处理rt_gauge.bat方便以后直接调用:
cd /D E:\Workspace\EclipseWorkspace\rt_gauge cmd /k Ant
成功!
方式2:直接需要将项目打成*.war包。那么首先需要将项目编译一下,然后打包整个项目(我在此将*.war打包到tomcat/webapps目录下)
build.xml
<?xml version="1.0" encoding="UTF-8"?> <project basedir="." default="antwar" name="rt_gauge"> <target name="init" description="设置初始化打war包需要的路径变量"> <property name="name" value="rt_gauge"/> <property name="src" value="${basedir}/src"/> <property name="webapp" value="${basedir}/WebContent"/> <property name="classes" value="${basedir}/WebContent/WEB-INF/classes/"/> <property name="tomcatlibs" value="D:/tomcat7.0.42/lib"/> <property name="lib" value="${basedir}/WebContent/WEB-INF/lib"/> <property name="war.pro" value="D:\tomcat7.0.42\webapps\rt_gauge.war"/> <path id="classpath"> <fileset dir="${lib}"> <include name="**/*.jar"/> </fileset> <fileset dir="${tomcatlibs}"> <include name="**/*.jar"/> </fileset> </path> </target> <target name="prepare" depends="init" description="创建class路径"> <delete dir="${classes}"/> <mkdir dir="${classes}"/> </target> <target name="build" depends="prepare" description="编译 java 文件"> <javac encoding="utf-8" srcdir="${src}" destdir="${classes}" includeantruntime="false"> <classpath refid="classpath"/> </javac> </target> <target name="pub" depends="build" description="将*.xml-*.properties文件拷入相应classes目录"> <copy todir="${classes}"> <fileset dir="${src}"> <include name="*.xml"/> </fileset> </copy> <copy todir="${classes}"> <fileset dir="${src}"> <include name="*.properties"/> </fileset> </copy> </target> <target name="antwar" depends="pub" description="打 war 包"> <delete dir="${war.pro}"/> <war warfile="${war.pro}" webxml="${webapp}/WEB-INF/web.xml"> <lib dir="${tomcatlibs}"/> <lib dir="${lib}"/> <fileset dir="${webapp}"/> </war> </target> </project>
同上命令:
成功!
附Ant安装:
1、 在官网下载Ant最新版,下载地址为:http://ant.apache.org/
2 、修改环境变量 path D:\ant1.9.3\bin;(Ant解压路径)
3 、cmd ant命令是否可行
4、OK