Search code examples
javamavendockerjib

Building with JIB give Multiple valid main classes were found even though the mainClass is define


When building my service project with jib command mvn clean compile jib:build it's give the following error:

Failed to execute goal com.google.cloud.tools:jib-maven-plugin:1.0.2:build (build-image-and-tag-image) on project my-service: Multiple valid main classes were found: com.myservice.MyServiceApplication, io.swagger.Swagger2SpringBoot, perhaps you should add a mainClass configuration to jib-maven-plugin -> [Help 1]

However I have set the main classes for spring-boot

    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
            <mainClass>com.myservice.MyServiceApplication</mainClass>
        </configuration>
        <executions>
            <execution>
                <goals>
                    <goal>repackage</goal>
                </goals>
            </execution>
        </executions>
    </plugin>

It's still doesn't work.

I've tried to add it to the jib config to:

        <plugins>
            <plugin>
                <groupId>com.google.cloud.tools</groupId>
                <artifactId>jib-maven-plugin</artifactId>
                <configuration>
                    <container>
                        <mainClass>com.myservice.MyServiceApplication</mainClass>
                    </container>
                </configuration>                        
                <executions>
                    <execution>
                        <id>build-image-and-tag-image</id>
                        <phase>package</phase>
                        <goals>
                            <goal>dockerBuild</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

It's still doesn't work. Any other way to force jib to ignore the other class and use com.myservice.MyServiceApplication instead.

Note: mvn clean install work fine and I have no problem using it has a stand alone spring boot app.


Solution

  • The main class need to be set in the < plugins > define in < build > of the pom.xml file.

    It would look like this to fix the problem:

        <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.myservice.MyServiceApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>com.google.cloud.tools</groupId>
                <artifactId>jib-maven-plugin</artifactId>
                <configuration>
                    <container>
                        <mainClass>com.myservice.MyServiceApplication</mainClass>
                        <ports>
                            <port>8080</port>
                        </ports>
                        <environment>
                            <application.title>${project.name}</application.title>
                            <application.version>${project.version}</application.version>
                        </environment>
                        <jvmFlags>
                            <jvmFlag>-javaagent:/usr/local/newrelic/newrelic.jar</jvmFlag>
                        </jvmFlags>
                    </container>
                </configuration>
            </plugin>
            .... (more plugin)
        </plugins>
    </build>