Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you configure cmake to only rebuild changed .java files in a java project?

Tags:

java

cmake

I have a cmake project that looks like:

project(myProject JAVA)
add_library(myLibrary 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,

like image 615
PerilousApricot Avatar asked Jan 24 '11 13:01

PerilousApricot


1 Answers

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.

like image 62
richq Avatar answered Sep 28 '22 05:09

richq