GoogleContainerTools / jib

🏗 Build container images for your Java applications.
Apache License 2.0
13.63k stars 1.43k forks source link

Jib gradle plugin and Java modules? #4280

Open hrstoyanov opened 3 months ago

hrstoyanov commented 3 months ago

Environment:

Description of the issue:

  1. How do prevent Jib gradle plugin from using/searching main class and instead make it use main module?
  2. How do I tell Jib gradle plugin to use module path?
  3. How do I tell it to stop extracting the content of the jar that has a my main class?

I would like to specify all of these mylsef:

java --module my.module/my.package.App. --module-path /app/libs

I have used the below configuration, but this plugin still tries to infer main class and unzip the jar with the main class.

container{
        entrypoint = [
                "java",
                '--module-path', '/app/libs',
                '--module', 'my.module/my.App'
        ]
    }

Expected behavior:

I want Jib to stop doing so opinionated and classpath-centric, and allow me to set up main module path and main module myself.

hrstoyanov commented 3 months ago

Ok, so I start using containerizedMode packed:

    containerizingMode = 'packaged'
    container{
        entrypoint = [
                "java",
                '--module-path', '/app/classpath:/app/libs',
                '--module', 'my.module/my.App'
        ]
        mainClass = 'NotNeeded'
    }

and that seem to prevent Jib from unpacking the main module. I was able to run my app as a 100% modular app, no classpaths involved. Still unhappy about:

  1. Jib insisting on container.mainClass, or trying to inspect my jars (with ASM) if I do not provide one.
  2. Jib putting the main module in /app/classpath separate from /app/libs, but this is not a big deal.
  3. Generating jib-classpath-file and jib-main-class-file, which I do not use.
chanseokoh commented 3 months ago

UPDATE: actually, searching for mainClass even when setting entrypoint is WAI. See https://github.com/GoogleContainerTools/jib/issues/4280#issuecomment-2323346289.

Not ignoring mainClass when setting entrypoint seems like a regression or a bug, because it's not going to be used anyway.

https://github.com/GoogleContainerTools/jib/tree/master/jib-maven-plugin#container-object

hrstoyanov commented 3 months ago

Regression confirmed - commenting out the mainClass below triggers main class discovery:

jib {
    containerizingMode = 'packaged'
    container {
        entrypoint = [
                "java",
                '--module-path', '/app/classpath:/app/libs', 
                '--module', 'my.module/my.App'
        ]
        //mainClass = 'NotNeeded'
    }
}
Execution failed for task ':webapp:jib'.
> Check the full stace trace, and if the root cause is from ASM ClassReader about unsupported class file version, see https://github.com/GoogleContainerTools/jib/blob/master/docs/faq.md#i-am-seeing-unsupported-class-file-major-version-when-building
chanseokoh commented 1 month ago

Actually, it's WAI that Jib searches for a main class even if you set a custom entrypoint. Jib still needs to know the main class to generate the /app/jib-main-class-file to support the popular use-case where the user wants to know and use the main class that Jib has found in their custom entrypoint. But I agree this isn't ideal for the Java module use-case.