Title: | Run Java Build Tool Maven from R |
---|---|
Description: | Distributing large files in packages is contrary to 'CRAN' policies. Java libraries frequently have large dependencies, and this makes them hard to submit to CRAN. Java has a sophisticated dependency management tool (Maven) which can calculate and cache dependencies. Running Maven from R allows for better integration of Java libraries into R. Maven provides various options for resolving complied Java code dependencies, configuring class path, or compiling Java source code, all of which are useful for efficient use of Java within R. |
Authors: | Robert Challen [aut, cre] |
Maintainer: | Robert Challen <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.3 |
Built: | 2025-01-10 04:31:54 UTC |
Source: | https://github.com/terminological/rmaven |
Maven coordinates
as.coordinates(groupId, artifactId, version, ...)
as.coordinates(groupId, artifactId, version, ...)
groupId |
the maven |
artifactId |
the maven |
version |
the maven version |
... |
other parameters ignored apart from |
a coordinates object containing the Maven artifact coordinates
as.coordinates("org.junit.jupiter","junit-jupiter-api","4.13.2")
as.coordinates("org.junit.jupiter","junit-jupiter-api","4.13.2")
rmaven
cacheDeletes all content in the rmaven
cache. This should not be necessary, but
never say never, and if there is really a problem with the cache, then
deleting it may be the best thing. This will wait for confirmation from the
user. If running unattended the options("rmaven.allow.cache.delete"=TRUE)
must be set for the action to occur, otherwise it will generate a warning and
do nothing.
clear_rmaven_cache()
clear_rmaven_cache()
nothing, called for side effects
# need to set the following option to allow cache to be deleted in non # interactive session opts = options("rmaven.allow.cache.delete"=TRUE) clear_rmaven_cache() options(opts)
# need to set the following option to allow cache to be deleted in non # interactive session opts = options("rmaven.allow.cache.delete"=TRUE) clear_rmaven_cache() options(opts)
Compilation will package the Java source code in to a Jar file for further
use. It will resolve dependencies and optionally package them into a single
uber jar
(using maven assembly).
compile_jar( path, nocache = FALSE, verbose = c("normal", "quiet", "debug"), with_dependencies = FALSE, ... )
compile_jar( path, nocache = FALSE, verbose = c("normal", "quiet", "debug"), with_dependencies = FALSE, ... )
path |
the path to - either a java source code directory containing a
|
nocache |
normally compilation is only performed if the input has
changed. |
verbose |
how much output from maven, one of "normal", "quiet", "debug" |
with_dependencies |
compile the Java code to a '...-jar-with-dependencies.jar' including transitive dependencies which may be easier to embed into R code as does not need a class path (however may be large if there are a lot of dependencies) |
... |
passed to |
the path to the compiled 'jar' file. If this is a fat jar this can be
passed straight to rJava
, otherwise an additional
resolve_dependencies(...)
call is required
# This code can take quite a while to run as has to # download a lot of plugins, especially on first run path = package_jars("rmaven","src") compile_jar(path,nocache=TRUE) path2 = system.file("testdata/test-project",package = "rmaven") compile_jar(path2,nocache=TRUE,with_dependencies=TRUE)
# This code can take quite a while to run as has to # download a lot of plugins, especially on first run path = package_jars("rmaven","src") compile_jar(path,nocache=TRUE) path2 = system.file("testdata/test-project",package = "rmaven") compile_jar(path2,nocache=TRUE,with_dependencies=TRUE)
This essentially runs a maven-dependency-plugin:copy
goal to copy a JAR
file from (usually) a remote repository to a local directory. The directory
is under the users control but defaults to the .m2
repository.
copy_artifact( groupId = NULL, artifactId = NULL, version = NULL, ..., coordinates = NULL, artifact = NULL, outputDirectory = .working_dir(artifact), repoUrl = .default_repos(), nocache = FALSE, verbose = c("normal", "quiet", "debug") )
copy_artifact( groupId = NULL, artifactId = NULL, version = NULL, ..., coordinates = NULL, artifact = NULL, outputDirectory = .working_dir(artifact), repoUrl = .default_repos(), nocache = FALSE, verbose = c("normal", "quiet", "debug") )
groupId |
optional, the maven |
artifactId |
optional, the maven |
version |
optional, the maven version, |
... |
other maven coordinates such as classifier or packaging |
coordinates |
optional, coordinates as a coordinates object, |
artifact |
optional, coordinates as an artifact string
|
outputDirectory |
optional path, defaults to the |
repoUrl |
the URLs of the repositories to check (defaults to maven
central, |
nocache |
normally artifacts are only fetched if required, |
verbose |
how much output from maven, one of "normal", "quiet", "debug" |
the output of the system2 call. 0 on success.
# This code can take quite a while to run as has to # download a lot of plugins, especially on first run tmp = copy_artifact("org.junit.jupiter","junit-jupiter-api","5.9.0") print(tmp)
# This code can take quite a while to run as has to # download a lot of plugins, especially on first run tmp = copy_artifact("org.junit.jupiter","junit-jupiter-api","5.9.0") print(tmp)
It should be clear that running this function will affect files in the users filesystem.
developer_mode()
developer_mode()
The default Maven directory is located in user space and writing to it is
forbidden by CRAN policies. This plugin is set up to use a cache directory
for the local Maven repository but if you are developing Java code and using
it in R then your Java build tools will be installing content to the default
Maven directory, and not the CRAN sanctioned cache. Thus rmaven
wont by
default be able to pick up jar files locally installed through standard Java
tooling. This function sets the rmaven
repository location to the Maven
standard location allowing local jar files to be used. Obviously this is not
portable until the Java packages are deployed to a Maven repository and is
only an useful option during development.
nothing. called for side effects
# set the repository location to the usual location for Java development # developer_mode() # We don't run this above example as it creates an empty directory in the # userspace and doing so in an example violates CRAN principles.
# set the repository location to the usual location for Java development # developer_mode() # We don't run this above example as it creates an empty directory in the # userspace and doing so in an example violates CRAN principles.
Maven goals are defined either as life-cycle goals (e.g. "clean", "compile")
or as plugin goals (e.g. "help:system"). Some Maven goals may be executed
without a pom.xml
file, others require one. Some maven goals (e.g.
compilation) require the use of a JDK
.
execute_maven( goal, opts = c(), pom_path = NULL, quiet = .quietly(verbose), debug = .debug(verbose), verbose = c("normal", "debug", "quiet"), require_jdk = FALSE, settings = .settings_path(), ... )
execute_maven( goal, opts = c(), pom_path = NULL, quiet = .quietly(verbose), debug = .debug(verbose), verbose = c("normal", "debug", "quiet"), require_jdk = FALSE, settings = .settings_path(), ... )
goal |
the goal of the |
opts |
provided options in the form
|
pom_path |
optional. the path to a |
quiet |
should output from maven be suppressed? ( |
debug |
should output from maven be verbose? ( |
verbose |
how much output from maven, one of "normal", "quiet", "debug" |
require_jdk |
does the goal you are executing require a |
settings |
the path to a |
... |
non-empty named parameters are passed to maven as options in the
form |
nothing, invisibly
# This code can take quite a while to run as has to # download a lot of plugins, especially on first run on a clean system execute_maven("help:system")
# This code can take quite a while to run as has to # download a lot of plugins, especially on first run on a clean system execute_maven("help:system")
.m2
repositoryThis can be used to get a JAR file from the maven repositories into a
local .m2
repository. The local path is made available for importing it into
the rJava
classpath for example.
fetch_artifact( groupId = NULL, artifactId = NULL, version = NULL, ..., coordinates = NULL, artifact = NULL, repoUrl = .default_repos(), nocache = FALSE, verbose = c("normal", "quiet", "debug") )
fetch_artifact( groupId = NULL, artifactId = NULL, version = NULL, ..., coordinates = NULL, artifact = NULL, repoUrl = .default_repos(), nocache = FALSE, verbose = c("normal", "quiet", "debug") )
groupId |
optional, the maven |
artifactId |
optional, the maven |
version |
optional, the maven version, |
... |
other maven coordinates such as classifier or packaging |
coordinates |
optional, but if not supplied |
artifact |
optional, coordinates as an artifact string
|
repoUrl |
the URLs of the repositories to check (defaults to Maven
central, 'Sonatype' snapshots and 'jitpack', defined in
|
nocache |
normally artifacts are only fetched if required, |
verbose |
how much output from maven, one of "normal", "quiet", "debug" |
the path of the artifact within the local maven cache
# This code can take quite a while to run as has to # download a lot of plugins, especially on first run fetch_artifact(artifact="com.google.guava:guava:31.1-jre") fetch_artifact(coordinates = as.coordinates("org.junit.jupiter", "junit-jupiter-api","5.9.0"))
# This code can take quite a while to run as has to # download a lot of plugins, especially on first run fetch_artifact(artifact="com.google.guava:guava:31.1-jre") fetch_artifact(coordinates = as.coordinates("org.junit.jupiter", "junit-jupiter-api","5.9.0"))
In general this function is mainly for internal use but maybe handy for
debugging. The maven repository location can be defined by
set_repository_location(...)
or through the option
options("rmaven.m2.repository"=...)
option but defaults to a
.m2/repository
directory in the rmaven
cache directory. This is not the
default location for Maven when used from Java writing to the default Maven
directory in user space is forbidden by CRAN policies. The result of this is
that rmaven
will have to unnecessarily download additional copies of Java
libraries, onto the users computer and cannot re-use already cached copies.
Also Maven wont be able to pick up jar files locally installed through
standard Java tooling, unless the default CRAN approved location is overridden
using set_repository_location("~/.m2/repository")
.
This is more of an issue for developers rather than users.
get_repository_location(settings_path = .settings_path())
get_repository_location(settings_path = .settings_path())
settings_path |
the file path of the |
the location of the maven repository
# the default location: get_repository_location() # change the location to the Java default. This change will not persist between sessions. opt = options("rmaven.m2.repository"=paste0(tempdir(),"/.m2/repository/")) set_repository_location() get_repository_location() # revert to rmaven defaults options(opt) set_repository_location()
# the default location: get_repository_location() # change the location to the Java default. This change will not persist between sessions. opt = options("rmaven.m2.repository"=paste0(tempdir(),"/.m2/repository/")) set_repository_location() get_repository_location() # revert to rmaven defaults options(opt) set_repository_location()
Find location of some or all of the jars in a particular package.
package_jars( package_name, types = c("all", "thin-jar", "fat-jar", "shaded", "src") )
package_jars( package_name, types = c("all", "thin-jar", "fat-jar", "shaded", "src") )
package_name |
the R package name |
types |
the jar types to look for in the package: one of
|
a vector of paths to jar files in the package
package_jars("rmaven") package_jars("rmaven","thin-jar")
package_jars("rmaven") package_jars("rmaven","thin-jar")
Prints a coordinates object
## S3 method for class 'coordinates' print(x, ...)
## S3 method for class 'coordinates' print(x, ...)
x |
a maven coordinates object |
... |
ignored |
nothing. for side effects.
print(as.coordinates("org.junit.jupiter","junit-jupiter-api","4.13.2"))
print(as.coordinates("org.junit.jupiter","junit-jupiter-api","4.13.2"))
classpath
for an artifact.This function makes sure the transitive dependencies for a maven artifact are
available locally in the .m2
maven cache and calculates a local classpath
which can be provided to rJava
. The artifact may be specified either as a
set of maven coordinates (in which case the artifact itself is also
downloaded, and included in the classpath
) or as a path to a jar file
containing a pom.xml (e.g. a compiled jar file, a compiled
...-jar-with-dependencies
, or a assembled ...-src.jar
).
resolve_dependencies( groupId = NULL, artifactId = NULL, version = NULL, ..., coordinates = NULL, artifact = NULL, path = NULL, include_self = NULL, nocache = FALSE, verbose = c("normal", "quiet", "debug") )
resolve_dependencies( groupId = NULL, artifactId = NULL, version = NULL, ..., coordinates = NULL, artifact = NULL, path = NULL, include_self = NULL, nocache = FALSE, verbose = c("normal", "quiet", "debug") )
groupId |
the maven |
artifactId |
the maven |
version |
the maven version, optional |
... |
passed on to as.coordinates() |
coordinates |
the maven coordinates, optional (either
|
artifact |
optional, coordinates as an artifact string
|
path |
the path to the source directory, pom file or jar file. if not
given |
include_self |
do you want include this path in the |
nocache |
do not used cached version, by default we use a cached version
of the |
verbose |
how much output from maven, one of "normal", "quiet", "debug" |
a character vector of the classpath
jar files (including the
current one if appropriate)
# This code can take quite a while to run as has # to download a lot of plugins, especially on first run # classpath would be cached if possible resolve_dependencies(groupId = "commons-io", artifactId = "commons-io", version="2.11.0") # forcing download and classpath calculation of an artifact resolve_dependencies(artifact = "org.junit.jupiter:junit-jupiter-api:5.9.0", nocache=TRUE) # find the test jar in this package and calculate its stated dependencies resolve_dependencies(path= system.file("testdata/test-project-0.0.1-SNAPSHOT.jar",package="rmaven")) # find the test source code jar in this package and calculate its stated # dependencies resolve_dependencies(path= system.file("testdata/test-project-0.0.1-SNAPSHOT-src.jar", package="rmaven") )
# This code can take quite a while to run as has # to download a lot of plugins, especially on first run # classpath would be cached if possible resolve_dependencies(groupId = "commons-io", artifactId = "commons-io", version="2.11.0") # forcing download and classpath calculation of an artifact resolve_dependencies(artifact = "org.junit.jupiter:junit-jupiter-api:5.9.0", nocache=TRUE) # find the test jar in this package and calculate its stated dependencies resolve_dependencies(path= system.file("testdata/test-project-0.0.1-SNAPSHOT.jar",package="rmaven")) # find the test source code jar in this package and calculate its stated # dependencies resolve_dependencies(path= system.file("testdata/test-project-0.0.1-SNAPSHOT-src.jar", package="rmaven") )
This writes a maven repository location to a temporary settings.xml
file
which persists only for the R session. The location of the maven repository
is either specified here, or can be defined by the
options("rmaven.m2.repository"=...)
option. If neither of these is
provided, the location will revert to a default location within the rmaven
cache. (Approved by CRAN for a local cache location) e.g. on 'Linux' this
will default to ~/.cache/rmaven/.m2/repository/
set_repository_location( repository_location = getOption("rmaven.m2.repository", default = .working_dir(subpath = ".m2/repository/")), settings_path = .settings_path() )
set_repository_location( repository_location = getOption("rmaven.m2.repository", default = .working_dir(subpath = ".m2/repository/")), settings_path = .settings_path() )
repository_location |
a file path (which will be expanded to a full
path) where the repository should be based, e.g. |
settings_path |
the file path of the settings.xml to update (generally the supplied default is what you want to use) |
the expanded path of the new repository location
# Setting the repository to be a temp dir as an example: set_repository_location(paste0(tempdir(), "/.m2/repository")) # you would never want to do this in real life as then the maven repository # would be rebuilt on every new R session. # set the repository location to the usual location for Java development # set_repository_location("~/.m2/repository") # We don't run this above example as it creates an empty directory in the # userspace and doing so in an example violates CRAN principles. # set the repository location back to the CRAN approved default location set_repository_location()
# Setting the repository to be a temp dir as an example: set_repository_location(paste0(tempdir(), "/.m2/repository")) # you would never want to do this in real life as then the maven repository # would be rebuilt on every new R session. # set the repository location to the usual location for Java development # set_repository_location("~/.m2/repository") # We don't run this above example as it creates an empty directory in the # userspace and doing so in an example violates CRAN principles. # set the repository location back to the CRAN approved default location set_repository_location()
rJava
JVM
with or without debugging optionsThis does not do anything if the JVM
has already been started. Otherwise
starts the JVM via rJava
with a set of options Additional JVM options
(beyond debugging) can be set with the
options("java.parameters"=c("-Xprof","-Xrunhprof"))
start_jvm( debug = FALSE, quiet = getOption("rmaven.quiet", TRUE), max_heap = NULL, thread_stack = NULL, ... )
start_jvm( debug = FALSE, quiet = getOption("rmaven.quiet", TRUE), max_heap = NULL, thread_stack = NULL, ... )
debug |
turn on debugging |
quiet |
don't report messages (defaults to |
max_heap |
optional. if a string like |
thread_stack |
optional. sensible values range from '1m' to '128m' (max is '1g'). Can be important with deeply nested structures. |
... |
any other named parameters are passed as |
nothing - called for side effects
start_jvm() ## Not run: # this may try to rebind debugging port start_jvm(debug = TRUE) ## End(Not run)
start_jvm() ## Not run: # this may try to rebind debugging port start_jvm(debug = TRUE) ## End(Not run)