MantisBT - CMake
View Issue Details
0013360CMakeModulespublic2012-06-28 07:162014-10-06 10:32
Marcin Wojdyr 
 
normalmajoralways
closedfixed 
Linux
CMake 2.8.8 
CMake 3.1CMake 3.1 
0013360: Java add_jar() doesn't copy resources to build directory when make is run for the first time
With this CMakeLists.txt:

cmake_minimum_required(VERSION 2.8.6)
find_package(Java)
include(UseJava)
project(foo Java)
add_jar(foo foo.java res1.txt)

the first time I run "make" it fails with message:

res1.txt : no such file or directory

but when I run make again it works:

[ 25%] Copying res1.txt to the build directory
[100%] Built target foo
Full session:

$ cmake .
CMake Warning at /usr/share/cmake/Modules/FindJava.cmake:120 (message):
  regex not supported: java version "1.7.0_03-icedtea"

  OpenJDK Runtime Environment (fedora-2.2.1.fc17.8-x86_64)

  OpenJDK 64-Bit Server VM (build 23.0-b21, mixed mode). Please report
Call Stack (most recent call first):
  CMakeLists.txt:2 (find_package)

-- Found Java: /bin/java (found version "..")
-- Configuring done
-- Generating done
-- Build files have been written to: /run/mw/test/sub
$ make
Scanning dependencies of target foo
[ 25%] Building Java objects for foo.jar
[ 50%] Generating CMakeFiles/foo.dir/java_class_filelist
[ 75%] Creating Java archive foo.jar
res1.txt : no such file or directory
make[2]: *** [foo.jar] Error 1
make[1]: *** [CMakeFiles/foo.dir/all] Error 2
make: *** [all] Error 2
$ make
[ 25%] Copying res1.txt to the build directory
[100%] Built target foo
No tags attached.
Issue History
2012-06-28 07:16Marcin WojdyrNew Issue
2012-08-11 21:42David ColeStatusnew => backlog
2012-08-11 21:42David ColeNote Added: 0030450
2012-10-02 10:46mefosterNote Added: 0031151
2012-10-02 11:04mefosterNote Added: 0031152
2014-03-20 09:54Brad KingNote Added: 0035454
2014-03-20 09:54Brad KingStatusbacklog => resolved
2014-03-20 09:54Brad KingResolutionopen => fixed
2014-03-20 09:54Brad KingFixed in Version => CMake 3.1
2014-03-20 09:54Brad KingTarget Version => CMake 3.1
2014-10-06 10:32Robert MaynardNote Added: 0036949
2014-10-06 10:32Robert MaynardStatusresolved => closed

Notes
(0030450)
David Cole   
2012-08-11 21:42   
Sending old, never assigned issues to the backlog.

(The age of the bug, plus the fact that it's never been assigned to anyone means that nobody is actively working on it...)

If an issue you care about is sent to the backlog when you feel it should have been addressed in a different manner, please bring it up on the CMake mailing list for discussion. Sign up for the mailing list here, if you're not already on it: http://www.cmake.org/mailman/listinfo/cmake [^]

It's easy to re-activate a bug here if you can find a CMake developer who has the bandwidth to take it on, and ferry a fix through to our 'next' branch for dashboard testing.
(0031151)
mefoster   
2012-10-02 10:46   
Additional information about this bug: you actually have to build *three* times to get the resources actually into the generated jar file.

- First time: error as shown above
- Second time: file gets copied but not included in jar
- Third time: file actually gets added to the jar

I may take a look at this myself ...
(0031152)
mefoster   
2012-10-02 11:04   
The following patch fixes the issue for me.

I suppose I should join the mailing list to submit this?

--- /usr/share/cmake/Modules/UseJava.cmake.orig 2012-10-02 16:01:22.406951456 +0100
+++ /usr/share/cmake/Modules/UseJava.cmake 2012-10-02 16:01:38.084905553 +0100
@@ -258,6 +258,7 @@
     set(_JAVA_COMPILE_FILES)
     set(_JAVA_DEPENDS)
     set(_JAVA_RESOURCE_FILES)
+ set(_JAVA_RESOURCE_FILES_RELATIVE)
     foreach(_JAVA_SOURCE_FILE ${_JAVA_SOURCE_FILES})
         get_filename_component(_JAVA_EXT ${_JAVA_SOURCE_FILE} EXT)
         get_filename_component(_JAVA_FILE ${_JAVA_SOURCE_FILE} NAME_WE)
@@ -294,7 +295,8 @@
             __java_copy_file(${CMAKE_CURRENT_SOURCE_DIR}/${_JAVA_SOURCE_FILE}
                              ${CMAKE_JAVA_CLASS_OUTPUT_PATH}/${_JAVA_SOURCE_FILE}
                              "Copying ${_JAVA_SOURCE_FILE} to the build directory")
- list(APPEND _JAVA_RESOURCE_FILES ${_JAVA_SOURCE_FILE})
+ list(APPEND _JAVA_RESOURCE_FILES ${CMAKE_JAVA_CLASS_OUTPUT_PATH}/${_JAVA_SOURCE_FILE})
+ list(APPEND _JAVA_RESOURCE_FILES_RELATIVE ${_JAVA_SOURCE_FILE})
         endif (_JAVA_EXT MATCHES ".java")
     endforeach(_JAVA_SOURCE_FILE)
 
@@ -337,7 +339,7 @@
             OUTPUT ${_JAVA_JAR_OUTPUT_PATH}
             COMMAND ${Java_JAR_EXECUTABLE}
                 -cf${_ENTRY_POINT_OPTION} ${_JAVA_JAR_OUTPUT_PATH} ${_ENTRY_POINT_VALUE}
- ${_JAVA_RESOURCE_FILES} @java_class_filelist
+ ${_JAVA_RESOURCE_FILES_RELATIVE} @java_class_filelist
             COMMAND ${CMAKE_COMMAND}
                 -D_JAVA_TARGET_DIR=${CMAKE_JAVA_TARGET_OUTPUT_DIR}
                 -D_JAVA_TARGET_OUTPUT_NAME=${_JAVA_TARGET_OUTPUT_NAME}
@@ -357,7 +359,7 @@
             OUTPUT ${_JAVA_JAR_OUTPUT_PATH}
             COMMAND ${Java_JAR_EXECUTABLE}
                 -cf${_ENTRY_POINT_OPTION} ${_JAVA_JAR_OUTPUT_PATH} ${_ENTRY_POINT_VALUE}
- ${_JAVA_RESOURCE_FILES} @java_class_filelist
+ ${_JAVA_RESOURCE_FILES_RELATIVE} @java_class_filelist
             COMMAND ${CMAKE_COMMAND}
                 -D_JAVA_TARGET_DIR=${CMAKE_JAVA_TARGET_OUTPUT_DIR}
                 -D_JAVA_TARGET_OUTPUT_NAME=${_JAVA_TARGET_OUTPUT_NAME}
(0035454)
Brad King   
2014-03-20 09:54   
A patch based on 0013360:0031152 has been applied:

 UseJava: Copy resource files before compiling the jar
 http://cmake.org/gitweb?p=cmake.git;a=commitdiff;h=2abd8efe [^]
(0036949)
Robert Maynard   
2014-10-06 10:32   
Closing resolved issues that have not been updated in more than 4 months.