THRIFT-3677 Improve CMake Java build

This closes #897
diff --git a/appveyor.yml b/appveyor.yml
index 61c5f9a..f36c7f0 100755
--- a/appveyor.yml
+++ b/appveyor.yml
@@ -53,27 +53,32 @@
 - move *.h include\
 - cd ..
 - cinst winflexbison
-- cinst nsis
-# TODO: Enable Java build
 # - cinst ant -ignoreDependencies
+- cd \
+- appveyor DownloadFile http://www.us.apache.org/dist//ant/binaries/apache-ant-1.9.6-bin.zip
+- 7z x apache-ant-1.9.6-bin.zip
+- cd %APPVEYOR_BUILD_FOLDER%
 # TODO: Enable Haskell build
 # - cinst HaskellPlatform -version 2014.2.0.0
 
 
 build_script:
-# - set PATH=C:\ProgramData\chocolatey\bin;C:\tools\apache-ant-1.9.4\bin;%PATH%
-# - set JAVA_HOME=C:\Program Files\Java\jdk1.7.0
-# - set PATH=%JAVA_HOME%\bin;%PATH%
+- set PATH=C:\ProgramData\chocolatey\bin;C:\apache-ant-1.9.6\bin;%PATH%
+- set JAVA_HOME=C:\Program Files\Java\jdk1.7.0
+- set PATH=%JAVA_HOME%\bin;%PATH%
 # - set PATH=%PATH%;C:\Program Files (x86)\Haskell Platform\2014.2.0.0\bin
 # - set PATH=%PATH%;C:\Program Files (x86)\Haskell Platform\2014.2.0.0\lib\extralibs\bin
 - set PATH=C:\Python27-x64;%PATH%
 - mkdir cmake-build
 - cd cmake-build
-- cmake -G "Visual Studio 14 2015 Win64" -DWITH_JAVA=OFF -DWITH_SHARED_LIB=OFF -DLIBEVENT_ROOT=%APPVEYOR_BUILD_FOLDER%\libevent-2.0.22-stable -DZLIB_INCLUDE_DIR=%APPVEYOR_BUILD_FOLDER%\zlib-1.2.8 -DZLIB_LIBRARY=%APPVEYOR_BUILD_FOLDER%\zlib-1.2.8\release\zlibstatic.lib -DBOOST_ROOT="%BOOST_ROOT% -DBOOST_LIBRARYDIR="%BOOST_LIBRARYDIR% ..
+- cmake -G "Visual Studio 14 2015 Win64" -DWITH_JAVA=ON -DWITH_SHARED_LIB=OFF -DLIBEVENT_ROOT=%APPVEYOR_BUILD_FOLDER%\libevent-2.0.22-stable -DZLIB_INCLUDE_DIR=%APPVEYOR_BUILD_FOLDER%\zlib-1.2.8 -DZLIB_LIBRARY=%APPVEYOR_BUILD_FOLDER%\zlib-1.2.8\release\zlibstatic.lib -DBOOST_ROOT="%BOOST_ROOT% -DBOOST_LIBRARYDIR="%BOOST_LIBRARYDIR% ..
+- findstr /b /e BUILD_COMPILER:BOOL=ON CMakeCache.txt
+- findstr /b /e BUILD_JAVA:BOOL=ON CMakeCache.txt
+- findstr /b /e BUILD_TESTING:BOOL=ON CMakeCache.txt
 # - cmake --build .
 - cmake --build . --config Release
 # TODO: Fix cpack
 # - cpack
-- ctest -C Release -VV -E "(concurrency_test|processor_test|python_test$|^Python)"
+- ctest -C Release -VV -E "(concurrency_test|processor_test|python_test$|^Python|^Java)"
 
 #TODO make it perfect ;-r
diff --git a/build/cmake/CPackConfig.cmake b/build/cmake/CPackConfig.cmake
index 45077c0..fdc1b4e 100644
--- a/build/cmake/CPackConfig.cmake
+++ b/build/cmake/CPackConfig.cmake
@@ -58,7 +58,7 @@
         "https://issues.apache.org/jira/browse/THRIFT" "Apache Thrift - Issues")
     set(CPACK_NSIS_CONTACT ${CPACK_PACKAGE_CONTACT})
     set(CPACK_NSIS_MODIFY_PATH "ON")
-    set(CPACK_PACKAGE_INSTALL_DIRECTORY ${CPACK_PACKAGE_NAME})
+    set(CPACK_PACKAGE_INSTALL_DIRECTORY "${CPACK_PACKAGE_NAME}")
 else()
     set(CPACK_GENERATOR "DEB" )
     set(CPACK_DEBIAN_PACKAGE_MAINTAINER ${CPACK_PACKAGE_CONTACT})
diff --git a/lib/java/CMakeLists.txt b/lib/java/CMakeLists.txt
index 2014f5c..57b97f1 100644
--- a/lib/java/CMakeLists.txt
+++ b/lib/java/CMakeLists.txt
@@ -28,7 +28,7 @@
     set(THRIFT_AAR outputs/aar/thrift-debug.aar outputs/aar/thrift-release.aar)
     add_custom_command(
         OUTPUT ${THRIFT_AAR}
-        COMMAND ${GRADLE_EXECUTABLE} -p${CMAKE_CURRENT_SOURCE_DIR}/android -PbuildDir=${CMAKE_CURRENT_BINARY_DIR} assemble
+        COMMAND ${GRADLE_EXECUTABLE} -p"${CMAKE_CURRENT_SOURCE_DIR}/android" -PbuildDir="${CMAKE_CURRENT_BINARY_DIR}" assemble
         DEPENDS ${java_sources} ${android_sources})
     add_custom_target(thrift_aar ALL DEPENDS ${THRIFT_AAR})
 
