JVM Utilities
Grolifant offers a number of items to work with JVM-based processes as well. Some of these attempt to hide change in the Gradle API as much as possible.
An instance of it can be obtained via ConfigCacheSafeOptions.jvmTools()
or ProjectOperations.getJvmTools()
.
This instance is configuration cache compatible and can be called from within a task action.
Java Fork Options
There are a number of places in the Gradle API which utilises JvmForkOptions
, but there is no easy way for a plugin provider to create a set of Java options for later usage. For this purpose we have created a version that looks the same and implements most of the methods on the interface.
Here is an example of using it with a Gradle worker configuration.
ConfigurationCacheSafeOperations ccso = ConfigurationCacheSafeOperations.from(project)
JvmForkOptions jfo = ccso.jvmTools().javaForkOptions()
jfo.systemProperties 'a.b.c' : 1
workerExecutor.submit(RunnableWorkImpl.class) { WorkerConfiguration conf ->
forkOptions { org.gradle.process.JavaForkOptions options ->
jfo.copyTo(options)
}
}
Another method is to use GrolifantSimpleJavaForkOptions
which were added in 5.4.
ConfigurationCacheSafeOperations ccso = ConfigurationCacheSafeOperations.from(project)
GrolifantSimpleJavaForkOptions jfo = ccso.jvmTools().simpleJavaForkOptions().tap {
systemProperties( [ 'a.b.c' : 1 ])
}
workerExecutor.submit(RunnableWorkImpl.class) { WorkerConfiguration conf ->
forkOptions { JavaForkOptions options ->
jfo.copyTo(options)
}
}
The advantage of this method is you can present GrolifantSimpleJavaForkOptions
as GrolifantSimpleSetJavaForkOptions
in your code and expose it as part of your DSL.
This will allow build script users to configure the most common fork options in a lazy-evaluated way, which you can then later to use to configure a out-of-process worker, a JavaExec
task, or a javaexec
specification.
Searching the classpath
Sometimes one wants to know in which file a class is located.
This might be important as it might be necessary to put a JAR on a classpath of an external JVM process.
For this purpose there is resolveClassLocation
and it comes in a number of variations.
Variation | Description |
---|---|
|
Simply looks for a class and returns the location which could be a file, a directory or a module. |
|
Searches for a class, then if it is found and it a JAR, look to see if it is also in the substitution path.
If so, use the location from the substitution search.
The pattern is typically the name of a JAR i.e. |
|
As the previous, but offers more control over what needs to be substituted. |
|
As before, but allows specification of what can be ignored. |