[CMake] Creating JAR files with cmake

Michael N michael.1138 at gmail.com
Sun Mar 8 22:53:39 EDT 2009


> Subject:    [CMake] Creating JAR files with cmake
> From:       Andreas Pokorny <andre....>
> Date:       2009-03-05 9:22:13
> Message-ID: e8be73390903050122v67f8b416t24a86e998a5ccbb5 () mail ! gmail ! com
>
> Hi there,
> I just tried building a JAR file with CMake. The project structure
> looks like this:
...
> In the top most CMakeLists.txt I search for external jars that are
> required by most of the subprojects. I then set them as:
>
> INCLUDE_DIRECTORIES( ${FOO_JAR_PATH} )
>
> I this the correct way of extending the classpath?
...
> Is there a way to get rid of the path element src/ and the duplicate
> path elements com/foo/bar?
> If not .. could you give me a starting point in the cmake source where
> the Java stuff is handled?
>
> regards
> Andreas Pokorny


Hi,
  I think using CMake is great for Java developers who need to compile
& deploy native code. I've been looking into it off & on for the past
year, but haven't really gotten CMake integrated into our build
process at work (yet).  As I (and other Java developers) already use
Ant & Maven almost exclusively for Java builds, it seemed to me
intuitive to integrate Ant directly into a CMake build process, rather
than use CMake to build Java directly.

  For building Jars & handling dependencies, I have been experimenting
with a custom_target, and having ant's build.xml manually do its own
out-of-source build. It's an idea / work-in-progress, but I was
wondering if it might work as a Java/CMake integration strategy (I'd
only use CMake when the Java app requires native code -- otherwise,
I'd use Ant & Maven exclusively).

  I'll attach a zip of a demo (I've stripped out the JNI stuff I was
using; this is just an Ant / Java / C++ "hello-world" example) for
review -- maybe it'll help with the poster's issue. (Also, there was a
post a week or two ago on setting the Main-Class attribute of the
manifest; I'm doing that in the example, as well.  All jar creation
(manifest, etc) is handled normally by Ant's build.xml.

The key implementation points:
 * the ant-build is *executed* in-source, but the output is placed out-of-source
 * the "makefile" (or whatever other generator) calls "ant" during the "make"
 * the build.xml isn't copied out-of-source; you can make changes to
the java source code and build.xml in SOURCE_DIR and re-run 'make' to
get a new build.


 ** Note/aside: For resolving/finding 3rd party jars, I use
maven-ant-tasks to download them dynamically at build-time, but that's
another topic. This is the equivalent of CMake's "find_package", but
uses on-line Maven repositories to download the dependencies
(transitively), as they are declared in the build.xml file. They are
cached locally once they are downloaded. (Who would want to
re-implement this in another build tool, with simply a different
syntax??)

** Note#2: for JNI, I had to change the FindJNI.cmake a bit to get it
to work. Basically, using JAVA_HOME as a fallback (or, rather, first
choice) helps quite a bit.  The example has the JNI stuff removed.

My 'CMake script' needs a lot of work (still learning), but basically
I'm doing this:
(note: I rely on ANT_HOME and JAVA_HOME, as these are very strong
conventions to use at build-time):
...
FIND_PROGRAM(ANT_EXECUTABLE ant PATHS $ENV{ANT_HOME} ENV PATH )
...
SET(work_dir ${HELLO_BINARY_DIR}/java-build)
ADD_CUSTOM_TARGET(ant_build ALL
  COMMAND ${ANT_EXECUTABLE} -Dworking.dir=${work_dir} -f build.xml
  WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  COMMENT "Running Ant ...")
...

(See attached ... not sure if a zip is allowed through or not; I can
post it somewhere, if not.)

Let me know if  you think this approach is work-able (or if it in any
way sort of 'violates' the CMake-way);  I've yet to really integrate
this into my project at work, but will be road testing it soon. (I'll
report back, if there's any interest.)

Thanks,
-Michael
-------------- next part --------------
A non-text attachment was scrubbed...
Name: CMakeExample.zip
Type: application/zip
Size: 11368 bytes
Desc: not available
URL: <http://www.cmake.org/pipermail/cmake/attachments/20090308/135529b4/attachment-0001.zip>


More information about the CMake mailing list