javaspring-bootgradlejibgradle-multi-project-build

Jib gradle plugin multimodule project ClassNotFoundException


I have multi-module gradle project and using Jib plugin for build docker image and push to registry.

But, when I build image and try to run this image I get this exception:

Error: Could not find or load main class Xmx512m

Caused by: java.lang.ClassNotFoundException: Xmx512m

Here is my root build.gradle file:

plugins {
    id 'org.springframework.boot' version "$springBootVersion" apply false
    id 'io.spring.dependency-management' version "$springDependencyManagerVersion" apply false
    id 'com.google.cloud.tools.jib' version "$jlibVersion" apply false
}

def javaMicroservices = [
        project(':akt-user-management-ms'),
        project(':akt-applications-ms')
]

allprojects {
    repositories {
        mavenCentral()
        jcenter()
    }

    apply plugin: 'java'

    dependencies {
        implementation "io.jsonwebtoken:jjwt-api:${jwt_version}"
        implementation "io.jsonwebtoken:jjwt-impl:${jwt_version}"
        implementation "io.jsonwebtoken:jjwt-jackson:${jwt_version}"
    }
}

subprojects {
    sourceCompatibility = jdk_version
    targetCompatibility = jdk_version

    apply plugin: 'pmd'
    apply plugin: 'checkstyle'

    pmd {
        toolVersion = '6.10.0'
        ruleSets = []
        ignoreFailures = false
        rulePriority = 3
        ruleSetFiles = files("${rootProject.projectDir}/qa/pmd.xml")
    }

    checkstyle {
        toolVersion = '8.15'
        configFile = file("${rootProject.projectDir}/qa/checkstyle.xml")
        ignoreFailures = false
    }
    tasks.withType(Test) {
        maxParallelForks = Runtime.runtime.availableProcessors().intdiv(2) ?: 1
    }
}

configure(javaMicroservices) {
    apply plugin: 'com.google.cloud.tools.jib'

    jib {
        to {
            image = "$dockerRepoUrl/${project.name}"
        }
        container {
            jvmFlags = ['Xmx512m']
            creationTime = 'USE_CURRENT_TIMESTAMP'
        }
    }

}

Root settings.gradle

rootProject.name = 'xxxx'
include 'akt-user-management-ms' //8080
include 'akt-applications-ms' //8081
include 'middleware' //8082

build.gradle file for one of this project. (All of them are same except dependecy):

plugins {
    id 'org.springframework.boot'
    id 'io.spring.dependency-management'
    id 'java'
    id 'com.google.cloud.tools.jib'
}

group = 'az.ingress.user.management'
version = '0.0.1-SNAPSHOT'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation project(":common")

    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.springframework.boot:spring-boot-starter-validation'
    implementation 'org.springframework.boot:spring-boot-starter-web'

    implementation "io.springfox:springfox-swagger2:$swaggerVersion"
    implementation "io.springfox:springfox-swagger-ui:$swaggerVersion"

    implementation 'org.liquibase:liquibase-core'

    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'

    runtimeOnly 'mysql:mysql-connector-java'

    testImplementation 'org.springframework.security:spring-security-test'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}

test {
    useJUnitPlatform()
}

Could not find any solution. Tried many things but keep getting this exception over and over again.

Note: using jib version: 2.6.0


Solution

  • Command-line options are usually prefixed with - or --. It should be -Xmx512m in this case. Without -, java will think Xmx512m is a Java main class name.

    $ java -Xmx512m com.example.HelloWorldMain
    hello world
    $ java foo
    Error: Could not find or load main class foo
    Caused by: java.lang.ClassNotFoundException: foo
    $ java Xmx512m
    Error: Could not find or load main class Xmx512m
    Caused by: java.lang.ClassNotFoundException: Xmx512m