BuildDesEncryptor.xml
- <project name="DesEncryptor" default="execute.class" basedir=".">
- <echo message="1.define variables~" />
- <!-- 名称、版本、作者 -->
- <property name="name" value="DesEncryptor" />
- <property name="version" value="1.0" />
- <property name="author" value="yang3wei" />
- <property name="libs" value="${basedir}/libs" />
- <property name="desktop" value="/Users/user/Desktop" />
- <property name="src" value="${basedir}/src_des" />
- <property name="build" value="${basedir}/build_des" />
- <property name="res_package" value="org/bruce/des/encryptor/resources" />
- <property name="src.res" value="${src}/${res_package}" />
- <property name="build.res" value="${build}/${res_package}" />
- <!-- jar包必要参数 -->
- <property name="dist" value="${basedir}/dist_des" />
- <property name="dist.jar.name" value="${name}${version}.jar" />
- <property name="dist.jar.path" value="${dist}/${dis.jar.name}" />
- <property name="entry.class" value="org.bruce.des.encryptor.entry.DesEncryptorView" />
- <echo message="2.define external.jars.path" />
- <path id="external.jars.path">
- <fileset dir="${libs}">
- <include name="**/*.jar" />
- </fileset>
- </path>
- <target name="prepare">
- <echo message="3.prepare" />
- <mkdir dir="${build}" />
- <mkdir dir="${dist}" />
- </target>
- <target name="compile" depends="prepare">
- <echo message="5.compile" />
- <!-- 将src目录下的资源文件复制到 build目录下面(保留包结构) -->
- <delete dir="${build.res}" />
- <copy todir="${build.res}">
- <fileset dir="${src.res}">
- <include name="**/*.jpg" />
- <include name="**/*.png" />
- <include name="**/*.gif" />
- <include name="**/*.prop" />
- <include name="**/*.properties" />
- <exclude name="**/*.torrent" />
- </fileset>
- </copy>
- <javac srcdir="${src}" destdir="${build}" encoding="UTF-8" deprecation="true" listfiles="off" fork="true" target="1.6" debug="false" failonerror="false">
- <!--给编译器指定编码,防止出现:"警告: 编码 GBK 的不可映射字符"-->
- <compilerarg line="-encoding UTF-8 " />
- <classpath refid="external.jars.path" />
- </javac>
- <echo message="compile finished!" />
- </target>
- <target name="execute.class" depends="compile">
- <echo message="${name}.execute" />
- <java classname="${entry.class}" classpath="${build}" fork="true">
- <sysproperty key="file.encoding" value="UTF-8" />
- <classpath refid="external.jars.path" />
- </java>
- </target>
- <target name="package2jar" depends="compile">
- <echo message="${name}.package2jar" />
- <jar destfile="dist.jar.path" basedir="${build}">
- <manifest>
- <attribute name="Created-By" value="${author}" />
- <attribute name="Main-Class" value="${entry.class}" />
- </manifest>
- <zipfileset excludes="META-INF/*.SF" src="./libs/IOUtils.jar" />
- </jar>
- </target>
- <target name="execute.jar" depends="package2jar">
- <echo message="${name}.jar.execute" />
- <java fork="true" failonerror="true" jar="dist.jar.path">
- <sysproperty key="file.encoding" value="UTF-8" />
- </java>
- </target>
- <target name="copy2desktop" depends="package2jar">
- <echo message="${name}.copy2desktop" />
- <copy file="dist.jar.path" tofile="${desktop}/${dist.jar.name}" />
- </target>
- <target name="clean">
- <delete dir="${build}" />
- <delete file="dist.jar.path" />
- <delete dir="${dist}" />
- </target>
- <target name="rerun" depends="clean">
- <ant antfile="BuildDesEncryptor.xml" target="execute.class" />
- </target>
- </project>
要注意的是,本次我将ant 默认的 build.xml 文件名改成了 BuildDesEncryptor.xml
该操作会导致一个问题:
在执行 rerun 这个 target 的时候,竟然报出错误说找不到 build.xml 文件
很显然是因为我没有使用默认的文件名称所致。
<ant antfile="BuildDesEncryptor.xml" target="execute.class" />
上面这行我之前是按如下的方式写的
<ant target="execute.class" />
可见:
如果构建文件没有使用默认的 build.xml 作为文件名的话
ant 标签里最好还是给写上 antfile 这个属性。