@@ -46,21 +46,35 @@
         set(JAVA_DOC_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/${DOC_INSTALL_DIR}/java")
     endif()
 
-    add_custom_target(ant_build ALL
-        COMMAND ${Ant_EXECUTABLE} ${ANT_FLAGS} -Dbuild.dir=${CMAKE_CURRENT_BINARY_DIR} -Dworking.dir=${work_dir} -f build.xml
+    file(GLOB_RECURSE ThriftJava_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/*.java)
+    set(ThriftJava_ARTIFACTS
+        libthrift-${thrift_VERSION}.jar
+        libthrift-${thrift_VERSION}.pom
+    )
+    add_custom_command(
+        OUTPUT ${ThriftJava_ARTIFACTS}
+        COMMAND ${Ant_EXECUTABLE} ${ANT_FLAGS} -Dbuild.dir="${CMAKE_CURRENT_BINARY_DIR}" -f build.xml
+        MAIN_DEPENDENCY ${CMAKE_CURRENT_SOURCE_DIR}/build.xml
+        DEPENDS ${ThriftJava_SOURCES}
         WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
+    )
+
+    add_custom_target(ThriftJava ALL
         COMMENT "Building Java library using Ant"
+        DEPENDS ${ThriftJava_ARTIFACTS}
     )
 
     # Hook the ant install task into CMake install
     install(CODE "execute_process(
         COMMAND ${Ant_EXECUTABLE} ${ANT_FLAGS} install
-        -Dbuild.dir=${CMAKE_CURRENT_BINARY_DIR} -Dworking.dir=${work_dir}
-        -Dinstall.path=${JAVA_INSTALL_DIR} -Dinstall.javadoc.path=${JAVA_DOC_INSTALL_DIR} -f build.xml
+        -Dbuild.dir=\"${CMAKE_CURRENT_BINARY_DIR}\"
+        -Dinstall.path=\"${JAVA_INSTALL_DIR}\" -Dinstall.javadoc.path=\"${JAVA_DOC_INSTALL_DIR}\" -f build.xml
         WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
     )")
 
     if(BUILD_TESTING)
-        #add_subdirectory(test)
+        add_test(NAME JavaTest
+                 COMMAND ${Ant_EXECUTABLE} ${ANT_FLAGS} -Dbuild.dir="${CMAKE_CURRENT_BINARY_DIR}" -Dthrift.compiler="${THRIFT_COMPILER}" -f build.xml test
+                 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
     endif()
 endif(ANDROID)
diff --git a/lib/java/build.xml b/lib/java/build.xml
old mode 100755
new mode 100644
index 62680b7..1097af5
--- a/lib/java/build.xml
+++ b/lib/java/build.xml
@@ -36,6 +36,7 @@
 
   <property name="final.name" value="${thrift.artifactid}-${version}"/>
 
+  <property name="thrift.compiler" location="../../compiler/cpp/thrift"/>
   <property name="src" location="${basedir}/src"/>
   <property name="build.dir" location="${basedir}/build"/>
   <property name="build.lib.dir" location="${build.dir}/lib"/>
@@ -260,31 +261,31 @@
 
   <target name="generate">
     <!-- Generate the thrift gen-java source -->
-    <exec executable="../../compiler/cpp/thrift" failonerror="true">
+    <exec executable="${thrift.compiler}" failonerror="true">
       <arg line="--gen java ${test.thrift.home}/ThriftTest.thrift"/>
     </exec>
-    <exec executable="../../compiler/cpp/thrift" failonerror="true">
+    <exec executable="${thrift.compiler}" failonerror="true">
       <arg line="--gen java ${test.thrift.home}/DebugProtoTest.thrift"/>
     </exec>
-    <exec executable="../../compiler/cpp/thrift" failonerror="true">
+    <exec executable="${thrift.compiler}" failonerror="true">
       <arg line="--gen java ${test.thrift.home}/OptionalRequiredTest.thrift"/>
     </exec>
-    <exec executable="../../compiler/cpp/thrift" failonerror="true">
+    <exec executable="${thrift.compiler}" failonerror="true">
       <arg line="--gen java:beans,nocamel ${test.thrift.home}/JavaBeansTest.thrift"/>
     </exec>
-    <exec executable="../../compiler/cpp/thrift" failonerror="true">
+    <exec executable="${thrift.compiler}" failonerror="true">
       <arg line="--gen java ${test.thrift.home}/ManyOptionals.thrift"/>
     </exec>
     <exec executable="mkdir" failonerror="true">
       <arg line="-p ${genfullcamel}"/>
     </exec>
-    <exec executable="../../compiler/cpp/thrift" failonerror="true">
+    <exec executable="${thrift.compiler}" failonerror="true">
       <arg line="--gen java:fullcamel -out ${genfullcamel} ${test.thrift.home}/FullCamelTest.thrift"/>
     </exec>
     <exec executable="mkdir" failonerror="true">
       <arg line="-p ${genreuse}"/>
     </exec>
-    <exec executable="../../compiler/cpp/thrift" failonerror="true">
+    <exec executable="${thrift.compiler}" failonerror="true">
       <arg line="--gen java:reuse-objects -out ${genreuse} ${test.thrift.home}/ReuseObjects.thrift"/>
     </exec>
   </target>
@@ -370,7 +371,7 @@
 
     <!-- Dependencies needed for testing -->
     <artifact:dependencies filesetId="test-dependency-jars" useScope="runtime">
-      <dependency groupId="org.slf4j" artifactId="slf4j-log4j12" version="1.5.8"/>
+      <dependency groupId="org.slf4j" artifactId="slf4j-log4j12" version="${slf4j.version}"/>
       <dependency groupId="junit" artifactId="junit" version="4.4"/>
     </artifact:dependencies>