THRIFT-453. java: By default, use Ivy to download Java dependencies

build.xml will now download Apache Ivy into the build tree.  Ivy will
then be used to download the other Java dependencies: log4j and Apache
Commons Lang.  This achieves the goal of allowing the Java library to be
built from a clean checkout, without requiring users to manually obtain
dependencies or checking external binaries into the source tree.

The Ivy behavior (except for a few mkdir calls) can be inhibited by
passing -Dnoivy= on the ant command line (or in a property file).  In
this case, log4j must be available in the user's CLASSPATH.

git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@769995 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/java/build.xml b/lib/java/build.xml
index 6778f2c..0a7c894 100644
--- a/lib/java/build.xml
+++ b/lib/java/build.xml
@@ -1,3 +1,4 @@
+<?xml version="1.0"?>
 <!--
  Licensed to the Apache Software Foundation (ASF) under one
  or more contributor license agreements. See the NOTICE file
@@ -16,7 +17,8 @@
  specific language governing permissions and limitations
  under the License.
 -->
-<project name="libthrift" default="dist" basedir=".">
+<project name="libthrift" default="dist" basedir="."
+  xmlns:ivy="antlib:org.apache.ivy.ant">
 
   <description>Thrift Build File</description>
 
@@ -33,16 +35,79 @@
 
   <property file="${user.home}/.thrift-build.properties" />
 
-  <property name="cpath" location="libthrift.jar:${thrift.extra.cpath}" />
+  <!-- ivy properties -->
+  <property name="ivy.version" value="2.0.0-rc2" />
+  <property name="ivy.dir" location="${build}/ivy" />
+  <property name="ivy.jar" location="${ivy.dir}/ivy-${ivy.version}.jar"/>
+  <property name="ivy.lib.dir" location="${ivy.dir}/lib" />
+  <property name="ivy_repo_url" value="http://repo2.maven.org/maven2/org/apache/ivy/ivy/${ivy.version}/ivy-${ivy.version}.jar"/>
+  <property name="ivysettings.xml" location="${ivy.dir}/ivysettings.xml" />
+
+  <path id="compile.classpath">
+    <fileset dir="${ivy.lib.dir}">
+      <include name="**/*.jar" />
+    </fileset>
+  </path>
+
+  <path id="test.classpath">
+    <path refid="compile.classpath" />
+    <pathelement location="build/test" />
+    <pathelement location="libthrift.jar" />
+  </path>
 
   <target name="init">
     <tstamp />
     <mkdir dir="${build}"/>
     <mkdir dir="${build.test}" />
+    <!--
+    Allow Ivy to be disabled with "-Dnoivy=".
+    It is kind of a hack to pretend that we already found it,
+    but Ant doesn't provide an easy way of blocking dependencies
+    from executing or checking multiple conditions.
+    -->
+    <condition property="ivy.found"><isset property="noivy" /></condition>
+    <condition property="offline"><isset property="noivy" /></condition>
   </target>
 
-  <target name="compile" depends="init">
-    <javac srcdir="${src}" destdir="${build}" source="1.5" debug="true" classpath="${cpath}" />
+  <target name="ivy-init-dirs">
+    <mkdir dir="${ivy.dir}" />
+    <mkdir dir="${ivy.lib.dir}" />
+  </target>
+
+  <target name="ivy-download" depends="ivy-init-dirs" description="To download ivy" unless="offline">
+    <get src="${ivy_repo_url}" dest="${ivy.jar}" usetimestamp="true"/>
+  </target>
+
+  <target name="ivy-probe-antlib">
+    <condition property="ivy.found">
+      <typefound uri="antlib:org.apache.ivy.ant" name="cleancache"/>
+    </condition>
+  </target>
+
+  <target name="ivy-init-antlib" depends="ivy-download,ivy-probe-antlib" unless="ivy.found">
+    <typedef uri="antlib:org.apache.ivy.ant" onerror="fail"
+      loaderRef="ivyLoader">
+      <classpath>
+        <pathelement location="${ivy.jar}"/>
+      </classpath>
+    </typedef>
+    <fail>
+      <condition >
+        <not>
+          <typefound uri="antlib:org.apache.ivy.ant" name="cleancache"/>
+        </not>
+      </condition>
+      You need Apache Ivy 2.0 or later from http://ant.apache.org/
+      It could not be loaded from ${ivy_repo_url}
+    </fail>
+  </target>
+
+  <target name="resolve" depends="ivy-init-antlib" description="retrieve dependencies with ivy" unless="noivy">
+    <ivy:retrieve />
+  </target>
+
+  <target name="compile" depends="init,resolve">
+    <javac srcdir="${src}" destdir="${build}" source="1.5" debug="true" classpathref="compile.classpath" />
   </target>
 
   <target name="javadoc" depends="init">
