UseJava — CMake 4.0.3 Documentation (original) (raw)
This file provides support for Java
. It is assumed thatFindJava has already been loaded. See FindJava for information on how to load Java into your CMake project.
Synopsis¶
Creating and Installing JARS add_jar ( [SOURCES] [...] ...) install_jar ( DESTINATION [COMPONENT ]) install_jni_symlink ( DESTINATION [COMPONENT ])
Header Generation create_javah ((TARGET | GENERATED_FILES ) CLASSES ... ...)
Exporting JAR Targets install_jar_exports (TARGETS ... FILE DESTINATION ...) export_jars (TARGETS ... [NAMESPACE ] FILE )
Finding JARs find_jar ( NAMES [...] [PATHS [... ENV ]] ...)
Creating Java Documentation create_javadoc ( (PACKAGES [...] | FILES [...]) ...)
Creating And Installing JARs¶
add_jar¶
Creates a jar file containing java objects and, optionally, resources:
add_jar( [SOURCES] [...] [...] [RESOURCES NAMESPACE ... [NAMESPACE ...]... ] [INCLUDE_JARS [...]] [ENTRY_POINT ] [VERSION ] [MANIFEST ] [OUTPUT_NAME ] [OUTPUT_DIR ] [GENERATE_NATIVE_HEADERS [DESTINATION (|INSTALL [BUILD ])]] )
This command creates a <target_name>.jar
. It compiles the given<source>
files and adds the given <resource>
files to the jar file. Source files can be java files or listing files (prefixed by @
). If only resource files are given then just a jar file is created.
SOURCES
Compiles the specified source files and adds the result in the jar file.
Added in version 3.4: Support for response files, prefixed by @
.
RESOURCES
Added in version 3.21.
Adds the named <resource>
files to the jar by stripping the source file path and placing the file beneath <ns>
within the jar.
For example:
RESOURCES NAMESPACE "/com/my/namespace" "a/path/to/resource.txt"
results in a resource accessible via /com/my/namespace/resource.txt
within the jar.
Resources may be added without adjusting the namespace by adding them to the list of SOURCES
(original behavior), in this case, resource paths must be relative to CMAKE_CURRENT_SOURCE_DIR
. Adding resources without using the RESOURCES
parameter in out of source builds will almost certainly result in confusion.
Note
Adding resources via the SOURCES
parameter relies upon a hard-coded list of file extensions which are tested to determine whether they compile (e.g. File.java). SOURCES
files which match the extensions are compiled. Files which do not match are treated as resources. To include uncompiled resources matching those file extensions use the RESOURCES
parameter.
INCLUDE_JARS
The list of jars are added to the classpath when compiling the java sources and also to the dependencies of the target. INCLUDE_JARS
also accepts other target names created by add_jar()
. For backwards compatibility, jar files listed as sources are ignored (as they have been since the first version of this module).
ENTRY_POINT
Defines an entry point in the jar file.
VERSION
Adds a version to the target output name.
The following example will create a jar file with the nameshibboleet-1.2.0.jar
and will create a symlink shibboleet.jar
pointing to the jar with the version information.
add_jar(shibboleet shibbotleet.java VERSION 1.2.0)
MANIFEST
Defines a custom manifest for the jar.
OUTPUT_NAME
Specify a different output name for the target.
OUTPUT_DIR
Sets the directory where the jar file will be generated. If not specified,CMAKE_CURRENT_BINARY_DIR is used as the output directory.
GENERATE_NATIVE_HEADERS
Added in version 3.11.
Generates native header files for methods declared as native. These files provide the connective glue that allow your Java and C code to interact. An INTERFACE target will be created for an easy usage of generated files. Sub-option DESTINATION
can be used to specify the output directory for generated header files.
This option requires, at least, version 1.8 of the JDK.
For an optimum usage of this option, it is recommended to include module JNI before any call to add_jar()
. The produced target for native headers can then be used to compile C/C++ sources with thetarget_link_libraries() command.
find_package(JNI) add_jar(foo foo.java GENERATE_NATIVE_HEADERS foo-native) add_library(bar bar.cpp) target_link_libraries(bar PRIVATE foo-native)
Added in version 3.20: DESTINATION
sub-option now supports the possibility to specify different output directories for BUILD
and INSTALL
steps. IfBUILD
directory is not specified, a default directory will be used.
To export the interface target generated by GENERATE_NATIVE_HEADERS
option, sub-option INSTALL
of DESTINATION
is required:
add_jar(foo foo.java GENERATE_NATIVE_HEADERS foo-native DESTINATION INSTALL include) install(TARGETS foo-native EXPORT native) install(DIRECTORY "$<TARGET_PROPERTY:foo-native,NATIVE_HEADERS_DIRECTORY>/" DESTINATION include) install(EXPORT native DESTINATION /to/export NAMESPACE foo)
Some variables can be set to customize the behavior of add_jar()
as well as the java compiler:
CMAKE_JAVA_COMPILE_FLAGS
Specify additional flags to java compiler.
CMAKE_JAVA_INCLUDE_PATH
Specify additional paths to the class path.
CMAKE_JNI_TARGET
If the target is a JNI library, sets this boolean variable to TRUE
to enable creation of a JNI symbolic link (see alsoinstall_jni_symlink()).
CMAKE_JAR_CLASSES_PREFIX
If multiple jars should be produced from the same java source filetree, to prevent the accumulation of duplicate class files in subsequent jars, set/reset CMAKE_JAR_CLASSES_PREFIX
prior to calling the add_jar()
:
set(CMAKE_JAR_CLASSES_PREFIX com/redhat/foo) add_jar(foo foo.java)
set(CMAKE_JAR_CLASSES_PREFIX com/redhat/bar) add_jar(bar bar.java)
The add_jar()
function sets the following target properties on<target_name>
:
INSTALL_FILES
The files which should be installed. This is used byinstall_jar().
JNI_SYMLINK
The JNI symlink which should be installed. This is used byinstall_jni_symlink().
JAR_FILE
The location of the jar file so that you can include it.
CLASSDIR
The directory where the class files can be found. For example to use them with javah
.
NATIVE_HEADERS_DIRECTORY
Added in version 3.20.
The directory where native headers are generated. Defined when optionGENERATE_NATIVE_HEADERS
is specified.
install_jar¶
This command installs the jar file to the given destination:
install_jar( ) install_jar( DESTINATION [COMPONENT ])
This command installs the <target_name>
file to the given<destination>
. It should be called in the same scope asadd_jar() or it will fail.
Added in version 3.4: The second signature with DESTINATION
and COMPONENT
options.
DESTINATION
Specify the directory on disk to which a file will be installed.
COMPONENT
Specify an installation component name with which the install rule is associated, such as "runtime" or "development".
The install_jar()
command sets the following target properties on <target_name>
:
INSTALL_DESTINATION
Holds the <destination>
as described above, and is used byinstall_jar_exports().
install_jni_symlink¶
Installs JNI symlinks for target generated by add_jar():
install_jni_symlink( ) install_jni_symlink( DESTINATION [COMPONENT ])
This command installs the <target_name>
JNI symlinks to the given<destination>
. It should be called in the same scope asadd_jar() or it will fail.
Added in version 3.4: The second signature with DESTINATION
and COMPONENT
options.
DESTINATION
Specify the directory on disk to which a file will be installed.
COMPONENT
Specify an installation component name with which the install rule is associated, such as "runtime" or "development".
Utilize the following commands to create a JNI symbolic link:
set(CMAKE_JNI_TARGET TRUE) add_jar(shibboleet shibbotleet.java VERSION 1.2.0) install_jar(shibboleet ${LIB_INSTALL_DIR}/shibboleet) install_jni_symlink(shibboleet ${JAVA_LIB_INSTALL_DIR})
Exporting JAR Targets¶
install_jar_exports¶
Added in version 3.7.
Installs a target export file:
install_jar_exports(TARGETS ... [NAMESPACE ] FILE DESTINATION [COMPONENT ])
This command installs a target export file <filename>
for the named jar targets to the given <destination>
directory. Its function is similar to that of install(EXPORT).
TARGETS
List of targets created by add_jar() command.
NAMESPACE
Added in version 3.9.
The <namespace>
value will be prepend to the target names as they are written to the import file.
FILE
Specify name of the export file.
DESTINATION
Specify the directory on disk to which a file will be installed.
COMPONENT
Specify an installation component name with which the install rule is associated, such as "runtime" or "development".
export_jars¶
Added in version 3.7.
Writes a target export file:
export_jars(TARGETS ... [NAMESPACE ] FILE )
This command writes a target export file <filename>
for the named <jars>
targets. Its function is similar to that of export().
TARGETS
List of targets created by add_jar() command.
NAMESPACE
Added in version 3.9.
The <namespace>
value will be prepend to the target names as they are written to the import file.
FILE
Specify name of the export file.
Finding JARs¶
find_jar¶
Finds the specified jar file:
find_jar( | NAMES [...] [PATHS [... ENV ]] [VERSIONS []] [DOC "cache documentation string"] )
This command is used to find a full path to the named jar. A cache entry named by <VAR>
is created to store the result of this command. If the full path to a jar is found the result is stored in the variable and the search will not repeated unless the variable is cleared. If nothing is found, the result will be <VAR>-NOTFOUND
, and the search will be attempted again next time find_jar()
is invoked with the same variable.
NAMES
Specify one or more possible names for the jar file.
PATHS
Specify directories to search in addition to the default locations. The ENV
var sub-option reads paths from a system environment variable.
VERSIONS
Specify jar versions.
DOC
Specify the documentation string for the <VAR>
cache entry.
Creating Java Documentation¶
create_javadoc¶
Creates java documentation based on files and packages:
create_javadoc( (PACKAGES [...] | FILES [...]) [SOURCEPATH ] [CLASSPATH ] [INSTALLPATH ] [DOCTITLE ] [WINDOWTITLE ] [AUTHOR (TRUE|FALSE)] [USE (TRUE|FALSE)] [VERSION (TRUE|FALSE)] )
The create_javadoc()
command can be used to create java documentation. There are two main signatures for create_javadoc()
.
The first signature works with package names on a path with source files:
create_javadoc(my_example_doc PACKAGES com.example.foo com.example.bar SOURCEPATH "${CMAKE_CURRENT_SOURCE_DIR}" CLASSPATH ${CMAKE_JAVA_INCLUDE_PATH} WINDOWTITLE "My example" DOCTITLE "
My example
" AUTHOR TRUE USE TRUE VERSION TRUE )The second signature for create_javadoc()
works on a given list of files:
create_javadoc(my_example_doc FILES java/A.java java/B.java CLASSPATH ${CMAKE_JAVA_INCLUDE_PATH} WINDOWTITLE "My example" DOCTITLE "
My example
" AUTHOR TRUE USE TRUE VERSION TRUE )Both signatures share most of the options. For more details please read the javadoc manpage.
PACKAGES
Specify java packages.
FILES
Specify java source files. If relative paths are specified, they are relative to CMAKE_CURRENT_SOURCE_DIR.
SOURCEPATH
Specify the directory where to look for packages. By default,CMAKE_CURRENT_SOURCE_DIR directory is used.
CLASSPATH
Specify where to find user class files. Same behavior as option-classpath
of javadoc
tool.
INSTALLPATH
Specify where to install the java documentation. If you specified, the documentation will be installed to${CMAKE_INSTALL_PREFIX}/share/javadoc/<VAR>
.
DOCTITLE
Specify the title to place near the top of the overview summary file. Same behavior as option -doctitle
of javadoc
tool.
WINDOWTITLE
Specify the title to be placed in the HTML <title>
tag. Same behavior as option -windowtitle
of javadoc
tool.
AUTHOR
When value TRUE
is specified, includes the @author
text in the generated docs. Same behavior as option -author
of javadoc
tool.
USE
When value TRUE
is specified, creates class and package usage pages. Includes one Use page for each documented class and package. Same behavior as option -use
of javadoc
tool.
VERSION
When value TRUE
is specified, includes the version text in the generated docs. Same behavior as option -version
of javadoc
tool.