Gradle 无法为 Docker 构建带有依赖的 JAR

3

我有一个使用Spring MVC框架开发的应用程序,我希望将其添加到Docker中。我创建了镜像,并配置好了Docker,但是在Docker中运行应用程序时出现了问题。我该如何解决这个问题?

我尝试了不同的方法来修复这个错误,例如,我在gradle.build文件中添加了以下代码:

dependencies {
extraLibs group: 'net.java.dev.jna', name: 'jna-platform', version: '4.2.2'
// ... dependencies ...
configurations.compile.extendsFrom(configurations.extraLibs)
}

但它没有起作用。

GRADLE:

plugins {
    id 'java'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'

sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}

//configurations {
    // configuration that holds jars to include in the jar
 //   extraLibs
//}

compileJava.options.encoding = 'UTF-8'
compileTestJava.options.encoding = 'UTF-8'
//


jar {
    manifest {
        attributes "Main-Class": 'ru.coffeetearea.CoffeeTeArea'
    }
}

ext {
    javaMainClass = "ru.coffeetearea.CoffeeTeArea"
}

task fatJar(type: Jar) {
    classifier = 'all'
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
    with jar
}

dependencies {
//    extraLibs group: 'net.java.dev.jna', name: 'jna-platform', version: '4.2.2'
    // https://mvnrepository.com/artifact/org.apache.commons/commons-lang3
    compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.11'
    // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test
    testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '2.3.3.RELEASE'
    // Thymeleaf
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf', version: '2.3.3.RELEASE'
    // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-validation
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-validation', version: '2.3.3.RELEASE'
    // Swagger UI
    compile group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.9.2'
    // Swagger 2
    compile group: 'io.springfox', name: 'springfox-swagger2', version: '2.9.2'
    // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot
    compile group: 'org.springframework.boot', name: 'spring-boot', version: '2.3.1.RELEASE'
    // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: '2.3.1.RELEASE'
    // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-jdbc
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-jdbc', version: '2.3.1.RELEASE'
    // https://mvnrepository.com/artifact/org.postgresql/postgresql
    compile group: 'org.postgresql', name: 'postgresql', version: '42.2.14'
    // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.3.1.RELEASE'
    // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: '2.3.1.RELEASE'
    // https://mvnrepository.com/artifact/org.flywaydb/flyway-core
    compile group: 'org.flywaydb', name: 'flyway-core', version: '6.5.1'
    // MapStruct
    implementation 'org.mapstruct:mapstruct:1.3.1.Final'
    annotationProcessor 'org.mapstruct:mapstruct-processor:1.3.1.Final'
    // https://mvnrepository.com/artifact/org.projectlombok/lombok
    compileOnly 'org.projectlombok:lombok:1.18.12'
    annotationProcessor 'org.projectlombok:lombok:1.18.12'
    // https://mvnrepository.com/artifact/org.hibernate.orm/hibernate-jpamodelgen
    annotationProcessor('org.hibernate:hibernate-jpamodelgen:6.0.0.Alpha5')
    // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-security
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-security', version: '2.3.2.RELEASE'
    // https://mvnrepository.com/artifact/io.jsonwebtoken/jjwt
    compile group: 'io.jsonwebtoken', name: 'jjwt', version: '0.9.1'
    // https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api
    compile group: 'javax.xml.bind', name: 'jaxb-api', version: '2.4.0-b180830.0359'
    // JUnit
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
    // https://mvnrepository.com/artifact/org.mockito/mockito-core
    testCompile group: 'org.mockito', name: 'mockito-core', version: '2.1.0'
//    configurations.compile.extendsFrom(configurations.extraLibs)
}

test {
    useJUnitPlatform()
    testLogging {
        events "passed", "skipped", "failed"
    }
}

DOCKERFILE:

FROM openjdk:11
ADD build/libs/Coffeetearea-0.0.1-SNAPSHOT.jar Coffeetearea-0.0.1-SNAPSHOT.jar
EXPOSE 5432
ENTRYPOINT ["java", "-jar", "Coffeetearea-0.0.1-SNAPSHOT.jar"]

Manifest.MF:

Manifest-Version: 1.0
Main-Class: ru.coffeetearea.CoffeeTeArea

错误:

C:\Users\vartanyan\IdeaProjects\Coffeetearea>docker run -p 5432:5432 coffeetearea
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/boot/SpringApplication
        at ru.coffeetearea.CoffeeTeArea.main(CoffeeTeArea.java:12)
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.SpringApplication
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
        ... 1 more

你正在使用Spring Boot,但在构建过程中完全忽略了它并绕过了它。不要这样做。请参见https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-container-images-docker。 - M. Deinum
1个回答

1
你的应用程序中使用了Spring Boot,并且在构建过程中,你非常努力地试图使用它。简而言之,不要这样做,使用Spring Boot Gradle插件来构建一个合适的jar文件。
plugins {
    id 'org.springframework.boot' version '2.3.6.RELEASE'
    id 'io.spring.dependency-management' version '1.0.10.RELEASE'
    id 'java'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'

sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}

compileJava.options.encoding = 'UTF-8'
compileTestJava.options.encoding = 'UTF-8'

dependencies {
//    extraLibs group: 'net.java.dev.jna', name: 'jna-platform', version: '4.2.2'
    // https://mvnrepository.com/artifact/org.apache.commons/commons-lang3
    implementation group: 'org.apache.commons', name: 'commons-lang3', version: 
'3.11'

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

    testImplementation group: 'org.springframework.boot:spring-boot-starter-test'

    // Swagger UI
    implementation group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.9.2'
    // Swagger 2
    implementation group: 'io.springfox', name: 'springfox-swagger2', version: '2.9.2'
    // https://mvnrepository.com/artifact/org.postgresql/postgresql
    implementation group: 'org.postgresql', name: 'postgresql'
    // https://mvnrepository.com/artifact/org.flywaydb/flyway-core
    implementation group: 'org.flywaydb', name: 'flyway-core'
    // MapStruct
    implementation 'org.mapstruct:mapstruct:1.3.1.Final'
    annotationProcessor 'org.mapstruct:mapstruct-processor:1.3.1.Final'
    // https://mvnrepository.com/artifact/org.projectlombok/lombok
    compileOnly 'org.projectlombok:lombok:1.18.12'
    annotationProcessor 'org.projectlombok:lombok:1.18.12'
    // https://mvnrepository.com/artifact/org.hibernate.orm/hibernate-jpamodelgen
    annotationProcessor('org.hibernate:hibernate-jpamodelgen:6.0.0.Alpha5')
    // https://mvnrepository.com/artifact/io.jsonwebtoken/jjwt
    compile group: 'io.jsonwebtoken', name: 'jjwt', version: '0.9.1'
    // https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api
    compile group: 'javax.xml.bind', name: 'jaxb-api', version: '2.4.0-b180830.0359'
}

test {
    useJUnitPlatform()
    testLogging {
        events "passed", "skipped", "failed"
    }
}

现在当您执行./gradlew build时,它将生成一个适当的Spring Boot jar文件,您可以在命令行上运行。您也可以在docker镜像中使用此jar文件。
从Spring Boot 2.3开始,可以使用构建包常规docker让Spring Boot创建镜像。
使用构建包时,上述build.gradle足以创建镜像。只需运行./gradlew bootBuildImage,然后它将使用构建包生成镜像。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接