Class CompileKotlinOperation


@NullMarked public class CompileKotlinOperation extends AbstractOperation<CompileKotlinOperation>
Compiles main and test Kotlin sources in the relevant build directories.
Since:
1.0
Author:
Erik C. Thauvin
  • Constructor Details

    • CompileKotlinOperation

      public CompileKotlinOperation()
  • Method Details

    • execute

      public void execute() throws Exception
      Performs the compile operation.
      Specified by:
      execute in class AbstractOperation<CompileKotlinOperation>
      Throws:
      NullPointerException - if project or workDir is null
      Exception - when an exception occurs during the execution
    • findKotlinCompilerPath

      public static String findKotlinCompilerPath()
      Locates the Kotlin compiler (kotlinc) executable.
      Returns:
      The path to the ekotlinc executable, or kotlinc/kotlinc.bat if not found.
      Since:
      1.1.0
    • findKotlinCompilerPath

      protected static String findKotlinCompilerPath(boolean isSilent)
      Locates the Kotlin compiler (kotlinc) executable.
      Parameters:
      isSilent - do not log the path to the kotlinc executable, if true
      Returns:
      The path to the kotlinc executable, or kotlinc/kotlinc.bat if not found.
      Since:
      1.1.0
    • buildMainDirectory

      public CompileKotlinOperation buildMainDirectory(Path directory)
      Provides the main build destination directory.
      Parameters:
      directory - the directory to use for the main build destination
      Returns:
      this operation instance
      Throws:
      NullPointerException - if directory is null
    • buildMainDirectory

      public CompileKotlinOperation buildMainDirectory(File directory)
      Provides the main build destination directory.
      Parameters:
      directory - the directory to use for the main build destination
      Returns:
      this operation instance
      Throws:
      NullPointerException - if directory is null
    • buildMainDirectory

      public CompileKotlinOperation buildMainDirectory(String directory)
      Provides the main build destination directory.
      Parameters:
      directory - the directory to use for the main build destination
      Returns:
      this operation instance
      Throws:
      NullPointerException - if directory is null
      IllegalArgumentException - if directory is blank
    • buildMainDirectory

      public @Nullable File buildMainDirectory()
      Retrieves the main build destination directory.
      Returns:
      the main build directory
    • buildTestDirectory

      public CompileKotlinOperation buildTestDirectory(File directory)
      Provides the test build destination directory.
      Parameters:
      directory - the directory to use for the test build destination
      Returns:
      this operation instance
      Throws:
      NullPointerException - if directory is null
    • buildTestDirectory

      public CompileKotlinOperation buildTestDirectory(Path directory)
      Provides the test build destination directory.
      Parameters:
      directory - the directory to use for the test build destination
      Returns:
      this operation instance
      Throws:
      NullPointerException - if directory is null
    • buildTestDirectory

      public CompileKotlinOperation buildTestDirectory(String directory)
      Provides the test build destination directory.
      Parameters:
      directory - the directory to use for the test build destination
      Returns:
      this operation instance
      Throws:
      NullPointerException - if directory is null
      IllegalArgumentException - if directory is blank
    • buildTestDirectory

      public @Nullable File buildTestDirectory()
      Retrieves the test build destination directory.
      Returns:
      the test build directory
    • compileMainClasspath

      public CompileKotlinOperation compileMainClasspath(String... classpath)
      Provides entries for the main compilation classpath.
      Parameters:
      classpath - one or more classpath entries
      Returns:
      this operation instance
      Throws:
      NullPointerException - if classpath is null or contains null elements
      IllegalArgumentException - if classpath is empty, or contains blank elements
      See Also:
    • compileMainClasspath

      public final CompileKotlinOperation compileMainClasspath(Collection<String> classpath)
      Provides the entries for the main compilation classpath.
      Parameters:
      classpath - the classpath entries
      Returns:
      this operation instance
      Throws:
      NullPointerException - if classpath is null or contains null elements
      IllegalArgumentException - if classpath is empty, or contains blank elements
    • compileMainClasspath

      public Set<String> compileMainClasspath()
      Retrieves the entries for the main compilation classpath.
      Returns:
      the classpath entries
    • compileOptions

      public @Nullable CompileOptions compileOptions()
      Retrieves the compilation options for the compiler.
      Returns:
      the compilation options
    • compileOptions

      public CompileKotlinOperation compileOptions(CompileOptions options)
      Provides the compilation options to pass to the Kotlin compiler.
      Parameters:
      options - the compiler options
      Returns:
      this operation instance
      Throws:
      NullPointerException - if options is null
    • compileTestClasspath

      public CompileKotlinOperation compileTestClasspath(String... classpath)
      Provides entries for the test compilation classpath.
      Parameters:
      classpath - one or more classpath entries
      Returns:
      this operation instance
      Throws:
      NullPointerException - if classpath is null or contains null elements
      IllegalArgumentException - if classpath is empty, or contains blank elements
    • compileTestClasspath

      public final CompileKotlinOperation compileTestClasspath(Collection<String> classpath)
      Provides the entries for the test compilation classpath.
      Parameters:
      classpath - the classpath entries
      Returns:
      this operation instance
      Throws:
      NullPointerException - if classpath is null or contains null elements
      IllegalArgumentException - if classpath is empty, or contains blank elements
    • compileTestClasspath

      public Set<String> compileTestClasspath()
      Retrieves the entries for the test compilation classpath.
      Returns:
      the classpath entries
    • env

      public CompileKotlinOperation env(String name, String value)
      Adds an environment variable.

      These variables are merged with the current process environment. Existing variables with the same name are overridden.

      Parameters:
      name - the variable name, must not be null
      value - the variable value, must not be null
      Returns:
      this operation instance
      Throws:
      IllegalArgumentException - if name is blank
      NullPointerException - if name or value is null
      See Also:
    • env

      Adds environment variables.

      These variables are merged with the current process environment. Existing variables with the same name are overridden.

      Parameters:
      vars - the map of environment variables, must not be null and must not contain null keys or values
      Returns:
      this operation instance
      Throws:
      NullPointerException - if vars is null, or if vars contains a null key or value
      See Also:
    • env

      public Map<String,String> env()
      Returns the environment variables.

      The returned map is mutable and can be modified directly before calling execute().

      Returns:
      the mutable environment variables map, never null
    • fromProject

      public CompileKotlinOperation fromProject(BaseProject project)
      Configures a compile operation from a BaseProject.

      Sets the following from the project:

      Parameters:
      project - the project to configure the compile operation from
      Returns:
      this operation instance
      Throws:
      NullPointerException - if project is null
    • inheritIO

      public CompileKotlinOperation inheritIO(boolean inheritIO)
      Configures whether the child process should inherit the I/O streams of the current JVM.

      When true, the child process uses the same stdin, stdout, and stderr as the current Java process. This enables interactive commands, preserves ANSI colors, and allows progress bars to display correctly. Output is not captured by the logger and cannot be asserted in tests.

      When false, stdout and stderr are merged and captured through the logger. This makes output testable and keeps it in the build log, but breaks interactive prompts and ANSI formatting.

      Default is TRUE

      Parameters:
      inheritIO - true to inherit I/O, false to capture output
      Returns:
      this operation instance
    • isInheritIO

      public boolean isInheritIO()
      Returns whether the child process inherits the I/O streams of the current JVM.
      Returns:
      true if I/O is inherited (default), false if stderr is redirected to stdout
      See Also:
    • jvmOptions

      public @Nullable JvmOptions jvmOptions()
      Retrieves the Java Virtual Machine options.
      Returns:
      the JVM options
    • jvmOptions

      public CompileKotlinOperation jvmOptions(JvmOptions options)
      Provides the Java Virtual Machine options.
      Parameters:
      options - the JVM options
      Returns:
      this operation instance
      Throws:
      NullPointerException - if options is null
      Since:
      1.2
    • kotlinHome

      public CompileKotlinOperation kotlinHome(String dir)
      Provides the Kotlin home directory, if it differs from the default KOTLIN_HOME.
      Parameters:
      dir - the directory path
      Returns:
      this operation instance
      Throws:
      NullPointerException - if dir is null
      IllegalArgumentException - if dir is blank
    • kotlinHome

      public CompileKotlinOperation kotlinHome(File dir)
      Provides the Kotlin home directory, if it differs from the default KOTLIN_HOME.
      Parameters:
      dir - the directory
      Returns:
      this operation instance
      Throws:
      NullPointerException - if dir is null
    • kotlinHome

      public CompileKotlinOperation kotlinHome(Path dir)
      Provides the Kotlin home directory, if it differs from the default KOTLIN_HOME.
      Parameters:
      dir - the directory path
      Returns:
      this operation instance
      Throws:
      NullPointerException - if dir is null
    • kotlinHome

      public @Nullable File kotlinHome()
      Retrieves the Kotlin home directory.
      Returns:
      the directory
    • kotlinc

      public CompileKotlinOperation kotlinc(String executable)
      Provides the path to the Kotlin compiler (kotlinc) executable, if not in kotlinHome().
      Parameters:
      executable - the executable path
      Returns:
      this operation instance
      Throws:
      NullPointerException - if executable is null
      IllegalArgumentException - if executable is blank
    • kotlinc

      public @Nullable File kotlinc()
      Retrieves the path to the Kotlin compiler (kotlinc) executable, if not in kotlinHome().
      Returns:
      the executable path
    • kotlinc

      public CompileKotlinOperation kotlinc(File executable)
      Provides the path to the Kotlin compiler (kotlinc) executable, if not in kotlinHome().
      Parameters:
      executable - the executable path
      Returns:
      this operation instance
      Throws:
      NullPointerException - if executable is null
    • kotlinc

      public CompileKotlinOperation kotlinc(Path executable)
      Provides the path to the Kotlin compiler (kotlinc) executable, if not in kotlinHome().
      Parameters:
      executable - the executable path
      Returns:
      this operation instance
      Throws:
      NullPointerException - if executable is null
    • mainSourceDirectories

      public List<File> mainSourceDirectories()
      Retrieves the main source directories that should be compiled.
      Returns:
      the main source directories
    • mainSourceDirectories

      public CompileKotlinOperation mainSourceDirectories(File... directories)
      Provides main source directories that should be compiled.
      Parameters:
      directories - one or more main source directories
      Returns:
      this operation instance
      Throws:
      NullPointerException - if directories is null
      See Also:
    • mainSourceDirectories

      public CompileKotlinOperation mainSourceDirectories(Path... directories)
      Provides main source directories that should be compiled.
      Parameters:
      directories - one or more main source directories
      Returns:
      this operation instance
      Throws:
      NullPointerException - if directories is null
      See Also:
    • mainSourceDirectories

      public CompileKotlinOperation mainSourceDirectories(String... directories)
      Provides main source directories that should be compiled.
      Parameters:
      directories - one or more main source directories
      Returns:
      this operation instance
      Throws:
      NullPointerException - if directories is null
      IllegalArgumentException - if directories is empty, or contains blank elements
      See Also:
    • mainSourceDirectories

      public final CompileKotlinOperation mainSourceDirectories(Collection<File> directories)
      Provides the main source directories that should be compiled.
      Parameters:
      directories - the main source directories
      Returns:
      this operation instance
      Throws:
      NullPointerException - if directories is null
      See Also:
    • mainSourceDirectoriesPaths

      public final CompileKotlinOperation mainSourceDirectoriesPaths(Collection<Path> directories)
      Provides the main source directories that should be compiled.
      Parameters:
      directories - the main source directories
      Returns:
      this operation instance
      Throws:
      NullPointerException - if directories is null
      IllegalArgumentException - if directories is empty, or contains null elements
      See Also:
    • mainSourceDirectoriesStrings

      public final CompileKotlinOperation mainSourceDirectoriesStrings(Collection<String> directories)
      Provides the main source directories that should be compiled.
      Parameters:
      directories - the main source directories
      Returns:
      this operation instance
      Throws:
      NullPointerException - if directories is null
      IllegalArgumentException - if directories is empty, or contains blank elements
      See Also:
    • mainSourceFiles

      public List<File> mainSourceFiles()
      Retrieves the main files that should be compiled.
      Returns:
      the files
    • mainSourceFiles

      public CompileKotlinOperation mainSourceFiles(File... files)
      Provides main source files that should be compiled.
      Parameters:
      files - one or more main source files
      Returns:
      this operation instance
      Throws:
      NullPointerException - if files is null
      See Also:
    • mainSourceFiles

      public CompileKotlinOperation mainSourceFiles(String... files)
      Provides the main source files that should be compiled.
      Parameters:
      files - one or more main source files
      Returns:
      this operation instance
      Throws:
      NullPointerException - if files is null
      IllegalArgumentException - if files is empty, or contains blank elements
      See Also:
    • mainSourceFiles

      public CompileKotlinOperation mainSourceFiles(Path... files)
      Provides the main source files that should be compiled.
      Parameters:
      files - one or more main source files
      Returns:
      this operation instance
      Throws:
      NullPointerException - if files is null
      See Also:
    • mainSourceFiles

      public CompileKotlinOperation mainSourceFiles(Collection<File> files)
      Provides the main source files that should be compiled.
      Parameters:
      files - the main source files
      Returns:
      this operation instance
      Throws:
      NullPointerException - if files is null
      See Also:
    • mainSourceFilesPaths

      public final CompileKotlinOperation mainSourceFilesPaths(Collection<Path> files)
      Provides the main source files that should be compiled.
      Parameters:
      files - the main source files
      Returns:
      this operation instance
      Throws:
      NullPointerException - if files is null
      IllegalArgumentException - if files is empty, or contains null elements
      See Also:
    • mainSourceFilesStrings

      public final CompileKotlinOperation mainSourceFilesStrings(Collection<String> files)
      Provides the main source files that should be compiled.
      Parameters:
      files - the main source files
      Returns:
      this operation instance
      Throws:
      NullPointerException - if files is null
      IllegalArgumentException - if files is empty, or contains blank elements
      See Also:
    • outputConsumer

      public CompileKotlinOperation outputConsumer(Consumer<String> outputConsumer)
      Sets a consumer to receive output lines when not inheriting I/O.

      Only called when isInheritIO() is false. Default logs at INFO level.

      Parameters:
      outputConsumer - the output consumer, must not be null
      Returns:
      this operation instance
      Throws:
      NullPointerException - if outputConsumer is null
    • plugins

      public CompileKotlinOperation plugins(String directory, CompilerPlugin... plugins)
      Provides compiler plugins.
      Parameters:
      directory - the directory containing the plugin JARs
      plugins - one or more plugins
      Returns:
      this class instance
      Throws:
      NullPointerException - if directory or plugins is null
      IllegalArgumentException - if directory is blank
    • plugins

      public CompileKotlinOperation plugins(File directory, CompilerPlugin... plugins)
      Provides compiler plugins.
      Parameters:
      directory - the directory containing the plugin JARs
      plugins - one or more plugins
      Returns:
      this class instance
      Throws:
      NullPointerException - if directory or plugins is null
    • plugins

      public Set<String> plugins()
      Retrieves the compiler plugins.
      Returns:
      the compiler plugins
    • plugins

      public CompileKotlinOperation plugins(String... plugins)
      Provides compiler plugins.
      Parameters:
      plugins - one or more plugins
      Returns:
      this class instance
      Throws:
      NullPointerException - if plugins is null
      IllegalArgumentException - if plugins is empty, or contains null or empty elements
    • plugins

      public final CompileKotlinOperation plugins(Collection<String> plugins)
      Provides compiler plugins.
      Parameters:
      plugins - the compiler plugins
      Returns:
      this class instance
      Throws:
      NullPointerException - if plugins is null
      IllegalArgumentException - if plugins is empty, or contains blank elements
    • plugins

      public CompileKotlinOperation plugins(Path directory, CompilerPlugin... plugins)
      Provides compiler plugins.
      Parameters:
      directory - the directory containing the plugin JARs
      plugins - one or more plugins
      Returns:
      this class instance
      Throws:
      NullPointerException - if directory or plugins is null
    • plugins

      public CompileKotlinOperation plugins(CompilerPlugin... plugins)
      Provides compiler plugins located in the kotlinHome() lib directory.
      Parameters:
      plugins - one or more plugins
      Returns:
      this class instance
      Throws:
      NullPointerException - if plugins is null or contains null elements
      IllegalArgumentException - if plugins is empty
      See Also:
    • testSourceDirectories

      public List<File> testSourceDirectories()
      Retrieves the test source directories that should be compiled.
      Returns:
      the test source directories
    • testSourceDirectories

      public CompileKotlinOperation testSourceDirectories(File... directories)
      Provides test source directories that should be compiled.
      Parameters:
      directories - one or more test source directories
      Returns:
      this operation instance
      Throws:
      NullPointerException - if directories is null
      See Also:
    • testSourceDirectories

      public CompileKotlinOperation testSourceDirectories(Path... directories)
      Provides test source directories that should be compiled.
      Parameters:
      directories - one or more test source directories
      Returns:
      this operation instance
      Throws:
      NullPointerException - if directories is null
      See Also:
    • testSourceDirectories

      public CompileKotlinOperation testSourceDirectories(String... directories)
      Provides test source directories that should be compiled.
      Parameters:
      directories - one or more test source directories
      Returns:
      this operation instance
      Throws:
      NullPointerException - if directories is null
      IllegalArgumentException - if directories is empty, or contains blank elements
      See Also:
    • testSourceDirectories

      public final CompileKotlinOperation testSourceDirectories(Collection<File> directories)
      Provides the test source directories that should be compiled.
      Parameters:
      directories - the test source directories
      Returns:
      this operation instance
      Throws:
      NullPointerException - if directories is null
      See Also:
    • testSourceDirectoriesPaths

      public final CompileKotlinOperation testSourceDirectoriesPaths(Collection<Path> directories)
      Provides the test source directories that should be compiled.
      Parameters:
      directories - the test source directories
      Returns:
      this operation instance
      Throws:
      NullPointerException - if directories is null
      IllegalArgumentException - if directories is empty, or contains null elements
      See Also:
    • testSourceDirectoriesStrings

      public final CompileKotlinOperation testSourceDirectoriesStrings(Collection<String> directories)
      Provides the test source directories that should be compiled.
      Parameters:
      directories - the test source directories
      Returns:
      this operation instance
      Throws:
      NullPointerException - if directories is null
      IllegalArgumentException - if directories is empty, or contains null or empty elements
      See Also:
    • testSourceFiles

      public List<File> testSourceFiles()
      Retrieves the test files that should be compiled.
      Returns:
      the test files
    • testSourceFiles

      public CompileKotlinOperation testSourceFiles(File... files)
      Provides test source files that should be compiled.
      Parameters:
      files - one or more test source files
      Returns:
      this operation instance
      Throws:
      NullPointerException - if files is null
      See Also:
    • testSourceFiles

      public CompileKotlinOperation testSourceFiles(String... files)
      Provides the test sources files that should be compiled.
      Parameters:
      files - one or more test source files
      Returns:
      this operation instance
      Throws:
      NullPointerException - if files is null
      IllegalArgumentException - if files is empty, or contains blank elements
      See Also:
    • testSourceFiles

      public CompileKotlinOperation testSourceFiles(Path... files)
      Provides the test sources files that should be compiled.
      Parameters:
      files - one or more test source files
      Returns:
      this operation instance
      Throws:
      NullPointerException - if files is null
      See Also:
    • testSourceFiles

      public final CompileKotlinOperation testSourceFiles(Collection<File> files)
      Provides the test source files that should be compiled.
      Parameters:
      files - the test source files
      Returns:
      this operation instance
      Throws:
      NullPointerException - if files is null
      See Also:
    • testSourceFilesPaths

      public final CompileKotlinOperation testSourceFilesPaths(Collection<Path> files)
      Provides the test source files that should be compiled.
      Parameters:
      files - the test source files
      Returns:
      this operation instance
      Throws:
      NullPointerException - if files is null
      IllegalArgumentException - if files is empty, or contains null elements
      See Also:
    • testSourceFilesStrings

      public final CompileKotlinOperation testSourceFilesStrings(Collection<String> files)
      Provides the test source files that should be compiled.
      Parameters:
      files - the test source files
      Returns:
      this operation instance
      Throws:
      NullPointerException - if files is null
      IllegalArgumentException - if files is empty, or contains blank elements
      See Also:
    • timeout

      public CompileKotlinOperation timeout(long seconds)
      Sets the timeout for the Kotlin compiler execution in seconds.

      If the process does not complete within the specified timeout, it will be terminated and the operation will fail. If set to any negative value, the process will wait indefinitely. Passing 0 is not allowed; use a negative value to indicate no timeout.

      Default is 600 seconds (10 minutes)

      Parameters:
      seconds - the timeout in seconds (positive); use a negative value for no timeout
      Returns:
      this operation instance
      Throws:
      IllegalArgumentException - if seconds is 0
      Since:
      1.2
    • timeout

      public long timeout()
      Retrieves the timeout for the Kotlin compiler execution in seconds.

      A positive value is the timeout duration in seconds. A negative value indicates no timeout (wait indefinitely). 0 is not a valid state; the setter disallows it.

      Returns:
      the timeout in seconds (positive), or a negative value if no timeout is set
      Since:
      1.2
    • workDir

      public CompileKotlinOperation workDir(File dir)
      Provides the working directory if it differs from the project's directory.
      Parameters:
      dir - the directory
      Returns:
      this operation instance
      Throws:
      NullPointerException - if dir is null
    • workDir

      public CompileKotlinOperation workDir(Path dir)
      Provides the working directory if it differs from the project's directory.
      Parameters:
      dir - the directory
      Returns:
      this operation instance
      Throws:
      NullPointerException - if dir is null
    • workDir

      public CompileKotlinOperation workDir(String dir)
      Provides the working directory if it differs from the project's directory.
      Parameters:
      dir - the directory path
      Returns:
      this operation instance
      Throws:
      NullPointerException - if dir is null
      IllegalArgumentException - if dir is blank
    • workDir

      public @Nullable File workDir()
      Retrieves the working directory.
      Returns:
      the directory
    • executeBuildMainSources

      protected void executeBuildMainSources() throws ExitStatusException
      Part of the execute operation, builds the main sources.

      Copies the configured classpath and sources to prevent mutation of operation state across multiple execute() invocations.

      Throws:
      ExitStatusException - if an error occurs
    • executeBuildSources

      protected void executeBuildSources(@Nullable Collection<String> classpath, Collection<File> sources, @Nullable File destination, @Nullable File friendPaths) throws ExitStatusException
      Part of the execute operation, build sources to a given destination.
      Parameters:
      classpath - the classpath list used for the compilation
      sources - the source files to compile
      destination - the destination directory
      friendPaths - the output directory for friendly modules
      Throws:
      ExitStatusException - if an error occurs
    • executeBuildTestSources

      protected void executeBuildTestSources() throws ExitStatusException
      Part of the execute operation, builds the test sources.

      Copies the configured classpath and sources to prevent mutation of operation state across multiple execute() invocations. Test compilation uses the main build directory as a friend path for module visibility.

      Throws:
      ExitStatusException - if an error occurs
    • executeCreateBuildDirectories

      protected void executeCreateBuildDirectories() throws IOException
      Part of the execute operation, creates the build directories.
      Throws:
      IOException - if an error occurs