1

我有多模块 gradle 项目并使用 Jib 插件构建 docker 映像并推送到注册表。

但是,当我构建图像并尝试运行这个图像时,我得到了这个异常:

错误:无法找到或加载主类 Xmx512m

引起:java.lang.ClassNotFoundException: Xmx512m

这是我的根 build.gradle 文件:

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'
        }
    }

}

根设置.gradle

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

此项目之一的 build.gradle 文件。(除了依赖之外,它们都是相同的):

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()
}

找不到任何解决方案。尝试了很多事情,但一次又一次地遇到这个异常。

注意:使用 jib 版本:2.6.0

4

2 回答 2

0

我从根 build.gradle 中删除了 jvmFlags = ['Xmx512m'],它按预期工作。

于 2020-12-11T22:24:31.893 回答
0

命令行选项通常以-或为前缀---Xmx512m在这种情况下应该是。没有-java会认为Xmx512m是一个Java主类名。

$ 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
于 2020-12-11T22:33:37.860 回答