How to work with Ant

完整版见https://jadyer.github.io/




<?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"/>
=================================================================================================================
-->
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
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.

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值