Class SharedUtil
- Implementation Note:
- Consumers should make their own
Utilclass and extend this one with it to inherit all static methods.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Transformer<String, JavaLauncher> Transformer to map a Java launcher to its executable path. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Set<Dependency> collect(ConfigurationContainer configurations, SourceSet sourceSet, boolean transitive, Predicate<? super Dependency> dependency) Checks if the given dependency is in the given source set.static booleancontains(ConfigurationContainer configurations, SourceSet sourceSet, boolean transitive, Dependency dependency) Checks if the given dependency is in the given source set.static booleancontains(ConfigurationContainer configurations, SourceSet sourceSet, boolean transitive, Spec<? super Dependency> dependency) Checks if the given dependency is in the given source set.static voidensureAfterEvaluate(Project project, Closure<?> closure) Runs the given closure usingProject.afterEvaluate(Action).static voidensureAfterEvaluate(Project project, Action<? super Project> action) Runs the given action usingProject.afterEvaluate(Action).Makes a returning-self closure that finalizes a given property usingfinalizeProperty(Property).static <P extends Property<?>>
PfinalizeProperty(P property) Finalizes the given property to prevent any additional changes from being made to it.static <T> voidforEach(DomainObjectCollection<T> collection, Action<? super T> action) Iterates through the given collection using the given action.static voidforEachClasspath(ConfigurationContainer configurations, SourceSet sourceSet, Action<? super Configuration> action) Iterates through the given source set's classpath configurations using the given action.static voidforEachClasspathEagerly(ConfigurationContainer configurations, SourceSet sourceSet, Action<? super Configuration> action) Iterates through the given source set's classpath configurations eagerly using the given action.static <T> voidforEachEagerly(DomainObjectCollection<T> collection, Action<? super T> action) Iterates through the given collection eagerly using the given action.static Provider<JavaLauncher> launcherFor(Project project, int version) Gets the Java launcher that can compile or run the given version.static Provider<JavaLauncher> launcherFor(Project project, JavaLanguageVersion version) Gets the Java launcher that can compile or run the given version.static Provider<JavaLauncher> launcherForStrictly(Project project, int version) Gets the Java launcher for the given version, even if the currently running Java toolchain is higher.static Provider<JavaLauncher> launcherForStrictly(Project project, JavaLanguageVersion version) Gets the Java launcher for the given version, even if the currently running Java toolchain is higher.static Provider<JavaLauncher> launcherForStrictly(JavaToolchainService javaToolchains, int version) Gets the Java launcher for the given version, even if the currently running Java toolchain is higher.static Provider<JavaLauncher> launcherForStrictly(JavaToolchainService javaToolchains, JavaLanguageVersion version) Gets the Java launcher for the given version, even if the currently running Java toolchain is higher.static <T extends TaskProvider<?>>
TEnsures that a given task is run first in the task graph for the given project.static <T> voidsetOptional(Property<T> to, Provider<? extends T> from) Conditionally set the given provider's value to the given property's value if the property is present.static PipedOutputStreamCreates an output stream that logs to the given action.static PipedOutputStreamCreates an output stream that logs to the given action.static StringtoString(Dependency dependency) Converts a given dependency to string.static StringtoString(ModuleVersionSelector module) Converts a given module to string.
-
Field Details
-
LAUNCHER_EXECUTABLE
Transformer to map a Java launcher to its executable path. Use to store in properties sinceJavaLaunchercannot be serialized.
-
-
Constructor Details
-
SharedUtil
protected SharedUtil()Empty constructor. This class should only be extended to make referencing these static methods easier.
-
-
Method Details
-
launcherFor
Gets the Java launcher that can compile or run the given version.
If the currently available Java toolchain is able to compile and run the given version, it will be used instead. The toolchain is first queried from the project's
JavaPluginExtension.getToolchain(). If the toolchain is not set or does not apply thejavaplugin,JavaLanguageVersion.current()will be used instead.- Parameters:
project- The project to get the Java extensions fromversion- The version of Java required- Returns:
- A provider for the Java launcher
-
launcherFor
Gets the Java launcher that can compile or run the given version.
If the currently available Java toolchain is able to compile and run the given version, it will be used instead. The toolchain is first queried from the project's
JavaPluginExtension.getToolchain(). If the toolchain is not set or does not apply thejavaplugin,JavaLanguageVersion.current()will be used instead.- Parameters:
project- The project to get the Java extensions fromversion- The version of Java required- Returns:
- A provider for the Java launcher
-
launcherForStrictly
Gets the Java launcher for the given version, even if the currently running Java toolchain is higher.- Parameters:
project- The extension-aware object to get the Java extensions fromversion- The version of Java required- Returns:
- A provider for the Java launcher
-
launcherForStrictly
public static Provider<JavaLauncher> launcherForStrictly(Project project, JavaLanguageVersion version) Gets the Java launcher for the given version, even if the currently running Java toolchain is higher.- Parameters:
project- The extension-aware object to get the Java extensions fromversion- The version of Java required- Returns:
- A provider for the Java launcher
-
launcherForStrictly
public static Provider<JavaLauncher> launcherForStrictly(JavaToolchainService javaToolchains, int version) Gets the Java launcher for the given version, even if the currently running Java toolchain is higher.- Parameters:
javaToolchains- The Java toolchain service to get the Java launcher fromversion- The version of Java required- Returns:
- A provider for the Java launcher
-
launcherForStrictly
public static Provider<JavaLauncher> launcherForStrictly(JavaToolchainService javaToolchains, JavaLanguageVersion version) Gets the Java launcher for the given version, even if the currently running Java toolchain is higher.- Parameters:
javaToolchains- The Java toolchain service to get the Java launcher fromversion- The version of Java required- Returns:
- A provider for the Java launcher
-
ensureAfterEvaluate
public static void ensureAfterEvaluate(Project project, @DelegatesTo(value=Project.class,strategy=1) Closure<?> closure) Runs the given closure usingProject.afterEvaluate(Action). If the project is already executed, the closure will be called instantly.- Parameters:
project- The project to run the closure onclosure- The closure to execute
-
ensureAfterEvaluate
Runs the given action usingProject.afterEvaluate(Action). If the project is already executed, the action will be executed instantly.- Parameters:
project- The project to run the action onaction- The action to execute
-
runFirst
Ensures that a given task is run first in the task graph for the given project.
This does not break the configuration cache as long as the task is always applied using this.
- Type Parameters:
T- The type of task to be run- Parameters:
project- The projecttask- The provider of the task to run first- Returns:
- The task provider
-
contains
public static boolean contains(ConfigurationContainer configurations, SourceSet sourceSet, boolean transitive, Dependency dependency) Checks if the given dependency is in the given source set.- Parameters:
configurations- The configuration container to usesourceSet- The source set to checktransitive- If the source set should be searched transitively (iffalse, for example, this method will returnfalseif the dependency is in themainsource set but not explicitely declared in one of thetestsource set's dependency-scope configurations)dependency- The dependency to find- Returns:
- If the source set contains the dependency
-
contains
public static boolean contains(ConfigurationContainer configurations, SourceSet sourceSet, boolean transitive, Spec<? super Dependency> dependency) Checks if the given dependency is in the given source set.- Parameters:
configurations- The configuration container to usesourceSet- The source set to checktransitive- If the source set should be searched transitively (iffalse, for example, this method will returnfalseif the dependency is in themainsource set but not explicitely declared in one of thetestsource set's dependency-scope configurations)dependency- The dependency to find- Returns:
- If the source set contains the dependency
-
collect
public static Set<Dependency> collect(ConfigurationContainer configurations, SourceSet sourceSet, boolean transitive, Predicate<? super Dependency> dependency) Checks if the given dependency is in the given source set.- Parameters:
configurations- The configuration container to usesourceSet- The source set to checktransitive- If the source set should be searched transitively (iffalse, for example, this method will returnfalseif the dependency is in themainsource set but not explicitely declared in one of thetestsource set's dependency-scope configurations)dependency- The dependency to find- Returns:
- The set containing the filtered dependencies
-
forEachClasspath
public static void forEachClasspath(ConfigurationContainer configurations, SourceSet sourceSet, Action<? super Configuration> action) Iterates through the given source set's classpath configurations using the given action.- Parameters:
configurations- The configuration containersourceSet- The source setaction- The action to run- See Also:
-
forEachClasspathEagerly
public static void forEachClasspathEagerly(ConfigurationContainer configurations, SourceSet sourceSet, Action<? super Configuration> action) Iterates through the given source set's classpath configurations eagerly using the given action.- Parameters:
configurations- The configuration containersourceSet- The source setaction- The action to run- See Also:
-
forEach
Iterates through the given collection using the given action.
This iterator will attempt to use
DomainObjectCollection.configureEach(Action)if it is in an eager context. If it is not, a copy of the collection will be iterated through usingIterable.forEach(Consumer)instead to prevent aConcurrentModificationException.- Type Parameters:
T- The type for the collection- Parameters:
collection- The collection to iterate throughaction- The action to run
-
forEachEagerly
public static <T> void forEachEagerly(DomainObjectCollection<T> collection, Action<? super T> action) Iterates through the given collection eagerly using the given action.
This iterator will iterate over a copy of the collection using
Iterable.forEach(Consumer)to prevent aConcurrentModificationException.- Type Parameters:
T- The type for the collection- Parameters:
collection- The collection to iterate throughaction- The action to run
-
toLog
Creates an output stream that logs to the given action.- Parameters:
logger- The logger to log tolevel- The log level to log at- Returns:
- The output stream
-
toLog
Creates an output stream that logs to the given action.- Parameters:
logger- The logger to log to- Returns:
- The output stream
-
toString
Converts a given module to string. Use this instead ofObject.toString().- Parameters:
module- The module- Returns:
- The string representation
-
toString
Converts a given dependency to string. Use this instead ofObject.toString().- Parameters:
dependency- The dependency- Returns:
- The string representation
-
finalizeProperty
Makes a returning-self closure that finalizes a given property usingfinalizeProperty(Property).This is best used as the method argument for
DefaultGroovyMethods.tap(Object, Closure), which allows for in-lining property creation in Groovy code.- Type Parameters:
P- The type of property to finalize- Returns:
- The returning-self closure for finalizing a property
-
finalizeProperty
@Contract(value="_ -> param1", mutates="param1") public static <P extends Property<?>> P finalizeProperty(P property) Finalizes the given property to prevent any additional changes from being made to it.
This is done by simply calling
HasConfigurableValue.disallowChanges()andHasConfigurableValue.finalizeValueOnRead(). These methods do not return the object itself, so this helper method exists to in-line property creation without needing to reference it again just to call these methods.- Type Parameters:
P- The type of property to finalize- Parameters:
property- The property to finalize- Returns:
- The property
-
setOptional
Conditionally set the given provider's value to the given property's value if the property is present.- Type Parameters:
T- The type of property- Parameters:
to- The property to apply the new value tofrom- The provider value to apply
-