How to work with Ant

<?xml version="1.0" encoding="UTF-8"?> <project name="AntDemoProject" default="dist" basedir="."> <!-- 【定义变量】set global properties for this build --> <property name="src" location="src" /> <property name="build" location="build" /> <property name="dist" location="dist" /> <!-- 【加载属性文件】 --> <property file="build.properties" /> <echo>${container.deploy.dir}</echo> <target name="init"> <!-- Create the time stamp --> <tstamp /> <!-- Create the build directory structure used by compile --> <mkdir dir="${build}" /> </target> <!-- 【编译】Compile the java code from ${src} into ${build} --> <target name="compile" depends="init"> <javac srcdir="${src}" destdir="${build}" /> </target> <!-- 【打包Jar】 --> <target name="dist" depends="compile"> <!-- Create the distribution directory --> <mkdir dir="${dist}/lib" /> <!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file --> <jar jarfile="${dist}/lib/MyProject-${DSTAMP}.jar" basedir="${build}" /> </target> <!-- 【清空】Delete the ${build} and ${dist} directory trees --> <target name="clean"> <delete dir="${build}" /> <delete dir="${dist}" /> </target> <!-- 【运行】 --> <target name="run" depends="compile"> <java classpath="${build}" classname="HelloJFrame" fork="true"> </java> </target> </project><!-- ================================================================================================================= 1、安装和配置Ant ①Download Ant binary distribution from:http://ant.apache.org/bindownload.cgi 从Apache下载它的zip包,解压到任何本地磁盘上 ②Set ANT_HOME to where you installed Ant 设置ANT_HOME环境变量,也就是解压后存放的目录 ③Include $ANT_HOME/bin in PATH 在环境变量中更新Path值,加上%ANT_HOME%/bin ④Make sure JAVA_HOME is set to point to JDK 检查JDK的目录,确定JAVA_HOME已加到环境变量中 ================================================================================================================= 2、build.xml 使用Ant的通常做法是在项目中建立一个XML文件,名字通常取build.xml。当然也可以取任何喜欢的名字 每一个build.xml只能有一个<project>标签,每个<project>标签中可以包含若干<tartget>标签 每一个target可以独立执行,或依赖于其他target执行完毕才能执行 ①<project>标签含以下属性 name=====项目名称 default==当没有指定target时使用的缺省target basedir==用于计算所有其他路径的基路径 ②<target>标签含以下属性 name=========target的名字 depends======用逗号分隔的target的名字列表,也就是依赖表 if===========执行target所需要设定的属性名 unless=======执行target需要清除设定的属性名 description==关于target功能的简短描述 ③举例: <target name="A"/> <target name="B" depends="A"/> <target name="C" depends="B"/> <target name="D" depends="C,B,A"/> 其中target A可以独立执行,但是B、C、D则依赖于其他target才可以执行 也就是说,执行target D时,Ant就会按照A__B__C这样的顺序先执行其他target 从依赖属性来看,你可能认为先执行C,然后B,最后A。错了!C依赖于B,B依赖于A,所以先执行A,然后B,然后C,最后D才会被执行 并且一个target只能被执行一次,即时有多个target依赖于它 而且还要确保初始化target总是出现在其他target依赖表中的第一个target。初始化target的名字通常是init。 ================================================================================================================= 3、定义变量 ①如果想使用定义变量,可以在<project>标签下定义<property>标签,如<property name="dist" location="dist" /> 这样在引用的时候就可以使用${dist}这样的变量了,省得自己去修改每一处需要用到变量的地方 ②另外,还可以使用一个外置的属性文件:build.properties,以name=value方式定义变量 然后在build.xml中添加<property file="build.properties" />引用这个文件 ================================================================================================================= 4、创建目录 <target name="init"> <mkdir dir="${dist.dir}" /> <mkdir dir="${dist.classes.dir}" /> </target> ①这里的dist.dir等用${}括起来的,是我们定义的变量 ②创建一个目录,如果他的父目录不存在,也会被同时创建 <mkdir dir="build/classes"/> 说明:如果build目录不存在,也会被同时创建 ================================================================================================================= --><!-- ================================================================================================================= 5、编译Java文件 <target name="compile"> <javac srcdir="${src.dir}" destdir="${dist.classes.dir}" debug ="true" encoding="GBK"> <classpath refid="classpath" /> </javac> <jar destfile="${dist.classes.dir}/lib/app.jar" basedir= "${dist. classes.dir}"/> </target> 这里的<javac>标签中有srcdir、destdir、debug、encoding等属性,还有一个classpath的子标签 srcdir==========即目标source,需要编译的源文件 destdir=========就是目的地,编译出来的class的存放地 debug===========指明source是不是需要把debug信息编译进去。如果不加这个参数,等于在命令行后面加上-g:none参数 encoding========指明以何种编码方式编码source文件。对于有中文文字的代码来说这项比较重要 classpath子标签==指明需要应用的jar包,或其他class文件的所在地。这也是非常重要的一个选项 classpath子标签的使用方式有以下两种: ①作为classpath引用预先定义的jar包位置,refid指明了一个引用变量 <property name="lib.dir" value="${basedir}/lib" /> <path id="classpath"> <fileset dir="${lib.dir}"> <include name="*.jar"/> </fileset> </path> <classpath refid="classpath" /> ②简单的使用是这样的:<javac srcdir="${src}" destdir="${build}" classpath="xyz.jar" debug="on" /> ③<javac srcdir="${src}:${src2}" destdir="${build}" includes="mypackage/p1/**,mypackage/p2/**" excludes="mypackage/p1/testpackage/**" classpath="xyz.jar" debug="on"/> 表示:编译${src}和${src2}目录及其子目录下的所有Java文件 但是package/p1/**,mypackage/p2/**将被编译,而mypackage/p1/testpackage/**将不会被编译 Class文件将放在${build}指定的目录下 classpath表示需要用到的类文件或者目录 debug设置为on表示输出debug信息 ================================================================================================================= 6、打Jar包 <jar destfile="${dist}/lib/app.jar" basedir="${dist.classes.dir}"/> 这个就是把编译好的文件打成Jar包的Ant脚本,和上面的javac一样,可以放在任意位置 destfile==该属性用来指明所要打成的包 basedir===该属性用来指明目标class文件 ①<jar destfile="${dist}/lib/app.jar"> <fileset dir="${build}/classes" excludes="**/Test.class" /> <fileset dir="${src}/resources"/> </jar> 上面这段脚本很容易理解,就是除了Test.class以外,把一个source的resource目录,连同编译后的class脚本一起打进app.jar包内 ②<jar destfile="${dist}/lib/app.jar" basedir="${build}/classes" includes="mypackage/test/**" excludes="**/Test.class" manifest=”my.mf”/> manifest==这个属性用来指定自己的META-INF/MANIFEST.MF文件,而不是由系统生成 ③创建ZIP文件<zip destfile="output.zip" basedir="output"/> ④使用GZip压缩文件<gzip src="output.tar" mce_src="output.tar" zipfile="output.tar.gz"/> ⑤解压缩和提取文件<unzip src="output.tar.gz" mce_src="output.tar.gz" dest="extractDir"/> ⑥打Ear包<ear destfile="build/myapp.ear" appxml="src/metadata/application.xml"> <fileset dir="build" includes="*.jar,*.war"/> </ear> ================================================================================================================= --><!-- ================================================================================================================= 7、复制文件 <copy todir="${dist.webapps.dir}/WEB-INF/lib" overwrite="true" flatten="true"> <fileset dir="${lib.dir}"> <include name="*.jar" /> <exclude name="j2ee.jar" /> </fileset> </copy> todir======指定需要拷贝的地点 overwrite==是否需要覆盖 【flatten====是否忽略目的目录结构,不管是什么目录,直接拷贝文件到目的地,丢弃其所在结构】 ================================================================================================================= 8、其他拷贝 ①单个文件的拷贝<copy file="myfile.txt" tofile="mycopy.txt"/>或者 <copyfile src="test.java" mce_src="test.java" dest="subdir/test.java"/> ②文件到目录拷贝<copy file="myfile.txt" todir="../some/other/dir"/> ③目录到目录拷贝<copy todir="../new/dir"> <fileset dir="src_dir"/> </copy> ④拷贝一批文件到指定目录下 <copy todir="../dest/dir"> <fileset dir="src_dir"> <exclude name="**/*.java"/> </fileset> </copy> 或者 <copy todir="../dest/dir"> <fileset dir="src_dir" excludes="**/*.java"/> </copy> ⑤拷贝一批文件到指定目录下,并将文件名后增加.Bak后缀 <copy todir="../backup/dir"> <fileset dir="src_dir"/> <mapper type="glob" from="*" to="*.bak"/> </copy> ⑥拷贝一个文件集合到一个目录,同时建立备份文件 <copy todir="../backup/dir"> <fileset dir="src_dir"/> <globmapper from="*" to="*.bak"/> </copy> ⑦拷贝sr_dir目录到backup/dir目录,并把所有文件中的@TITLE@替换成Foo Bar <copy todir="../backup/dir"> <fileset dir="src_dir"/> <filterset> <filter token="TITLE" value="Foo Bar"/> </filterset> </copy> ⑧拷贝一个目录下的东西到另一个目录下(includes加入,excludes排除) <copydir src="${src}/resources" mce_src="${src}/resources" dest="${dist}" includes="**/*.java" excludes="**/Test.java"/> ================================================================================================================= --><!-- ================================================================================================================= 9、删除操作 <target name="clean"> <delete dir="${dest.dir}"/> <delete file="${dest2.dir}"/> </target> ①删除一个文件<delete file="/lib/ant.jar"/> ②删除指定目录及其子目录<delete dir="lib"/> ③删除指定的一组文件<delete> <fileset dir="." includes="**/*.bak"/> </delete> ④删除当前目录下所有的文件和目录,不包括当前目录<delete includeEmptyDirs="true"> <fileset dir="build"/> </delete> ⑤删除当前目录下所有的文件和目录,不包括当前目录<delete includeEmptyDirs="true"> <fileset dir="build" includes="**/*"/> </delete> ⑥删除当前目录下所有的svn相关文件(因为svn文件默认是excludes的,所以这里要设置一下) <delete defaultexcludes="false"> <fileset dir="src" includes="**/.svn"/> </delete> ⑦删除文件目录树<deltree dir="dist"/> ================================================================================================================= 10、移动操作 ①移动或重命名一个文件<move file="file.orig" tofile="file.moved"/> ②移动或重命名一个文件到另一个文件夹下面<move file="file.orig" todir="dir/to/move/to"/> ③将一个目录移到另外一个目录下<move todir="new/dir/to/move/to"> <fileset dir="src/dir"/> </move> ④将一组文件移动到另外的目录下<move todir="some/new/dir"> <fileset dir="my/src/dir"> <include name="**/*.jar"/> <exclude name="**/ant.jar"/> </fileset> </move> ⑤移动文件过程中增加.Bak后缀<move todir="my/src/dir"> <fileset dir="my/src/dir"> <exclude name="**/*.bak"/> </fileset> <mapper type="glob" from="*" to="*.bak"/> </move> ================================================================================================================= 11、其它操作 ①重命名文件<rename src="foo.jar" mce_src="foo.jar" dest="ant-${version}.jar"/> ②建立临时文件<tempfile property="temp.file" destDir="build" suffix=".xml"/> 即在build目录下,建立文件名为temp.file,后缀为.xml的文件 ③输出信息<echo message="xxx"/>或者<echo>yyy</echo> ④输出一段XML<echoxml file="subbuild.xml"> <project default="foo"> <target name="foo"> <echo>foo</echo> </target> </project> </echoxml> ⑤引入一个XML文件<import file="../common-targets.xml"/> ================================================================================================================= --><!-- ================================================================================================================= 12、Touch的使用 ①若文件不存在,则创建文件;如果存在,则更改最后访问时间为当前系统时间 <touch file="myfile"/> ②若文件不存在,则创建文件,并更改最后访问时间为10/15/2010 2:02 pm <touch file="myfile" datetime="10/15/2010 2:02 pm"/> ③更改目录下所有文件的最后访问时间为10/15/2010 2:02 pm <touch datetime="10/15/2010 2:02 pm"> <fileset dir="src_dir"/> </touch> ================================================================================================================= 13、Condition的使用 有<and>、<or>、<not>等tag <condition property="isMacOsButNotMacOsX"> <and> <os family="mac"/> <not> <os family="unix"/> </not> </and> </condition> ================================================================================================================= 14、替换replace <replace file="configure.sh" value="defaultvalue" propertyFile="source/name.properties"> <replacefilter token="@token1@"/> <replacefilter token="@token2@" value="value2"/> <replacefilter token="@token3@" property="property.key"/> </replace> ================================================================================================================= 15、调用chmod <chmod perm="go-rwx" type="file"> <fileset dir="/web"> <include name="**/*.cgi"/> <include name="**/*.old"/> </fileset> <dirset dir="/web"> <include name="**/private_*"/> </dirset> </chmod> ================================================================================================================= --><!-- ================================================================================================================= 16、checksum MD5运算 ①md5文件,然后把值放入foo.bar.MD5属性<checksum file="foo.bar"/> ②md5文件,然后把值放入foobarMD5属性 <checksum file="foo.bar" property="foobarMD5"/> ③Md5目录下的所有文件,然后建立一个.md5文件,把所有的md5值放入 <checksum> <fileset dir="."> <include name="foo*"/> </fileset> </checksum> ================================================================================================================= 17、Available的使用 ①如果类存在,则设置属性Myclass.present为true,如果没有就false <available classname="org.whatever.Myclass" property="Myclass.present"/> ②若文件存在,则设置属性jaxp.jar.presen为true,否则为false <property name="jaxp.jar" value="./lib/jaxp11/jaxp.jar"/> <available file="${jaxp.jar}" property="jaxp.jar.present"/> ③若目录存在,则设置属性local.lib.present为true,否则为false <available file="/usr/local/lib" type="dir" property="local.lib.present"/> ================================================================================================================= 18、设置property ①设置属性<property name="foo.dist" value="dist"/> ②读取属性文件中的属性配置<property file="foo.properties"/> ③读取网络中的属性文件<property url="http://www.mysite.com/bla/props/foo.properties"/> ④读取文件中的属性配置<property resource="foo.properties"/> ⑤读取环境变量<property environment="env"/> ⑥读取属性文件中的属性,并作为全局引用<property file="/Users/antoine/ant-global.properties"/> ================================================================================================================= 19、显示错误 ①方式一<fail>Something wrong here.</fail> ②方式二<fail message="${属性}"/> ③如果这个属性不存在显示错误<fail unless="failCondition" message="unless Condition"/> ④如果这个属性存在显示错误<fail if="failCondition" message="if Condition"/> ⑤如果符合条件显示错误,这里的条件是(没有设置属性) <fail message="tag condition"> <condition> <not> <isset property=" failConditon "/> </not> </condition> </fail> ================================================================================================================= --><!-- ================================================================================================================= 20、时间戳 ①在生成环境中使用当前时间和日期,以某种方式标记某个生成任务的输出,以便记录它是何时生成的,这经常是可取的 这可能涉及编辑一个文件,以便插入一个字符串来指定日期和时间,或将这个信息合并到JAR或zip文件的文件名中 这种需要是通过简单但是非常有用的tstamp任务来解决的 这个任务通常在某次生成过程开始时调用,比如在一个init目标中。这个任务不需要属性,许多情况下只需<tstamp/>就足够了 tstamp不产生任何输出;相反,它根据当前系统时间和日期设置Ant属性 ②下面是tstamp设置的一些属性、对每个属性的说明,以及这些属性可被设置到的值的例子 DSTAMP==设置为当前日期,默认格式为yyyymmdd==如20031217 TSTAMP==设置为当前时间,默认格式为hhmm======如1603 TODAY===设置为当前日期,带完整的月份========如2010年10月15日 ③比如我们按该方式创建了一个JAR文件:<jar destfile="package.jar" basedir="classes"/> 在调用tstamp任务之后,我们能够根据日期命名该JAR文件:<jar destfile="package-${DSTAMP}.jar" basedir="classes"/> 因此,如果这个任务在2010年10月15日调用,则该JAR文件将被命名为package-20101015.jar ④另外,还可以配置tstamp任务来设置不同的属性,应用一个当前时间之前或之后的时间偏移,或以不同的方式格式化该字符串 所有这些都是使用一个嵌套的format元素来完成的,如下所示: <tstamp> <format property="OFFSET_TIME" pattern="HH:mm:ss" offset="10" unit="minute"/> </tstamp> 上面的清单将OFFSET_TIME属性设置为距离当前时间10分钟之后的小时数、分钟数和秒数 用于定义格式字符串的字符与java.text.SimpleDateFormat类所定义的那些格式字符相同 ================================================================================================================= 21、通过JDBC执行SQL语句 <sql driver="org.gjt.mm.mysql.Driver" url="jdbc:mysql://localhost:3306/mydb" userid="root" password="root" src="data.sql" mce_src="data.sql"/> ================================================================================================================= 22、使用SMTP服务器发送邮件 <mail mailhost="smtp.myisp.com" mailport="1025" subject="Test build"> <from address="me@myisp.com"/> <to address="all@xyz.com"/> <message>The ${buildname} nightly build has completed</message> <fileset dir="dist"> <includes name="**/*.zip"/> </fileset> </mail> mailhost==SMTP服务器地址 mailport==服务器端口 subject===主题 from======发送人地址 to========接收人地址 message===发送的消息 fileset===设置附件 ================================================================================================================= --><!-- ================================================================================================================= 23、配合JUnit实现单元测试 ①把junit.jar拷入ANT_HOME/lib下,并确认在这个目录下有optional.jar,因为JUnit是Ant的扩展任务,需要引用这个扩展包 ②在build.xml中加入JUnit的任务 <target name="run" depends="client"> <junit printsummary="yes" fork="yes" haltonfailure="yes"> <classpath> <pathelement location="client.jar" /> </classpath> <formatter type="plain" /> <test name="com.ascenttech.HelloWorldTest" /> </junit> </target> ================================================================================================================= --><!-- ================================================================================================================= 【补充】使用Ant调用native2ascii转换中文 在src下建立一个名为build.xml的文件,代码如下 <project> <target name="I18N"> <native2ascii dest="com//jadyer//test" src="com//jadyer//temp" mce_src="com//jadyer//temp" includes="*.properties" encoding="UTF-8"> </native2ascii> </target> </project> src表示所要转换的资源文件的路径 dest表示目标路径。路径若为空则代表当前根目录 ================================================================================================================= -->

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Hydra is a multi-cloud orchestration platform that provides a unified interface to manage resources and applications across different cloud providers. To use Hydra with Oracle Cloud, you need to follow the steps below: 1. Create an Oracle Cloud account and obtain the required credentials, including the tenancy ID, user ID, and API key. 2. Install the Hydra CLI tool on your local machine. 3. Authenticate Hydra with Oracle Cloud by running the following command: ``` hydra connect oracle --tenancy-id=<tenancy_id> --user-id=<user_id> --api-key=<api_key> ``` 4. Verify that the connection is successful by running the following command: ``` hydra status ``` 5. Create a Hydra environment file that defines the resources you want to manage in Oracle Cloud. The file should include the following information: - Provider: "oracle" - Region: The Oracle Cloud region where you want to deploy the resources - Compartment: The compartment ID where you want to deploy the resources - Resources: The resources you want to deploy, such as virtual machines, load balancers, or databases. 6. Deploy the resources by running the following command: ``` hydra apply -f <path_to_environment_file> ``` 7. Monitor and manage the deployed resources using the Hydra dashboard or CLI. Note that Hydra supports a wide range of cloud providers and services, including AWS, Azure, Google Cloud, and Kubernetes, among others. You can use Hydra to manage resources across multiple clouds and regions, and automate common tasks such as provisioning, scaling, and monitoring.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值