Java/Ant/Separated Build File
Содержание
Ant call another ant script
<source lang="java">
<project name="foo" default="deploy" basedir=".">
<target name="init"> <tstamp/> <property name="src" value="src" /> <property name="build" value="build" /> <property name="classes" value="classes" /> <property name="deploy" value="deploy" /> <property name="config" value="config" /> <property name="runDir" value="." /> <property name="local" value="local" /> <property name="remote" value="remote" /> <property name="lib" value="lib" /> </target> <target name="clean" depends="init"> <deltree dir="${classes}" /> <deltree dir="${remote}" /> <deltree dir="${deploy}" /> <deltree dir="${lib}" /> </target> <target name="prepare" depends="clean"> <mkdir dir="${classes}" /> <mkdir dir="${deploy}" /> <mkdir dir="${lib}" /> </target> <target name="compile" depends="prepare"> <javac srcdir="${src}" destdir="${classes}" /> <copyfile src="${lib}/app.jar" dest="${deploy}/app.jar" /> <copyfile src="${config}/remote.properties" dest="${runDir}\remote.properties" /> <jar jarfile="${lib}/app.jar" basedir="${classes}" /> </target> <target name="prepareDeploy" depends="compile"> <copyfile src="${lib}/app.jar" dest="${deploy}/app.jar" /> <copyfile src="${build}/remotebuild.xml" dest="${deploy}/build.xml" /> <mkdir dir="${remote}" /> </target> <target name="deploy" depends="prepareDeploy"> <ant antfile="${build}/deploy.xml" dir="." /> </target>
</project>
</source>
Ant script calls another ant script
<source lang="java">
//COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0 <project name="ajax4jsf" default="distribute">
<property name="commonLibs.dir" value="${basedir}/commonLibs"></property> <property name="distrib.dir" value="${basedir}/distribution"/> <property name="work.dir" value="${basedir}/build"/> <property name="src.root.dir" value="${basedir}/src"></property> <property name="test.src.dir" value="${src.root.dir}/testCase"></property> <property name="framework.src.dir" value="${src.root.dir}/framework"></property> <property name="framework.java.src.dir" value="${framework.src.dir}/java"></property> <property name="framework.js.src.dir" value="${framework.src.dir}/JavaScript"></property> <property name="framework.generate.src.dir" value="${framework.src.dir}/generate"></property> <property name="framework.build.dir" value="${work.dir}/framework"></property> <property name="cdk.src.dir" value="${src.root.dir}/cdk"></property> <property name="cdk.build.dir" value="${work.dir}/cdk"></property> <property name="test.src.dir" value="${src.root.dir}/test"></property> <property name="test.build.dir" value="${work.dir}/test"></property> <property name="doc.root.dir" value="${distrib.dir}/docs"></property> <property name="tlddoc.dir" value="${doc.root.dir}/tldDoc"></property> <property name="javadoc.dir" value="${doc.root.dir}/javaAPI"></property> <property name="tlddoc.jar" value="${commonLibs.dir}/tlddoc.jar"></property> <property name="framework-distribution-name" value="ajax4jsf"></property> <property name="cdk-distribution-name" value="ajax4jsf-cdk" /> <property name="test-distribution-name" value="ajax4jsf-test" /> <property name="generated-source.dir" value="${work.dir}/gen-src"></property> <fileset id="commonLibs" dir="${commonLibs.dir}"> <include name="*.jar"/> </fileset> <path id="class-path"> <fileset refid="commonLibs"></fileset> </path> <target name="prepare"> <mkdir dir="${distrib.dir}"/> <mkdir dir="${work.dir}"/> </target> <target name="generate-framework" depends="compile-framework,compile-cdk"> <ant antfile="${framework.generate.src.dir}/build.xml" inheritall="true" dir="${basedir}" target="xml-merge"> </ant> </target>
<target name="prepare-framework"> <mkdir dir="${framework.build.dir}" /> </target> <target name="prepare-cdk"> <mkdir dir="${cdk.build.dir}" /> </target> <target name="prepare-test"> <mkdir dir="${test.build.dir}" /> </target> <target name="compile-framework" depends="prepare-framework"> <javac destdir="${framework.build.dir}" compiler="javac1.4" source="1.4" target="1.4" debug="true" failonerror="false" debuglevel="lines,vars,source"> <classpath refid="class-path"></classpath> <src path="${framework.src.dir}"></src> </javac> <copy todir="${framework.build.dir}"> <fileset dir="${framework.java.src.dir}"> <include name="**/*.*"/> <exclude name="**/*.java"/> <exclude name="META-INF/*faces-config.xml"/> </fileset> </copy> </target> <target name="compile-cdk" depends="prepare-cdk"> <javac destdir="${cdk.build.dir}" compiler="javac1.5" source="1.5" target="1.5" debug="true" debuglevel="lines,vars,source"> <classpath refid="class-path"></classpath> <src path="${cdk.src.dir}"></src> </javac> <copy todir="${cdk.build.dir}/META-INF"> <fileset dir="${cdk.src.dir}/META-INF"> <include name="**/*.*"/> </fileset> </copy> </target> <target name="compile-test" depends="prepare-test"> <javac destdir="${test.build.dir}" compiler="javac1.4" source="1.4" target="1.4" debug="true" debuglevel="lines,vars,source" includes="org/ajax4jsf/tests/*.*" > <classpath refid="class-path"></classpath> <classpath path="${framework.build.dir}"></classpath> <src path="${test.src.dir}"> </src> </javac> <copy todir="${test.build.dir}/META-INF"> <fileset dir="${test.src.dir}/META-INF"> <include name="**/*.*"/> </fileset> </copy> <copy todir="${test.build.dir}/WEB-INF"> <fileset dir="${test.src.dir}/WEB-INF"> <include name="**/*.*"/> </fileset> </copy> </target> <target name="compile-framework-phase2" depends="generate-framework"> <javac destdir="${framework.build.dir}" compiler="javac1.4" source="1.4" target="1.4" debug="true" debuglevel="lines,vars,source"> <classpath> <path> <path refid="class-path"></path> <pathelement path="${framework.build.dir}"/> </path> </classpath> <src path="${generated-source.dir}"></src> </javac> <copy todir="${framework.build.dir}"> <fileset dir="${generated-source.dir}"> <include name="**/*.*"/> <exclude name="**/*.java"/> <exclude name="META-INF/*-faces-config.xml"/> </fileset> </copy> </target> <target name="assemble-javascripts" depends="prepare-framework"> <ant antfile="${framework.js.src.dir}/build.xml" dir="${framework.js.src.dir}"> <property name="target-dir" value="${framework.build.dir}"></property> </ant> </target> <target name="build-framework" depends="compile-framework,assemble-javascripts,compile-framework-phase2"> </target> <target name="build-cdk" depends="compile-cdk"> </target> <target name="build-test" depends="compile-test"> </target> <target name="prepare-distribution-dir"> <mkdir dir="${distrib.dir}/lib"/> </target> <target name="package-framework" depends="build-framework, prepare-distribution-dir"> <jar destfile="${distrib.dir}/lib/${framework-distribution-name}.jar"> <fileset dir="${framework.build.dir}"> <include name="**/*.*"/> </fileset> </jar> </target> <target name="package-cdk" depends="prepare-distribution-dir,build-cdk"> <jar destfile="${distrib.dir}/lib/${cdk-distribution-name}.jar"> <fileset dir="${cdk.build.dir}"> <include name="**/*.*"/> </fileset> </jar> </target> <target name="package-test" depends="prepare-distribution-dir,build-test"> <jar destfile="${distrib.dir}/lib/${test-distribution-name}.jar"> <fileset dir="${test.build.dir}"> <include name="**/*.*"/> </fileset> </jar> </target> <target name="prepare-docs"> <mkdir dir="${doc.root.dir}"/> </target> <target name="docs" depends="prepare-docs, javadoc, tlddoc, copy-doc"> </target> <target name="copy-doc" depends="prepare-docs"> <copy todir="${doc.root.dir}" failonerror="false"> <fileset dir="${basedir}/doc"> <include name="**/*"/> </fileset> </copy> </target> <target name="javadoc"> <mkdir dir="${javadoc.dir}"/> <javadoc packagenames="org.*" destdir="${javadoc.dir}"> <sourcepath> <pathelement path="${framework.java.src.dir}"/> </sourcepath> <classpath refid="class-path"></classpath> </javadoc> </target> <target name="tlddoc"> <mkdir dir="${tlddoc.dir}"/> <echo message="${tlddoc.jar}"></echo> <apply executable="java" parallel="true" verbose="true"> <arg line="-jar ${tlddoc.jar}"/> <arg line="-doctitle "AJAX4JSF""/> <arg line="-d ${tlddoc.dir}"/> <fileset dir="${framework.build.dir}/META-INF" includes="**.tld"/> </apply> </target> <target name="copy-dependencies" depends="prepare-distribution-dir"> <copy todir="${distrib.dir}/lib"> <fileset dir="${commonLibs.dir}"> <include name="oscache*.jar"/> </fileset> </copy> </target> <target name="copy-src"> <mkdir dir="${distrib.dir}/src"/> <copy todir="${distrib.dir}/src"> <fileset dir="${framework.src.dir}"> <include name="**/*"/> </fileset> </copy> </target> <target name="copy-demo"> <mkdir dir="${distrib.dir}/demo"/> <copy todir="${distrib.dir}/demo" failonerror="false"> <fileset dir="${basedir}/demo"> <include name="**/*"/> </fileset> </copy> </target> <target name="copy-txt"> <copy todir="${distrib.dir}"> <fileset dir="${basedir}"> <include name="*.txt"/> <include name="*.TXT"/> </fileset> </copy> </target> <target name="build-test-framework"> <javac destdir="${framework.build.dir}" compiler="javac1.4" source="1.4" target="1.4" debug="true" debuglevel="lines,vars,source"> <classpath refid="class-path"></classpath> <src path="${test.src.dir}"></src> </javac> <copy todir="${framework.build.dir}"> <fileset dir="${test.src.dir}"> <include name="**/*.*"/> <exclude name="**/*.java"/> <exclude name="META-INF/*"/> <exclude name="WEB-INF/*"/> </fileset> </copy> </target> <target name="distribute" depends="clean,build-framework,package-framework, copy-dependencies, docs, copy-src, copy-txt, copy-demo, package-cdk, package-test"> <delete file="${basedir}/velocity.log" failonerror="false" verbose="true"></delete> </target> <target name="clean"> <delete dir="${work.dir}"></delete> <delete includeemptydirs="true" failonerror="false"> <fileset dir="${distrib.dir}"> <include name="**/*"/> </fileset> </delete> </target>
</project>
</source>
One ant script calls another antscript and dir setting
<source lang="java">
<project name="ACT" basedir="." default="build">
<property name="prototype" value="prototype"/> <property name="main" value="main"/> <target name="build" description="Building all ACT projects"> <ant antfile="build.xml" dir="${prototype}"/> </target>
</project>
</source>
Use main ant build file call sub build file
<source lang="java">
<?xml version="1.0"?> <project name="Example Application Build" default="build-both" basedir=".">
<property file="build.properties"/> <path id="build.classpath"> <pathelement location="${servlet24.jar}"/> <pathelement location="${jsp20.jar}"/> <pathelement location="${mysql.jar}"/> <pathelement path="${appName.jar}"/> </path> <path id="test.classpath"> <path refid="build.classpath"/> <fileset dir="${httpunit.home}/jars"> <include name="*.jar"/> <exclude name="junit.jar"/> </fileset> <pathelement location="${junit.jar}"/> <pathelement location="${httpunit.jar}"/> <pathelement location="${checkstyle.jar}"/> <pathelement location="${test.build}"/> </path> <fileset id="javadoc" dir="${src}"> <exclude name="*/conf/**"/> <exclude name="*/docs/*"/>
<exclude name="**/package.html"/> <exclude name="**/*.xml"/> <include name="shared/**"/> <include name="stand-alone/**"/> <include name="web/java/**"/> </fileset> <property name="docs.all.dir" value="${build}"/> <patternset id="docs.all"> <include name="docs/**"/> </patternset> <property name="docs.misc.dir" value="${src.shared.docs}"/> <patternset id="docs.misc"> <include name="README"/> <include name="LICENSE"/> </patternset> <property name="src.files.dir" value="."/> <patternset id="src.files"> <include name="${src}/**"/> <include name="build.*"/> </patternset> <patternset id="bin.jar"> <include name="*.jar"/> </patternset> <patternset id="bin.war"> <include name="*.war"/> </patternset> <taskdef name="deploy" classname="org.apache.catalina.ant.DeployTask"/> <taskdef name="undeploy" classname="org.apache.catalina.ant.UndeployTask"/> <target name="dir" description="Create the working directories"> <echo message="Creating the working directories"/> <mkdir dir="${build.stand-alone.root}"/> <mkdir dir="${build.web.classes}"/> <mkdir dir="${dist}"/> <mkdir dir="${lib}"/> </target> <target name="download-servlet-jar" depends="dir" description="Download the servlet JAR"> <echo message="Downloading the servlet JAR"/> <get src="http://www.ibiblio.org/maven/servletapi/jars/servletapi-2.4.jar" dest="${servlet24.jar}" verbose="true"/> </target> <target name="package-stand-alone" depends="dir" description="Compile stand-alone application"> <echo message="Compiling the stand-alone application"/> <property name="destination" value="${build.stand-alone.root}"/> <ant antfile="${build.shared.xml}" inheritRefs="true"/> <ant antfile="${build.stand-alone.xml}" inheritRefs="true"/> </target> <target name="stand-alone-complete" depends="dir" description="Compile stand-alone application, using CVS version of the MySQL connector"> <echo message="Compiling stand-alone application, using CVS versions of the MySQL connector"/> <ant antfile="${build.mysql.xml}" inheritRefs="true"/> <antcall target="package-stand-alone"/> </target> <target name="set-argument-defaults" description="Set the defaults for the command-line arguments"> <echo message="Setting the defaults for the command-line arguments"/> <condition property="arg0" value=""> <not> <isset property="${arg0}"/> </not> </condition> <condition property="arg1" value=""> <not> <isset property="${arg1}"/> </not> </condition> </target> <target name="run-stand-alone-java" depends="set-argument-defaults, package-stand-alone" description="Run the stand-alone application"> <echo message="Running the stand-alone application"/> <property name="results.file" value="results.txt"/> <tstamp> <format property="TSTAMP" pattern="HHmm"/> </tstamp> <java classname="org.mwrm.plants.client.PlantClient"> <arg value="${arg0}"/> <arg value="${arg1}"/> <classpath refid="build.classpath"/> <redirector output="${DSTAMP}-${TSTAMP}-${results.file}"/> </java> </target> <target name="package-web" depends="dir" description="Build the WAR file in one step"> <echo message="Building the WAR file in one step"/> <property name="destination" value="${build.stand-alone.root}"/> <ant antfile="${build.shared.xml}" inheritRefs="true"/> <ant antfile="${build.web.xml}" inheritRefs="true"/> </target> <target name="web-complete" description="Compile web application, using CVS versions of the MySQL connector and the JSTL"> <echo message="Compiled web application, using CVS versions of the MySQL connector and the JSTL"/> <ant antfile="${build.mysql.xml}" inheritRefs="true"/> <ant antfile="${build.jstl.xml}" inheritRefs="true"/> <antcall target="package-web"/> </target> <target name="check-port" description="Check whether Tomcat is running"> <echo message="Checking whether Tomcat is running"/> <condition property="tomcat.running"> <socket server="${tomcat.host}" port="${tomcat.port}"/> </condition> </target> <target name="start-tomcat" depends="check-port" description="Start Tomcat if it isn"t running" unless="tomcat.running"> <echo message="Starting Tomcat"/> <property environment="env"/> <exec executable="${env.CATALINA_HOME}/bin/${tomcat.executableName}" spawn="true" vmlauncher="false"/> <sleep seconds="10"/> </target>
<target name="build-both" depends="package-stand-alone, package-web" description="Compile both applications, without CVS versions of the MySQL connector and the JSTL"> <echo message="Compiled both applications, without CVS versions of the MySQL connector and the JSTL"/> </target> <target name="build-all" depends="stand-alone-complete, web-complete" description="Compile both applications, using CVS versions of the MySQL connector and the JSTL"> <echo message="Compiled both applications, using CVS versions of the MySQL connector and the JSTL"/> </target> <target name="clean" description="Clean up the working directories"> <echo message="Cleaning up"/> <delete dir="${build}"/> </target> <target name="zip-all" depends="package-stand-alone, package-web" description="Zip the binary and source distributions"> <echo message="Zipping the binary and source distributions"/> <ant antfile="${build.package.xml}" target="zip-all" inheritRefs="true"/> </target> <target name="tar-all" depends="package-stand-alone, package-web" description="Tar the binary and source distributions"> <echo message="Tarring the binary and source distributions"/> <ant antfile="${build.package.xml}" target="tar-all" inheritRefs="true"/> </target> <target name="package-all" depends="package-stand-alone, package-web" description="Create all the packages"> <echo message="Creating all the packages"/> <ant antfile="${build.package.xml}" inheritRefs="true"/> </target> <target name="ftp-docs" depends="package-all" description="Place the documentation on FTP"> <echo message="Placing the documentation on FTP"/> <ftp server="${ftp.server}" userid="${ftp.user}" password="${ftp.password}" remotedir="${ftp.src.dir}" action="send" newer="true" timediffauto="true"> <fileset dir="${dist}"> <include name="${appName}-${package.docs}.*"/> </fileset> </ftp> </target> <target name="ftp-src" depends="package-all" description="Place the source code on FTP"> <echo message="Placing the source code on FTP"/> <ftp server="${ftp.server}" userid="${ftp.user}" password="${ftp.password}" remotedir="${ftp.src.dir}" action="send" newer="true" timediffauto="true"> <fileset dir="${dist}"> <include name="${appName}-src.*"/> </fileset> </ftp> </target> <target name="ftp-bin" depends="package-all" description="Place the binaries on FTP"> <echo message="Placing the binaries on FTP"/> <ftp server="${ftp.server}" userid="${ftp.user}" password="${ftp.password}" remotedir="${ftp.bin.dir}" action="send" newer="true" timediffauto="true"> <fileset dir="${dist}"> <include name="${appName}*bin*"/> </fileset> </ftp> </target> <target name="ftp" description="Place everything on FTP"> <echo message="Placing everything on FTP"/> <input message="Please enter your username." addproperty="ftp.user"/> <input message="Please enter your password." addproperty="ftp.password"/> <splash showduration="0"/> <antcall target="ftp-docs"/> <antcall target="ftp-src"/> <antcall target="ftp-bin"/> </target> <target name="email-docs" depends="package-all" description="Email the documentation"> <echo message="Emailing the documentation"/> <mail from="${mail.from}" tolist="${mail.tolist}" message="${mail.message.docs}" mailhost="${mail.mailhost}" user="${mail.user}" password="${mail.password}" subject="${mail.subject}"> <fileset dir="${dist}"> <include name="${appName}-${package.docs}.*"/> </fileset> </mail> </target> <target name="email-src" depends="package-all" description="Email the source"> <echo message="Emailing the source"/> <mail from="${mail.from}" tolist="${mail.tolist}" message="${mail.message.src}" mailhost="${mail.mailhost}" user="${mail.user}" password="${mail.password}" subject="${mail.subject}"> <fileset dir="${dist}"> <include name="${appName}-src.*"/> </fileset> </mail> </target> <target name="email-bin" depends="package-all" description="Email the binaries"> <echo message="Emailing the binaries"/> <mail from="${mail.from}" tolist="${mail.tolist}" message="${mail.message.bin}" mailhost="${mail.mailhost}" user="${mail.user}" password="${mail.password}" subject="${mail.subject}"> <fileset dir="${dist}"> <include name="${appName}*bin*"/> </fileset> </mail> </target> <target name="email" description="Email everything"> <echo message="Emailing everything"/> <antcall target="email-docs"/> <antcall target="email-src"/> <antcall target="email-bin"/> </target>
<target name="deploy-copy-war" depends="package-web, start-tomcat" description="Deploy the WAR by copying it to Tomcat"> <echo message="Copying the WAR to CATALINA_HOME"/> <property environment="env"/> <copy file="${appName.war}" todir="${env.CATALINA_HOME}/webapps"/> </target>
<target name="deploy" depends="package-web, start-tomcat" description="Hot deploy the application"> <echo message="Deploying the WAR to Tomcat"/> <deploy url="${manager.url}" username="${manager.user}" password="${manager.password}" path="/${appName}" war="file:${appName.war}" update="true"/> </target> <target name="undeploy" description="Undeploy the application"> <echo message="Undeploying the WAR"/> <undeploy url="${manager.url}" username="${manager.user}" password="${manager.password}" path="/${appName}"/> </target> <target name="database" description="Prepare the database by creating it and inserting data"> <echo message="Preparing the database by creating it and inserting data"/> <property file="${database.properties}"/> <sql driver="${driver.name}" url="${database.root}" userid="${database.user}" password="${database.password}"> <classpath refid="build.classpath"/> <transaction src="${src.shared.conf}/${drop.sql}"/> <transaction src="${src.shared.conf}/${create.sql}"/> <transaction src="${src.shared.conf}/${insert.sql}"/> </sql> </target> <target name="database-drop" description="Prepare the database by creating it and inserting data"> <echo message="Preparing the database by creating it and inserting data"/> <property file="${database.properties}"/> <sql driver="${driver.name}" url="${database.root}" userid="${database.user}" password="${database.password}"> <classpath refid="build.classpath"/> <transaction src="${src.shared.conf}/${drop.sql}"/> </sql> </target> <target name="test" depends="package-stand-alone, deploy-copy-war" description="Run the JUnit tests"> <echo message="Running the JUnit tests"/> <ant antfile="${build.test.xml}" target="test" inheritRefs="true"/> </target> <target name="coding-style" description="Check the coding conventions"> <echo message="Checking the coding conventions"/> <ant antfile="${build.test.xml}" target="coding-style" inheritRefs="true"/> </target> <target name="test-all" depends="package-stand-alone, deploy-copy-war" description="Run all the tests"> <echo message="Running all the tests"/> <ant antfile="${build.test.xml}" inheritRefs="true"/> </target> <target name="test-target" depends="package-stand-alone,deploy-copy-war"> <ant antfile="${build.test.xml}" target="${target}" inheritRefs="true"/> </target> <target name="stand-alone-target" depends="dir"> <ant antfile="${build.stand-alone.xml}" target="${target}" inheritRefs="true"/> </target> <target name="web-target" depends="dir"> <ant antfile="${build.web.xml}" target="${target}" inheritRefs="true"/> </target >
</project>
</source>