GoogleContainerTools / jib

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

Searching for mainClass even if entrypoint is specified #4300

Closed mihalyr closed 1 month ago

mihalyr commented 2 months ago

Environment:

Description of the issue:

Gradle Jib plugin shows the following warnings when an entrypoint is specified without a main class:

Searching for main class... Add a 'mainClass' configuration to 'jib' to improve build speed.
Could not find a valid main class from 'jar' task; looking into all class files to infer main class.

However, this is wrong, because the mainClass should be ignore when an entrypoint is specified. I'm not sure if this is only cosmetic or Jib indeed searches for a main class that it ignores later, becaue of the entrypoint is already given.

Expected behavior:

No warnings and no searching for main class when an entrypoint is specified.

Steps to reproduce:

  1. configure jib with an entrypoint and no mainclass
  2. run jibDockerBuild

jib-gradle-plugin Configuration:

application {
  mainClass = 'app.Main'
}

jib {
    from {
        image = 'docker://localhost/app-base:latest'
    }
    to {
        image = 'app'
    }
    container {
        appRoot = '/app'
        workingDirectory = '/app'
        entrypoint = ['./run.sh']
        ports = ['8080']
    }
    extraDirectories {
        paths {
            path {
                from = file('bin')
                into = '/app'
                includes = ['run.sh']
            }
        }
        permissions = [
                '/app/run.sh': '755'
        ]
    }
}

Log output:

> Task :app:jibDockerBuild
Caching disabled for task ':app:jibDockerBuild' because:
  Caching has not been enabled for the task
Task ':app:jibDockerBuild' is not up-to-date because:
  Task has not declared any outputs despite executing actions.
Searching for main class... Add a 'mainClass' configuration to 'jib' to improve build speed.
Could not find a valid main class from 'jar' task; looking into all class files to infer main class.

Containerizing application to Docker daemon as ...
chanseokoh commented 2 months ago

This is intended and WAI, because often you want to know the discovered main class in your entrypoint: https://github.com/GoogleContainerTools/jib/issues/4280#issuecomment-2323346289.

A simple workaround in your case is to set mainClass to a garbage value.

mihalyr commented 2 months ago

I see, although in my case I do have an application { mainClass = 'app.Main' } configuration. Shouldn't Jib just use that class by default instead of having me to duplicate configuration?

This is intended and WAI, because often you want to know the discovered main class in your entrypoint: https://github.com/GoogleContainerTools/jib/issues/4280#issuecomment-2323346289.

If this is indeed the case, I think the warning message might be a bit misleading, since it tells you that the mainClass is ignored, but it is not the case, because setting mainClass will prevent scanning for mainClass and it still generates the files that contain the main class and classpaths, right? So it isn't really ignored, it is just up to you how you utilize the discovered class in your own entrypoint.

mainClass, extraClasspath, jvmFlags, and expandClasspathDependencies are ignored when entrypoint is specified
chanseokoh commented 2 months ago

Agreed.

blakeli0 commented 1 month ago

Thanks @mihalyr for reporting this issue and thanks @chanseokoh for triaging! Closing this as WAI, but we are going look for ways to improve the logging. Feel free to reopen if needed.