Class GeneratorTask

java.lang.Object
org.gradle.api.internal.AbstractTask
org.gradle.api.DefaultTask
io.github.suppierk.codegen.GeneratorTask
All Implemented Interfaces:
Comparable<org.gradle.api.Task>, org.gradle.api.internal.DynamicObjectAware, org.gradle.api.internal.TaskInternal, org.gradle.api.Named, org.gradle.api.plugins.ExtensionAware, org.gradle.api.Task, org.gradle.util.Configurable<org.gradle.api.Task>

@CacheableTask public class GeneratorTask extends org.gradle.api.DefaultTask
Gradle task that orchestrates Testcontainers database startup, Flyway migrations, and jOOQ code generation for a single configuration.

The task temporarily rewires the JooqConfig JDBC configuration to point at the managed container, runs Flyway within the same class loader that resolves user migrations, and finally delegates to the original JooqGenerate task.

See Also:
  • Nested Class Summary

    Nested classes/interfaces inherited from interface org.gradle.api.Task

    org.gradle.api.Task.Namer
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final DatabaseExtension
    Database definition supplying container overrides and driver selection.
    protected String
    Name of the logical database currently processed (if provided by the DSL).
    protected static final String
    Fallback Flyway location used when no migration locations are defined.
    protected final FlywayConfig
    Flyway configuration provided by the schema DSL.
    protected final nu.studer.gradle.jooq.JooqConfig
    jOOQ configuration currently being processed.
    protected String
    Optional driver override supplied by the DSL.
    protected String
    Name of the schema currently processed (if provided by the DSL).

    Fields inherited from interface org.gradle.api.Task

    TASK_ACTION, TASK_CONSTRUCTOR_ARGS, TASK_DEPENDS_ON, TASK_DESCRIPTION, TASK_GROUP, TASK_NAME, TASK_OVERWRITE, TASK_TYPE
  • Constructor Summary

    Constructors
    Constructor
    Description
    GeneratorTask(DatabaseExtension databaseExtension, FlywayConfig flywayConfig, nu.studer.gradle.jooq.JooqConfig jooqConfig, org.gradle.api.tasks.TaskProvider<nu.studer.gradle.jooq.JooqGenerate> jooqGenerateTask)
    Creates a new task instance bound to a specific combination of database, Flyway configuration and jOOQ configuration.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    applyDslOverrides(String databaseName, String schemaName, String driverClassName)
    Applies schema metadata captured from the DSL.
    static String
    assumeJooqGenerateTaskName(nu.studer.gradle.jooq.JooqConfig jooqConfig)
    Derives the original JooqGenerate task name for the supplied configuration, following the same conventions as JooqPlugin.
    protected final Set<File>
    Gathers the runtime classpath entries that should back Flyway execution.
    void
    Configures task inputs and outputs to make Gradle caching effective.
    protected final URLClassLoader
    Creates a dedicated class loader containing the runtime classpath needed for Flyway execution.
    static String
    createTaskName(nu.studer.gradle.jooq.JooqConfig jooqConfig)
    Computes the Gradle task name used to expose this generator task for a given jOOQ configuration.
    protected final void
    ensureDriverAvailable(String driverClassName, ClassLoader fallbackLoader)
    Ensures the supplied driver class can be resolved via either the current context class loader or the provided fallback.
    org.gradle.api.file.ConfigurableFileCollection
    Runtime classpath Gradle should fingerprint to detect Flyway code changes.
    org.gradle.api.file.ConfigurableFileCollection
    Classpath Gradle should fingerprint for jOOQ runtime changes.
    org.gradle.api.file.ConfigurableFileCollection
    Returns the Flyway migration locations the task uses to decide whether regeneration is required.
    org.gradle.api.file.DirectoryProperty
    Returns the directory Gradle should treat as build output for the generated sources.
    protected final Boolean
    Resolves a boolean property from schema overrides or the base Flyway configuration.
    protected final String
    resolveDriver(String fallbackDriver)
    Resolves the JDBC driver to use, preferring DSL overrides, then Flyway defaults, and finally the existing jOOQ configuration.
    protected final Integer
    Resolves an integer property from schema overrides or the base Flyway configuration.
    protected final List<String>
    Resolves a list property from schema overrides or the base Flyway configuration.
    protected final Map<String,String>
    Resolves a map property from schema overrides or the base Flyway configuration.
    protected final String
    resolveSchema(String fallbackSchema)
    Resolves the schema to target, preferring DSL overrides, then Flyway defaults, and finally the existing jOOQ configuration.
    protected final String
    Resolves a string property from schema overrides or the base Flyway configuration.
    void
    run()
    Task action that: Launches the requested Testcontainers database Runs Flyway migrations against the container Temporarily rewires the JooqConfig JDBC properties to point at the container Executes the original JooqGenerate task Restores the jOOQ configuration
    protected final void
    Runs Flyway migrations using a container supplied by the extension.
    protected final void
    Runs Flyway migrations using a dedicated class loader.
    protected final void
    withDatabase(String driverClassName, io.github.suppierk.codegen.AbstractDatabaseTask.DatabaseAction action)
    Executes the supplied action with a managed database container and dedicated class loader.

    Methods inherited from class org.gradle.api.DefaultTask

    compareTo, configure, dependsOn, doFirst, doFirst, doFirst, doLast, doLast, doLast, finalizedBy, getActions, getAnt, getDependsOn, getDescription, getDestroyables, getDidWork, getEnabled, getExtensions, getFinalizedBy, getGroup, getInputs, getLocalState, getLogger, getLogging, getMustRunAfter, getName, getOutputs, getPath, getProject, getShouldRunAfter, getState, getTaskDependencies, getTemporaryDir, getTimeout, hasProperty, mustRunAfter, onlyIf, onlyIf, onlyIf, property, setActions, setDependsOn, setDescription, setDidWork, setEnabled, setFinalizedBy, setGroup, setMustRunAfter, setOnlyIf, setOnlyIf, setOnlyIf, setProperty, setShouldRunAfter, shouldRunAfter, usesService

    Methods inherited from class org.gradle.api.internal.AbstractTask

    acceptServiceReferences, appendParallelSafeAction, doNotTrackState, getAsDynamicObject, getConvention, getIdentityPath, getImpliesSubProjects, getLifecycleDependencies, getOnlyIf, getReasonNotToTrackState, getReasonTaskIsIncompatibleWithConfigurationCache, getRequiredServices, getServices, getSharedResources, getStandardOutputCapture, getTaskActions, getTaskIdentity, getTemporaryDirFactory, hasTaskActions, injectIntoNewInstance, isCompatibleWithConfigurationCache, isEnabled, isHasCustomActions, notCompatibleWithConfigurationCache, prependParallelSafeAction, setImpliesSubProjects

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface org.gradle.api.Task

    doNotTrackState, getConvention, notCompatibleWithConfigurationCache
  • Field Details

    • FALLBACK_FLYWAY_LOCATION

      protected static final String FALLBACK_FLYWAY_LOCATION
      Fallback Flyway location used when no migration locations are defined.
      See Also:
    • databaseExtension

      @Nonnull protected final DatabaseExtension databaseExtension
      Database definition supplying container overrides and driver selection.
    • flywayConfig

      @Nonnull protected final FlywayConfig flywayConfig
      Flyway configuration provided by the schema DSL.
    • jooqConfig

      @Nonnull protected final nu.studer.gradle.jooq.JooqConfig jooqConfig
      jOOQ configuration currently being processed.
    • overrideDriverClassName

      @Nullable protected String overrideDriverClassName
      Optional driver override supplied by the DSL.
    • schemaName

      @Nullable protected String schemaName
      Name of the schema currently processed (if provided by the DSL).
    • databaseName

      @Nullable protected String databaseName
      Name of the logical database currently processed (if provided by the DSL).
  • Constructor Details

    • GeneratorTask

      @Inject public GeneratorTask(@Nonnull DatabaseExtension databaseExtension, @Nonnull FlywayConfig flywayConfig, @Nonnull nu.studer.gradle.jooq.JooqConfig jooqConfig, @Nonnull org.gradle.api.tasks.TaskProvider<nu.studer.gradle.jooq.JooqGenerate> jooqGenerateTask)
      Creates a new task instance bound to a specific combination of database, Flyway configuration and jOOQ configuration.
      Parameters:
      databaseExtension - database specification coming from the jooqCodegen DSL
      flywayConfig - Flyway configuration that should be applied to this schema
      jooqConfig - original jOOQ configuration
      jooqGenerateTask - provider for the underlying JooqGenerate task
  • Method Details

    • createTaskName

      @Nonnull public static String createTaskName(@Nonnull nu.studer.gradle.jooq.JooqConfig jooqConfig) throws IllegalStateException
      Computes the Gradle task name used to expose this generator task for a given jOOQ configuration.
      Parameters:
      jooqConfig - configuration to inspect
      Returns:
      Gradle task name (e.g. generateMainDatabaseClasses)
      Throws:
      IllegalStateException
      See Also:
    • assumeJooqGenerateTaskName

      @Nonnull public static String assumeJooqGenerateTaskName(@Nonnull nu.studer.gradle.jooq.JooqConfig jooqConfig) throws IllegalStateException
      Derives the original JooqGenerate task name for the supplied configuration, following the same conventions as JooqPlugin.
      Parameters:
      jooqConfig - configuration to inspect
      Returns:
      jOOQ task name (e.g. generateMainJooq)
      Throws:
      IllegalStateException
    • run

      public void run()
      Task action that:
      1. Launches the requested Testcontainers database
      2. Runs Flyway migrations against the container
      3. Temporarily rewires the JooqConfig JDBC properties to point at the container
      4. Executes the original JooqGenerate task
      5. Restores the jOOQ configuration
      See Also:
    • getOutputDirectory

      @OutputDirectory @Nonnull public org.gradle.api.file.DirectoryProperty getOutputDirectory()
      Returns the directory Gradle should treat as build output for the generated sources.
      Returns:
      directory containing generated jOOQ sources
    • getMigrationInputs

      @InputFiles @PathSensitive(RELATIVE) @Nonnull public org.gradle.api.file.ConfigurableFileCollection getMigrationInputs()
      Returns the Flyway migration locations the task uses to decide whether regeneration is required.
      Returns:
      collection of resolved migration directories
    • getJooqRuntimeClasspath

      @Classpath @Nonnull public org.gradle.api.file.ConfigurableFileCollection getJooqRuntimeClasspath()
      Classpath Gradle should fingerprint for jOOQ runtime changes.
      Returns:
      file collection with jOOQ runtime dependencies
    • configureCaching

      public void configureCaching()
      Configures task inputs and outputs to make Gradle caching effective. Captures DSL derived data, Flyway fingerprints, normalized jOOQ configuration hashes, and the migration locations so that Gradle can determine when regeneration is necessary.
      See Also:
      • configureJooqClasspathInput()
      • updateMigrationInputs()
    • applyDslOverrides

      public void applyDslOverrides(@Nonnull String databaseName, @Nonnull String schemaName, @Nullable String driverClassName)
      Applies schema metadata captured from the DSL.
      Parameters:
      databaseName - logical database identifier (used for diagnostics)
      schemaName - logical schema identifier (used for diagnostics)
      driverClassName - optional driver override provided by the DSL
    • getFlywayClasspath

      @Classpath @Nonnull public org.gradle.api.file.ConfigurableFileCollection getFlywayClasspath()
      Runtime classpath Gradle should fingerprint to detect Flyway code changes.
      Returns:
      file collection representing the Flyway runtime classpath
    • runFlywayMigrations

      protected final void runFlywayMigrations(@Nonnull AbstractDatabaseContainer db, @Nonnull String schema)
      Runs Flyway migrations using a container supplied by the extension.
      Parameters:
      db - database container to target
      schema - schema to migrate
    • runFlywayMigrations

      protected final void runFlywayMigrations(@Nonnull AbstractDatabaseContainer db, @Nonnull String schema, @Nonnull URLClassLoader classLoader)
      Runs Flyway migrations using a dedicated class loader.
      Parameters:
      db - database container to target
      schema - schema to migrate
      classLoader - class loader to use when resolving application classes
    • resolveSchema

      @Nonnull protected final String resolveSchema(@Nullable String fallbackSchema)
      Resolves the schema to target, preferring DSL overrides, then Flyway defaults, and finally the existing jOOQ configuration. Throws a detailed exception when the DSL does not identify a schema or when Flyway and jOOQ disagree.
      Parameters:
      fallbackSchema - schema declared in the jOOQ configuration
      Returns:
      resolved schema name
    • resolveDriver

      @Nonnull protected final String resolveDriver(@Nullable String fallbackDriver)
      Resolves the JDBC driver to use, preferring DSL overrides, then Flyway defaults, and finally the existing jOOQ configuration. Throws a descriptive exception when no driver can be found so that users know how to amend their configuration.
      Parameters:
      fallbackDriver - driver declared in the jOOQ configuration
      Returns:
      resolved driver class name
    • ensureDriverAvailable

      protected final void ensureDriverAvailable(@Nonnull String driverClassName, @Nonnull ClassLoader fallbackLoader)
      Ensures the supplied driver class can be resolved via either the current context class loader or the provided fallback. Prevents confusing Testcontainers errors by verifying driver availability up front.
      Parameters:
      driverClassName - fully qualified JDBC driver class name
      fallbackLoader - class loader capable of resolving project dependencies
    • createFlywayClassLoader

      @Nonnull protected final URLClassLoader createFlywayClassLoader()
      Creates a dedicated class loader containing the runtime classpath needed for Flyway execution.
      Returns:
      new URLClassLoader instance
    • withDatabase

      protected final void withDatabase(@Nonnull String driverClassName, @Nonnull io.github.suppierk.codegen.AbstractDatabaseTask.DatabaseAction action)
      Executes the supplied action with a managed database container and dedicated class loader.
      Parameters:
      driverClassName - JDBC driver class to resolve
      action - callback invoked once the container is ready
    • collectFlywayClasspathEntries

      @Nonnull protected final Set<File> collectFlywayClasspathEntries()
      Gathers the runtime classpath entries that should back Flyway execution.
      Returns:
      set of files contributing to the Flyway classpath
    • resolveString

      @Nullable protected final String resolveString(@Nonnull Function<FlywayConfig,String> getter)
      Resolves a string property from schema overrides or the base Flyway configuration.
      Parameters:
      getter - accessor to invoke on the configuration
      Returns:
      resolved value or null
    • resolveList

      @Nonnull protected final List<String> resolveList(@Nonnull Function<FlywayConfig,List<String>> getter)
      Resolves a list property from schema overrides or the base Flyway configuration.
      Parameters:
      getter - accessor to invoke on the configuration
      Returns:
      resolved list (never null)
    • resolveMap

      @Nonnull protected final Map<String,String> resolveMap(@Nonnull Function<FlywayConfig,Map<String,String>> getter)
      Resolves a map property from schema overrides or the base Flyway configuration.
      Parameters:
      getter - accessor to invoke on the configuration
      Returns:
      resolved map combining overrides and defaults
    • resolveInteger

      @Nullable protected final Integer resolveInteger(@Nonnull Function<FlywayConfig,Integer> getter)
      Resolves an integer property from schema overrides or the base Flyway configuration.
      Parameters:
      getter - accessor to invoke on the configuration
      Returns:
      resolved integer or null
    • resolveBoolean

      @Nullable protected final Boolean resolveBoolean(@Nonnull Function<FlywayConfig,Boolean> getter)
      Resolves a boolean property from schema overrides or the base Flyway configuration.
      Parameters:
      getter - accessor to invoke on the configuration
      Returns:
      resolved boolean or null