Class SharedUtil

java.lang.Object
net.minecraftforge.gradleutils.shared.SharedUtil

public abstract class SharedUtil extends Object
Shared utilities for Gradle plugins.
Implementation Note:
Consumers should make their own Util class and extend this one with it to inherit all static methods.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final org.gradle.api.Transformer<String, org.gradle.jvm.toolchain.JavaLauncher>
    Transformer to map a Java launcher to its executable path.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Empty constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    static Set<org.gradle.api.artifacts.Dependency>
    collect(org.gradle.api.artifacts.ConfigurationContainer configurations, org.gradle.api.tasks.SourceSet sourceSet, boolean transitive, Predicate<? super org.gradle.api.artifacts.Dependency> dependency)
    Checks if the given dependency is in the given source set.
    static boolean
    contains(org.gradle.api.artifacts.ConfigurationContainer configurations, org.gradle.api.tasks.SourceSet sourceSet, boolean transitive, org.gradle.api.artifacts.Dependency dependency)
    Checks if the given dependency is in the given source set.
    static boolean
    contains(org.gradle.api.artifacts.ConfigurationContainer configurations, org.gradle.api.tasks.SourceSet sourceSet, boolean transitive, org.gradle.api.specs.Spec<? super org.gradle.api.artifacts.Dependency> dependency)
    Checks if the given dependency is in the given source set.
    static void
    ensureAfterEvaluate(org.gradle.api.Project project, groovy.lang.Closure<?> closure)
    Runs the given closure using Project.afterEvaluate(Action).
    static void
    ensureAfterEvaluate(org.gradle.api.Project project, org.gradle.api.Action<? super org.gradle.api.Project> action)
    Runs the given action using Project.afterEvaluate(Action).
    static <P extends org.gradle.api.provider.Property<?>>
    groovy.lang.Closure<P>
    Makes a returning-self closure that finalizes a given property using finalizeProperty(Property).
    static <P extends org.gradle.api.provider.Property<?>>
    P
    finalizeProperty(P property)
    Finalizes the given property to prevent any additional changes from being made to it.
    static <T> void
    forEach(org.gradle.api.DomainObjectCollection<T> collection, org.gradle.api.Action<? super T> action)
    Iterates through the given collection using the given action.
    static void
    forEachClasspath(org.gradle.api.artifacts.ConfigurationContainer configurations, org.gradle.api.tasks.SourceSet sourceSet, org.gradle.api.Action<? super org.gradle.api.artifacts.Configuration> action)
    Iterates through the given source set's classpath configurations using the given action.
    static void
    forEachClasspathEagerly(org.gradle.api.artifacts.ConfigurationContainer configurations, org.gradle.api.tasks.SourceSet sourceSet, org.gradle.api.Action<? super org.gradle.api.artifacts.Configuration> action)
    Iterates through the given source set's classpath configurations eagerly using the given action.
    static <T> void
    forEachEagerly(org.gradle.api.DomainObjectCollection<T> collection, org.gradle.api.Action<? super T> action)
    Iterates through the given collection eagerly using the given action.
    static org.gradle.api.provider.Provider<org.gradle.jvm.toolchain.JavaLauncher>
    launcherFor(org.gradle.api.Project project, int version)
    Gets the Java launcher that can compile or run the given version.
    static org.gradle.api.provider.Provider<org.gradle.jvm.toolchain.JavaLauncher>
    launcherFor(org.gradle.api.Project project, org.gradle.jvm.toolchain.JavaLanguageVersion version)
    Gets the Java launcher that can compile or run the given version.
    static org.gradle.api.provider.Provider<org.gradle.jvm.toolchain.JavaLauncher>
    launcherForStrictly(org.gradle.api.Project project, int version)
    Gets the Java launcher for the given version, even if the currently running Java toolchain is higher.
    static org.gradle.api.provider.Provider<org.gradle.jvm.toolchain.JavaLauncher>
    launcherForStrictly(org.gradle.api.Project project, org.gradle.jvm.toolchain.JavaLanguageVersion version)
    Gets the Java launcher for the given version, even if the currently running Java toolchain is higher.
    static org.gradle.api.provider.Provider<org.gradle.jvm.toolchain.JavaLauncher>
    launcherForStrictly(org.gradle.jvm.toolchain.JavaToolchainService javaToolchains, int version)
    Gets the Java launcher for the given version, even if the currently running Java toolchain is higher.
    static org.gradle.api.provider.Provider<org.gradle.jvm.toolchain.JavaLauncher>
    launcherForStrictly(org.gradle.jvm.toolchain.JavaToolchainService javaToolchains, org.gradle.jvm.toolchain.JavaLanguageVersion version)
    Gets the Java launcher for the given version, even if the currently running Java toolchain is higher.
    static String
    pathify(org.gradle.api.artifacts.Dependency dependency)
    Converts a given dependency to a relative path, accounting for the possible nullable group and version.
    static <T extends org.gradle.api.tasks.TaskProvider<?>>
    T
    runFirst(org.gradle.api.Project project, T task)
    Ensures that a given task is run first in the task graph for the given project.
    static <T> void
    setOptional(org.gradle.api.provider.Property<T> to, org.gradle.api.provider.Provider<? extends T> from)
    Conditionally set the given provider's value to the given property's value if the property is present.
    toLog(Consumer<? super String> logger)
    Creates an output stream that logs to the given action.
    toLog(org.gradle.api.logging.Logger logger, org.gradle.api.logging.LogLevel level)
    Creates an output stream that logs to the given action.
    static String
    toString(org.gradle.api.artifacts.Dependency dependency)
    Converts a given dependency to string.
    static String
    toString(org.gradle.api.artifacts.ModuleVersionSelector module)
    Converts a given module to string.
    static <T> T
    unpack(groovy.lang.Closure<T> value)
    Unpacks a deferred value.
    static <T> T
    unpack(Object value)
    Unpacks a deferred value.
    static <T> T
    unpack(Callable<T> value)
    Unpacks a deferred value.
    static <T> T
    unpack(Supplier<T> value)
    Unpacks a deferred value.
    static <T> T
    unpack(kotlin.jvm.functions.Function0<T> value)
    Unpacks a deferred value.
    static <T> T
    unpack(org.gradle.api.provider.Provider<T> value)
    Unpacks a deferred value.
    static <T> T
    unpack(org.gradle.api.provider.ProviderConvertible<T> value)
    Unpacks a deferred value.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • LAUNCHER_EXECUTABLE

      public static final org.gradle.api.Transformer<String, org.gradle.jvm.toolchain.JavaLauncher> LAUNCHER_EXECUTABLE
      Transformer to map a Java launcher to its executable path. Use to store in properties since JavaLauncher cannot 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

      public static org.gradle.api.provider.Provider<org.gradle.jvm.toolchain.JavaLauncher> launcherFor(org.gradle.api.Project project, int version)

      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 the java plugin, JavaLanguageVersion.current() will be used instead.

      Parameters:
      project - The project to get the Java extensions from
      version - The version of Java required
      Returns:
      A provider for the Java launcher
    • launcherFor

      public static org.gradle.api.provider.Provider<org.gradle.jvm.toolchain.JavaLauncher> launcherFor(org.gradle.api.Project project, org.gradle.jvm.toolchain.JavaLanguageVersion version)

      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 the java plugin, JavaLanguageVersion.current() will be used instead.

      Parameters:
      project - The project to get the Java extensions from
      version - The version of Java required
      Returns:
      A provider for the Java launcher
    • launcherForStrictly

      public static org.gradle.api.provider.Provider<org.gradle.jvm.toolchain.JavaLauncher> launcherForStrictly(org.gradle.api.Project project, int 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 from
      version - The version of Java required
      Returns:
      A provider for the Java launcher
    • launcherForStrictly

      public static org.gradle.api.provider.Provider<org.gradle.jvm.toolchain.JavaLauncher> launcherForStrictly(org.gradle.api.Project project, org.gradle.jvm.toolchain.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 from
      version - The version of Java required
      Returns:
      A provider for the Java launcher
    • launcherForStrictly

      public static org.gradle.api.provider.Provider<org.gradle.jvm.toolchain.JavaLauncher> launcherForStrictly(org.gradle.jvm.toolchain.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 from
      version - The version of Java required
      Returns:
      A provider for the Java launcher
    • launcherForStrictly

      public static org.gradle.api.provider.Provider<org.gradle.jvm.toolchain.JavaLauncher> launcherForStrictly(org.gradle.jvm.toolchain.JavaToolchainService javaToolchains, org.gradle.jvm.toolchain.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 from
      version - The version of Java required
      Returns:
      A provider for the Java launcher
    • ensureAfterEvaluate

      public static void ensureAfterEvaluate(org.gradle.api.Project project, @DelegatesTo(value=org.gradle.api.Project.class,strategy=1) groovy.lang.Closure<?> closure)
      Runs the given closure using Project.afterEvaluate(Action). If the project is already executed, the closure will be called instantly.
      Parameters:
      project - The project to run the closure on
      closure - The closure to execute
    • ensureAfterEvaluate

      public static void ensureAfterEvaluate(org.gradle.api.Project project, org.gradle.api.Action<? super org.gradle.api.Project> action)
      Runs the given action using Project.afterEvaluate(Action). If the project is already executed, the action will be executed instantly.
      Parameters:
      project - The project to run the action on
      action - The action to execute
    • runFirst

      public static <T extends org.gradle.api.tasks.TaskProvider<?>> T runFirst(org.gradle.api.Project project, T task)

      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 project
      task - The provider of the task to run first
      Returns:
      The task provider
    • contains

      public static boolean contains(org.gradle.api.artifacts.ConfigurationContainer configurations, org.gradle.api.tasks.SourceSet sourceSet, boolean transitive, org.gradle.api.artifacts.Dependency dependency)
      Checks if the given dependency is in the given source set.
      Parameters:
      configurations - The configuration container to use
      sourceSet - The source set to check
      transitive - If the source set should be searched transitively (if false, for example, this method will return false if the dependency is in the main source set but not explicitely declared in one of the test source set's dependency-scope configurations)
      dependency - The dependency to find
      Returns:
      If the source set contains the dependency
    • contains

      public static boolean contains(org.gradle.api.artifacts.ConfigurationContainer configurations, org.gradle.api.tasks.SourceSet sourceSet, boolean transitive, org.gradle.api.specs.Spec<? super org.gradle.api.artifacts.Dependency> dependency)
      Checks if the given dependency is in the given source set.
      Parameters:
      configurations - The configuration container to use
      sourceSet - The source set to check
      transitive - If the source set should be searched transitively (if false, for example, this method will return false if the dependency is in the main source set but not explicitely declared in one of the test source set's dependency-scope configurations)
      dependency - The dependency to find
      Returns:
      If the source set contains the dependency
    • collect

      public static Set<org.gradle.api.artifacts.Dependency> collect(org.gradle.api.artifacts.ConfigurationContainer configurations, org.gradle.api.tasks.SourceSet sourceSet, boolean transitive, Predicate<? super org.gradle.api.artifacts.Dependency> dependency)
      Checks if the given dependency is in the given source set.
      Parameters:
      configurations - The configuration container to use
      sourceSet - The source set to check
      transitive - If the source set should be searched transitively (if false, for example, this method will return false if the dependency is in the main source set but not explicitely declared in one of the test source set's dependency-scope configurations)
      dependency - The dependency to find
      Returns:
      The set containing the filtered dependencies
    • forEachClasspath

      public static void forEachClasspath(org.gradle.api.artifacts.ConfigurationContainer configurations, org.gradle.api.tasks.SourceSet sourceSet, org.gradle.api.Action<? super org.gradle.api.artifacts.Configuration> action)
      Iterates through the given source set's classpath configurations using the given action.
      Parameters:
      configurations - The configuration container
      sourceSet - The source set
      action - The action to run
      See Also:
    • forEachClasspathEagerly

      public static void forEachClasspathEagerly(org.gradle.api.artifacts.ConfigurationContainer configurations, org.gradle.api.tasks.SourceSet sourceSet, org.gradle.api.Action<? super org.gradle.api.artifacts.Configuration> action)
      Iterates through the given source set's classpath configurations eagerly using the given action.
      Parameters:
      configurations - The configuration container
      sourceSet - The source set
      action - The action to run
      See Also:
    • forEach

      public static <T> void forEach(org.gradle.api.DomainObjectCollection<T> collection, org.gradle.api.Action<? super T> action)

      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 using Iterable.forEach(Consumer) instead to prevent a ConcurrentModificationException.

      Type Parameters:
      T - The type for the collection
      Parameters:
      collection - The collection to iterate through
      action - The action to run
    • forEachEagerly

      public static <T> void forEachEagerly(org.gradle.api.DomainObjectCollection<T> collection, org.gradle.api.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 a ConcurrentModificationException.

      Type Parameters:
      T - The type for the collection
      Parameters:
      collection - The collection to iterate through
      action - The action to run
    • toLog

      public static PipedOutputStream toLog(org.gradle.api.logging.Logger logger, org.gradle.api.logging.LogLevel level)
      Creates an output stream that logs to the given action.
      Parameters:
      logger - The logger to log to
      level - The log level to log at
      Returns:
      The output stream
    • toLog

      public static PipedOutputStream toLog(Consumer<? super String> logger)
      Creates an output stream that logs to the given action.
      Parameters:
      logger - The logger to log to
      Returns:
      The output stream
    • toString

      public static String toString(org.gradle.api.artifacts.ModuleVersionSelector module)
      Converts a given module to string. Use this instead of Object.toString().
      Parameters:
      module - The module
      Returns:
      The string representation
    • toString

      public static String toString(org.gradle.api.artifacts.Dependency dependency)
      Converts a given dependency to string. Use this instead of Object.toString().
      Parameters:
      dependency - The dependency
      Returns:
      The string representation
    • pathify

      public static String pathify(org.gradle.api.artifacts.Dependency dependency)
      Converts a given dependency to a relative path, accounting for the possible nullable group and version.
      Parameters:
      dependency - The dependency to pathify
      Returns:
      The pathified dependency
    • unpack

      public static <T> T unpack(org.gradle.api.provider.Provider<T> value)
      Unpacks a deferred value.
      Type Parameters:
      T - The type of value held by the provider
      Parameters:
      value - The value to unpack
      Returns:
      The unpacked value
      See Also:
    • unpack

      public static <T> T unpack(org.gradle.api.provider.ProviderConvertible<T> value)
      Unpacks a deferred value.
      Type Parameters:
      T - The type of value held by the provider
      Parameters:
      value - The value to unpack
      Returns:
      The unpacked value
      See Also:
    • unpack

      public static <T> T unpack(groovy.lang.Closure<T> value)
      Unpacks a deferred value.
      Type Parameters:
      T - The type of value held by the provider
      Parameters:
      value - The value to unpack
      Returns:
      The unpacked value
      See Also:
    • unpack

      public static <T> T unpack(Callable<T> value)
      Unpacks a deferred value.
      Type Parameters:
      T - The type of value held by the provider
      Parameters:
      value - The value to unpack
      Returns:
      The unpacked value
      See Also:
    • unpack

      public static <T> T unpack(kotlin.jvm.functions.Function0<T> value)
      Unpacks a deferred value.
      Type Parameters:
      T - The type of value held by the provider
      Parameters:
      value - The value to unpack
      Returns:
      The unpacked value
      See Also:
    • unpack

      public static <T> T unpack(Supplier<T> value)
      Unpacks a deferred value.
      Type Parameters:
      T - The type of value held by the provider
      Parameters:
      value - The value to unpack
      Returns:
      The unpacked value
      See Also:
    • unpack

      public static <T> T unpack(Object value)

      Unpacks a deferred value.

      Since buildscripts are dynamically compiled, this allows buildscript authors to use this method with version catalog entries, other provider-like objects. This prevents the need to arbitrarily call Provider.get() (or similar) on values which may or may not be deferred based on circumstance.

      Type Parameters:
      T - The type of value held by the provider
      Parameters:
      value - The value to unpack
      Returns:
      The unpacked value
    • finalizeProperty

      public static <P extends org.gradle.api.provider.Property<?>> groovy.lang.Closure<P> finalizeProperty()
      Makes a returning-self closure that finalizes a given property using finalizeProperty(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 org.gradle.api.provider.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() and HasConfigurableValue.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

      public static <T> void setOptional(org.gradle.api.provider.Property<T> to, org.gradle.api.provider.Provider<? extends T> from)
      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 to
      from - The provider value to apply