user14342151
user14342151

Reputation:

Gradle cannot build JAR with dependecies for Docker

I have a Spring MVC application that I want to add to Docker. I created the image, configured Docker, but the application in Docker does not want to start. How can I fix the problem?

I tried different ways to fix this error, for example, I added the following code to gradle.build:

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

But it didn't work.

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

ERRORS:

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

Upvotes: 3

Views: 1146

Answers (1)

M. Deinum
M. Deinum

Reputation: 124526

You are using Spring Boot in your application and in your build you are trying very hard to not use it. In short don't, use the Spring Boot Gradle plugin to build a proper 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"
    }
}

Now when you do ./gradlew build it will generate a proper Spring Boot jar that you can run on the command-line. This jar you can also use in your docker images.

As of Spring Boot 2.3 it is possible to let Spring Boot create the image as well using build packs or regular docker.

When using a build pack, the above build.gradle is enough to create an image. Just run ./gradlew bootBuildImage, it will then use build packs to generate an image.

Upvotes: 1

Related Questions