Jib Construction image analysis problems (Could not find or load main class $ {start-class})

Description of problem

By Jib plug-SpringBoot project made into a successful Docker mirror, but a mirror running time error (Could not find load main class $ {start-class} or), today to analyze the problem together, hoping to help the reader to skip the pits.

About Jib plug

Jib can use Maven plug-in project will be built into the current Java project Docker mirroring, please refer to:

  1. "Docker and Jib (maven plug-in version) combat" ;
  2. "Jib Use Summary (Maven plug-in version)" ;

Environmental Information

  1. Operating System: macOS Mojave 10.14.6 (18G103)
  2. JDK:10.14.6 (18G103)
  3. Docker:10.14.6 (18G103)
  4. SpringBoot:2.1.8.RELEASE
  5. Jib Plugin Version: 1.6.1

Source download

To reproduce the problem, I will upload the problem of SpringBoot project to GitHub, address and link information in the following table:

name link Remark
Project Home https://github.com/zq2599/blog_demos The project's home page on GitHub
git repository address (https) https://github.com/zq2599/blog_demos.git The project's source code repository address, https protocol
git repository address (ssh) [email protected]: zq2599 / blog_demos.git The project's source code repository address, ssh protocol


The git item has multiple folders in the application of this chapter jib-error-demo folder, as shown in FIG red box:
Here Insert Picture Description

problem:

  1. Pom.xml file directory execute the command mvn Clean the compile -U , mirroring can build successful, but the console output of a warning message, as shown below:
    Here Insert Picture Description
  2. Attempt to create a container of this mirror, the command line Docker --name = RUN Test bolingcavalry / hellojib: 0.0.1-the SNAPSHOT , given as follows:
CN0014005932:~ zhaoqin$ docker run --name=test bolingcavalry/hellojib:0.0.1-SNAPSHOT
Error: Could not find or load main class ${start-class}
  1. docker ps -a view container information below, only to see state is "Exit", the other lacks:
CN0014005932:~ zhaoqin$ docker ps -a
CONTAINER ID        IMAGE                                   COMMAND                  CREATED             STATUS                     PORTS               NAMES
d618f6588821        bolingcavalry/hellojib:0.0.1-SNAPSHOT   "java -Xms4g -Xmx4g …"   4 minutes ago       Exited (1) 4 minutes ago                       test
  1. Can not be reconciled with the command docker ps -a --no-trunc view container information is not truncated:
CN0014005932:~ zhaoqin$ docker ps -a --no-trunc
CONTAINER ID                                                       IMAGE                                   COMMAND                                                                           CREATED             STATUS                     PORTS               NAMES
d618f6588821f00d3bd0b67a85ff92988b90dfff710370c9d340d5c544c550af   bolingcavalry/hellojib:0.0.1-SNAPSHOT   "java -Xms4g -Xmx4g -cp /app/resources:/app/classes:/app/libs/* ${start-class}"   7 minutes ago       Exited (1) 7 minutes ago                       test
  1. The new discovery, execute the command to start when the container is the Java -Xms4g -Xmx4g -cp / App / Resources: / App / classes: / App / libs / * $ {} Start-class , Guaizai! The $ {start-class} What is? Let's look at a mirror image of the normal startup command:
java -Xms4g -Xmx4g -cp /app/resources:/app/classes:/app/libs/* com.bolingcavalry.jiberrordemo.JibErrorDemoApplication

As indicated above, com.bolingcavalry.jiberrordemo.JibErrorDemoApplication is a method where the main class, this class command can be run JibErrorDemoApplication normal main method;

  1. Summary: Implementation of the java command to start the container, the $ {start-class} as an argument to java, java can not process leading to this parameter, so the process error, cause the container to exit;

problem causes

The reason for this problem is simple: class java project with more than one main method , to look at the code jib-error-demo project, found in Utils.java really have a main method:

public class Utils {

    public static String time(){
      return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()).toString();
    }

    public static void main(String[] args){
        System.out.println(time());
    }
}

The above-mentioned main methods deleted, and then build and run the container mirror, confirmed the problem has been resolved.

Another solution to the problem

If you want to move the code Utils class (maybe a jar package with a main class method), pom.xml file open, increases in the plug arranged jib mainClass node, the node content class is specified class, as half moon like this:
Here Insert Picture Description
after the above settings, the problem can be resolved.

Next, if you are interested in a deeper reason, let's take a deep exploration of it.

Find a problem

  1. This problem in the official GitHub Jib is recorded, first look at the following address: https: //github.com/GoogleContainerTools/jib/issues/1601, red box as shown, the same problem, and finally issue sponsor himself closed the issue, because he found that his project has two classes with main methods:
    Here Insert Picture Description
  2. Let's look at this issue, https://github.com/GoogleContainerTools/jib/issues/170, Jib author Q Chen speculated that the Spring $ {start-class} This string is set to Main-Class attribute value (personal feeling here that Spring should be a spring boot of mave plug it), so Jib plug-in worth when using the Main-class, is to get $ {start-class} string of:
    Here Insert Picture Description
  3. Subsequent episode 170 this issue is very interesting, Jib author Q Chen on this issue is very tangled, if the Java project found more than one class with a main method, Jib exactly how to handle it? Q Chen finally decided to output a warning, as shown below:
    Here Insert Picture Description
  4. Take a look at this code it, that is the figure above # 288, the address is: https: //github.com/GoogleContainerTools/jib/pull/228/files/c8757e1f9ea47edd78df18142de7836a68f22034, if mainClass unlike a class name of the class, outputs an alarm, this logic in Gradle and Maven plug-ins are written:
    Here Insert Picture Description
  5. One last question: the code above mainClass come from? Open source on the map, the address is: https: //github.com/GoogleContainerTools/jib/blob/c8757e1f9ea47edd78df18142de7836a68f22034/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildImageMojo .java, red box as can be inferred from the name of the method, the value from construct plugins SpringBoot maven project, so the front-mentioned value Q Chen main-class variables are modified Spring, should be derived from this code:
    Here Insert Picture Description
    in this case You, if still something more to say, we have another look at the consolidation of start-class SpringBoot

    About start-class

  6. In fact students familiar SpringBoot $ {start-class} is not strange, when the plurality of classes in both engineering main When a method using this parameter to specify the start SpringBoot class;
  7. Look at SpringBoot official familiar with the document Start-class , the address is: https: //docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/, the following figure compares the contents of the key: we set the startup class is assigned to the start-class attribute, and Main-class attribute becomes org.springframework.boot.loader.JarLauncher , this is the real start SpringBoot categories:
    Here Insert Picture Description
  8. Below, this is a supplementary explanation, the Main-Class value of the property is transferred to the Start-Class attributes of this operation, when building a plug jar maven to do:
    Here Insert Picture Description
  9. Therefore, the value of start-class from main-class, the main-class value look come, as shown in FIG red box, with Maven widget will look for public static void main (String [] args) class:
    Here Insert Picture Description
    So far, mirroring the problem Jib build the analysis is complete, a small problem cited发了这么多learn and explore, though a bit time-consuming, but it can make people feel "technology is the same" feeling again, I do not know you have no such feel it?

    I welcome the attention of the public numbers: programmer Chen Xin

    Here Insert Picture Description

Guess you like

Origin www.cnblogs.com/bolingcavalry/p/11609371.html