gradle无法为docker构建具有依赖项的jar

vuv7lop3  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(339)

我有一个
Spring MVC 我要添加到的应用程序 Docker . 我创造了 image ,已配置 Docker ,但应用程序 Docker 不想开始。我怎样才能解决这个问题?
我尝试了不同的方法来修复这个错误,例如,我在 gradle.build :

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

但没用。
grad尔:

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"]

清单.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
r6hnlfcb

r6hnlfcb1#

您正在应用程序中使用springboot,在您的构建中,您非常努力地避免使用它。简而言之,不要使用springbootgradle插件来构建一个合适的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引导jar,您可以在命令行上运行它。你也可以在docker图像中使用这个jar。
从springboot2.3开始,可以使用构建包或常规docker让springboot创建映像。
当使用构建包时,上面的 build.gradle 足以创造一个形象。快跑 ./gradlew bootBuildImage ,然后它将使用构建包生成一个映像。

相关问题