jib icon indicating copy to clipboard operation
jib copied to clipboard

Gradle: Allow setting custom sourceset

Open stashymane opened this issue 1 year ago • 4 comments

Environment:

  • Jib version: 3.4.3
  • Build tool: Gradle 8.7

Description of the issue: Currently, the main sourceset is hardcoded into the gradle plugin. With the default Java plugin this works well, however running anything else makes using Jib difficult. My use-case is Kotlin Multiplatform - it does generate a JVM target, however it does not create a main sourceset. This results in Jib throwing an exception:

org.gradle.api.ProjectConfigurationException: A problem occurred configuring project ':server'.
Caused by: org.gradle.api.UnknownDomainObjectException: SourceSet with name 'main' not found.
	at com.google.cloud.tools.jib.gradle.JibPlugin.lambda$apply$9(JibPlugin.java:189)

Having to manually set up the main sourceset as a "mirror" is error-prone and cumbersome. A very simple choice of sourceset in the Jib configuration would solve most issues.

This exact issue was already mentioned in https://github.com/GoogleContainerTools/jib/issues/1778, and even though the issue was closed, only the configuration was allowed to be configurable. The sourceset still isn't.

stashymane avatar Sep 26 '24 10:09 stashymane

Out of curiosity, @stashymane could you explain how you bypassed this problem ?

yluom avatar Dec 09 '24 10:12 yluom

@yluom specifically for Kotlin Multiplatform, turns out you can run withJava() on the JVM configuration block. I avoided this earlier due to some issues being associated with it, but I'm not sure what that was anymore. In case of any other configuration, no clue.

stashymane avatar Dec 09 '24 10:12 stashymane

With Kotlin 2.1.20 now incompatible with the application plugin and the withJava() API now deprecated, it could become relevant to consider adding configurations to Jib, to support KMP/JVM users who have an e.g. jvmMain source-set instead of plain main.

chris-hatton avatar Apr 16 '25 11:04 chris-hatton

It seems like the only way to bypass this problem is to create a new JVM-only project with JIB applied and add the KMP project as a dependency to it.

osipxd avatar May 05 '25 15:05 osipxd

Maybe there is a way to configure this, or at least check for multiple targets? (Like you check for "main" ?: "jvmMain" ?: "commonMain", you get the idea) EDIT: I inspected the code, I'm making a PR that allows to configure it via the configuration extension of the Gradle plugin

nathanfallet avatar Jul 01 '25 11:07 nathanfallet