---
title: "ant使用sample（数据库连接）"
author: "cocogg"
date: "2009-02-26T13:29:35Z"
tags: []
slug: "ant-sample"
draft: false
lastmod: "2009-02-26T13:48:52Z"
id: 339274
url: "https://cocogg.iteye.com/blog/339274"
---

下面是一个sample，还没有完成说明。
properties

```java
oracle.service = sample
oracle.host = localhost
oracle.port = 1521
jdbcdriver.path = ....\ojdbc14.jar
```




```java
<project name="sql_sample" default="usage" basedir=".">
	<property name="prop.file" value="sample.properties" />
	<property file="${prop.file}" />
	<property name="install.dir" value="${sample.home}/install" />
	<property name="conf.dir" value="${sample.home}/conf" />
	<property name="sql.dir" value="${sample.home}/sql" />
	<property name="sql.table.dir" value="${sql.dir}/table" />
	<property name="jdbc.url" value="jdbc:oracle:thin:@${oracle.host}:${oracle.port}:${oracle.service}" />
	<property name="jdbc.driver.class" value="oracle.jdbc.driver.OracleDriver" />

	<property name="jdbc.sample.user" value="sample" />
	<property name="jdbc.sample.password" value="sample" />

	<path id="jdbcdriver.class.path">
		<pathelement location="${jdbcdriver.path}" />
	</path>

	<!-- =======================================================
	Main Targets.
      ======================================================= -->
	<target name="INSTALL" >
		<antcall target="echo_prop" />
		<antcall target="assertion" />
		<antcall target="sql_create_user" />
		<antcall target="sql_create_table" />
	</target>

	<target name="UNINSTALL">
		<antcall target="echo_prop" />
		<antcall target="assertion" />
		<antcall target="sql_delete_table" />
		<antcall target="sql_drop_user" />
	</target>

	<target name="usage">
		<echo message="Usage   :  ant -f &lt;THIS_FILE&gt; -Doracle.system.password=&lt;ORACLE_SYSTEM_PASSWORD&gt; &lt;TARGET&gt;" />
		<echo message="Example :  ant -f ant_setup_db.xml -Doracle.system.password=change_on_install INSTALL" />
	</target>

	<!-- =======================================================
         Database Management
      ======================================================= -->
	<target name="sql_connect">
		<echo message="jdbcUrl = ${jdbc.url}" />
		<echo message="jdbc.driver.class = ${jdbc.driver.class}"/>
		<echo message="oracle.system.password = ${oracle.system.password}"/>
		<echo message="jdbcdriver.class.path = ${jdbcdriver.class.path}"/>
		<sql classpathref="jdbcdriver.class.path" driver="${jdbc.driver.class}" password="${oracle.system.password}" url="${jdbc.url}" userid="system" autocommit="true" print="true" output="${install.dir}/assertion_readBy_ant.log" append="true" onerror="continue">
			SELECT SYSDATE FROM DUAL;
		</sql>
	</target>

  <target name="sql_create_user">
		<echo message="jdbcUrl = ${jdbc.url}" />
		<sql classpathref="jdbcdriver.class.path" driver="${jdbc.driver.class}" password="${oracle.system.password}" url="${jdbc.url}" userid="system" autocommit="true" print="true" onerror="continue">
			CREATE USER sample PROFILE DEFAULT IDENTIFIED BY sample DEFAULT TABLESPACE USERS TEMPORARY TABLESPACE TEMP ACCOUNT UNLOCK;
			GRANT CONNECT               TO sample;
			GRANT RESOURCE              TO sample;
			GRANT CREATE PUBLIC SYNONYM TO sample;
			GRANT DROP   PUBLIC SYNONYM TO sample;
			GRANT UNLIMITED TABLESPACE  TO sample;
			ALTER USER sample DEFAULT ROLE  ALL;
		</sql>
	</target>
	
	<target name="sql_drop_user">
		<echo message="jdbcUrl = ${jdbc.url}" />
		<sql classpathref="jdbcdriver.class.path" driver="${jdbc.driver.class}" password="${oracle.system.password}" url="${jdbc.url}" userid="system" autocommit="true" print="true" onerror="abort">
			DROP USER sample CASCADE;
		</sql>
	</target>

	<target name="sql_create_table">
		<echo message="jdbcUrl = ${jdbc.url}" />
		<sql classpathref="jdbcdriver.class.path" driver="${jdbc.driver.class}" password="sample" url="${jdbc.url}" userid="sample" autocommit="true" print="true" onerror="continue">
			<transaction src="${sql.table.dir}/createtables.sql" />
			<transaction src="${sql.table.dir}/inittables.sql" />
		</sql>
	</target>
	
	<target name="sql_delete_table">
		<echo message="jdbcUrl = ${jdbc.url}" />
		<sql classpathref="jdbcdriver.class.path" driver="${jdbc.driver.class}" password="sample" url="${jdbc.url}" userid="sample" autocommit="true" print="true" onerror="abort">
			<transaction src="${sql.table.dir}/droptables.sql" />
		</sql>
	</target>

	<!-- =======================================================
         Utility
      ======================================================= -->
	<target name="echo_prop" >
		<echo message="echo poperties" />
		<echo message="oracle.service = ${oracle.service}" />
		<echo message="oracle.host = ${oracle.host}" />
		<echo message="oracle.port = ${oracle.port}" />
		<echo message="jdbcdriver.path = ${jdbcdriver.path}" />
	</target>

	<!-- write assertion_readBy_InstallScript.log -->
	<target name="assertion" >
		<delete file="${install.dir}/assertion_readBy_ant.log" failonerror="false" />
		<delete file="${install.dir}/assertion_readBy_InstallScript.log" failonerror="false" />
		<antcall target="sql_connect" />
		<loadfile property="assertionLog" srcfile="${install.dir}/assertion_readBy_ant.log" />
		<condition property="assertion">
			<and>
				<contains string="${assertionLog}" substring="SYSDATE" casesensitive="false" />
			</and>
		</condition>
		<antcall target="write_assertion" />
		<fail message="VerifyError :  Please Check Oracle Database is running..." unless="assertion" />
	</target>

	<target name="write_assertion">
		<antcall target="write_assertion_OK" />
		<antcall target="write_assertion_NG" />
	</target>

	<target name="write_assertion_OK" if="assertion">
		<echo file="${install.dir}/assertion_readBy_InstallScript.log" append="false">OK</echo>
	</target>

	<target name="write_assertion_NG" unless="assertion">
		<echo file="${install.dir}/assertion_readBy_InstallScript.log" append="false">NG</echo>
	</target>
	
</project>
```