How do you configure cmake to only rebuild changed .java files in a java project?
I have a cmake project that looks like:
project(myProject JAVA)
add_library(myLibrary 开发者_JAVA技巧foo.java bar.java)
but when I run make in the directory, all the java files are rebuilt, even if they weren't changed. Is there a way to turn off that behavior?
Thanks,
The add_library
Java support in CMake is not too hot. It ignores the "package" directive, and assume that "foo.java" creates "foo.class" in the base directory, not in a sub-directory com/example/
for package com.example;
.
If you look at the generated makefiles in CMakeFiles/<jar_file>.dir/build.make
, it has code like this (cleaned up a bit)
CMakeFiles/test.dir/foo.class: ../foo.java
javac $(Java_FLAGS) /full/path/to/foo.java -d CMakeFiles/test.dir
This is a broken dependency when foo.java contains "package com.example;" at the top. Make expects foo.class to be created, when it isn't and you run make again, it will compile foo.java to see if maybe this time it will work. The actual file generated is in com/example (which luckily gets added to the final jar file).
The good news is that things have improved recently. In version 2.8.6 of CMake a new module was added called UseJava
that does a much better job of compiling Java files and correctly rebuilding when there are changes. Instead of using add_library
you need to use add_jar
. Here is a complete example CMakeLists.txt file:
cmake_minimum_required(VERSION 2.8.6)
find_package(Java)
include(UseJava)
project(java_test Java)
set(SRC
src/com/example/test/Hello.java
src/com/example/test/Message.java
)
add_jar(hello ${SRC})
That will produce hello.jar from the input source files.
I think your answer is to just build the entire Java project. If you change one class and just recompile it, how will cmake know to compile another class that depended on it. What happens if you remove a method in one class that others depend on, skipping recompiling the other classes will give you run-time errors instead of a compiler error.
However, 40 java files is a small amount and it seems like they should compile in seconds or less.
精彩评论