CMake: Added better dependency tracking for external build tools
diff --git a/lib/java/CMakeLists.txt b/lib/java/CMakeLists.txt
index 28158c0..b9bfc81 100644
--- a/lib/java/CMakeLists.txt
+++ b/lib/java/CMakeLists.txt
@@ -48,19 +48,25 @@
         set(PRELEASE "false")
     endif ()
 
-    add_custom_target(ThriftJava ALL
+    file(GLOB_RECURSE THRIFTJAVA_SOURCES LIST_DIRECTORIES false
+        "${CMAKE_CURRENT_SOURCE_DIR}/src/*")
+    add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/build/libs/libthrift.jar"
         COMMENT "Building Java library using Gradle Wrapper"
         COMMAND ${GRADLEW_EXECUTABLE} ${GRADLE_OPTS} assemble
             --console=plain --no-daemon
             -Prelease=${PRELEASE}
             -Pthrift.version=${thrift_VERSION}
             "-Pbuild.dir=${CMAKE_CURRENT_BINARY_DIR}/build"
+        DEPENDS ${THRIFTJAVA_SOURCES}
         WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
     )
+    add_custom_target(ThriftJava ALL
+        DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/build/libs/libthrift.jar")
 
     # Enable publishing from CMake if the publishing information is provided
     add_custom_target(MavenPublish
         COMMENT "Publishing Java Library to Apache Maven staging"
+        DEPENDS ThriftJava
         COMMAND ${GRADLEW_EXECUTABLE} ${GRADLE_OPTS} clean uploadArchives
             --console=plain --no-daemon
             -Prelease=${PRELEASE}
@@ -83,13 +89,13 @@
 
     if(BUILD_TESTING)
         add_test(NAME JavaTest
-                COMMAND ${GRADLEW_EXECUTABLE} ${GRADLE_OPTS} test
-                    --console=plain --no-daemon
-                    -Prelease=${PRELEASE}
-                    -Pthrift.version=${thrift_VERSION}
-                    "-Pbuild.dir=${CMAKE_CURRENT_BINARY_DIR}/build"
-                    "-Pthrift.compiler=${THRIFT_COMPILER}"
-                WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
+            COMMAND ${GRADLEW_EXECUTABLE} ${GRADLE_OPTS} test
+                --console=plain --no-daemon
+                -Prelease=${PRELEASE}
+                -Pthrift.version=${thrift_VERSION}
+                "-Pbuild.dir=${CMAKE_CURRENT_BINARY_DIR}/build"
+                "-Pthrift.compiler=${THRIFT_COMPILER}"
+            WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
     endif()
 
 endif()