1
root-projec
ㄴ sub1-project
   build.gradle
ㄴ sub2-project
   build.gradle
build.gradle

I'm trying to build all subprojects as jibs in the root project.
The following script is the build.gradle file of the root and sub modules.

...
jib {
    from {
        image = "eclipse-temurin:17"
    }

    to {
        image = "abc/${project.name}:${project.version}"
        tags = ["latest"]

    }
} 
cd root-project
./gradlew jib

Execution failed for task ':jib'.
> com.google.cloud.tools.jib.plugins.common.MainClassInferenceException: Main class was not found, perhaps you should add a `mainClass` configuration to jib

Of course there are no classes in the root project. Why do you want to include the main class in the root project when building the jib? I'd like to find a way to exclude this.

Additionally, is there any way to build with jib when building with a specific profile as follows?
There is a way to write a script in maven, but gradle has grammatical difficulties.

./gradlew clean build -P build-docker-image
majava
  • 45
  • 9

1 Answers1

0

You need to make following changes in your root build.gradle -

  1. I'd suggest you to first use the './gradlew jibDockerBuild' task instead of './gradlew jib' task, and then manually push each image to your preferred container registry(dockerhub, gcr, ecr etc) by using the 'docker push <image_id>' command.

  2. Since, you're calling the './gradlew jibDockerBuild' task from root, you need to explicitly disable this task for root by using the following command -

    jibDockerBuild.enabled = false

  3. Shift the 'jib' block inside a 'subprojects' block. (I'd say try to also apply the jib plugin inside the subprojects block.

  4. ** Try the first three steps first. If you're still getting the mainClass not found exception, only then try this one. So you've to explicitly specify the mainClass property for each subproject. You can do this by simply adding an if-else block at the end of the subprojects block like this -

     subprojects {
     -----
         if(project.name == 'sub1-project') {
             container {
                 mainClass = "com.example.sub1project.Sub1proejctApplication"
             }
         } else if(project.name == 'sub2-project') {
             container {
                 mainClass = "com.example.sub2project.Sub2projectApplication"
             }
         }...
     }
    

Your final build.gradle might look something like this -

----

jibDockerBuild.enabled = false

subprojects {
    apply plugin:'com.google.cloud.tools.jib
    jib {
        from {
            image = "eclipse-temurin:17"
            auth {
                username = property('DOCKERHUB_USERNAME') //pass these through a gradle.properties file or as cmd-line args.
                password = property('DOCKERHUB_PASSWORD')
            }
        }

        to {
            image = "abc/${project.name}:${project.version}"
            tags = ["latest"]
            auth {
                username = property('DOCKERHUB_USERNAME') //pass these through a gradle.properties file or as cmd-line args.
                password = property('DOCKERHUB_PASSWORD')
            }
        }
        // if above code doesn't work, add the if-else block here for specifying mainClass
    }
}
dev
  • 19
  • 5