@@ -85,28 +150,28 @@
   </target>
 
   <target name="compile-test" description="Build the test suite classes" depends="generate,dist">
-    <javac debug="true" srcdir="${gen}" destdir="${build.test}" classpath="${cpath}" />
-    <javac debug="true" srcdir="${genbean}" destdir="${build.test}" classpath="${cpath}" />
-    <javac debug="true" srcdir="${src.test}" destdir="${build.test}" classpath="${cpath}:${gen}" />
+    <javac debug="true" srcdir="${gen}" destdir="${build.test}" classpathref="test.classpath" />
+    <javac debug="true" srcdir="${genbean}" destdir="${build.test}" classpathref="test.classpath" />
+    <javac debug="true" srcdir="${src.test}" destdir="${build.test}" classpathref="test.classpath" />
   </target>
 
   <target name="test" description="Run the full test suite" depends="compile-test">
     <java classname="org.apache.thrift.test.JSONProtoTest"
-      classpath="${cpath}:${build.test}" failonerror="true" />
+      classpathref="test.classpath" failonerror="true" />
     <java classname="org.apache.thrift.test.TCompactProtocolTest"
-      classpath="${cpath}:${build.test}" failonerror="true" />
+      classpathref="test.classpath" failonerror="true" />
     <java classname="org.apache.thrift.test.IdentityTest"
-      classpath="${cpath}:${build.test}" failonerror="true" />
+      classpathref="test.classpath" failonerror="true" />
     <java classname="org.apache.thrift.test.EqualityTest"
-      classpath="${cpath}:${build.test}" failonerror="true" />
+      classpathref="test.classpath" failonerror="true" />
     <java classname="org.apache.thrift.test.ToStringTest"
-      classpath="${cpath}:${build.test}" failonerror="true" />
+      classpathref="test.classpath" failonerror="true" />
     <java classname="org.apache.thrift.test.DeepCopyTest"
-      classpath="${cpath}:${build.test}" failonerror="true" />
+      classpathref="test.classpath" failonerror="true" />
     <java classname="org.apache.thrift.test.MetaDataTest"
-      classpath="${cpath}:${build.test}" failonerror="true" />
+      classpathref="test.classpath" failonerror="true" />
     <java classname="org.apache.thrift.test.JavaBeansTest"
-      classpath="${cpath}:${build.test}" failonerror="true" />
+      classpathref="test.classpath" failonerror="true" />
   </target>
 
   <target name="generate">
diff --git a/lib/java/ivy.xml b/lib/java/ivy.xml
new file mode 100644
index 0000000..0b1be5d
--- /dev/null
+++ b/lib/java/ivy.xml
@@ -0,0 +1,7 @@
+<ivy-module version="1.0">
+    <info organisation="jayasoft" module="hello-ivy" />
+    <dependencies>
+       <dependency org="log4j" name="log4j" rev="1.2.15" conf="default->master"/> 
+       <dependency org="commons-lang" name="commons-lang" rev="2.4" conf="* -> *,!sources,!javadoc"/>
+    </dependencies>
+</ivy-module>