Home > Back-end >  Kotlin/Spring Boot Application Fails to Build in Maven using Docker
Kotlin/Spring Boot Application Fails to Build in Maven using Docker

Time:12-09

I am trying to build an image for Kotlin/Spring Boot application. But when I run docker build I get the following error:

[ERROR] Failed to execute goal org.jetbrains.kotlin:kotlin-maven-plugin:1.7.20:compile (compile) on project download-common: Compilation failure
[ERROR] java.lang.IllegalStateException: Unable to find extension point configuration extensions/compiler.xml (cp:
[ERROR]   null)
[ERROR]         at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment$Companion.registerApplicationExtensionPointsAndExtensionsFrom(KotlinCoreEnvironment.kt:612)
[ERROR]         at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment$Companion.createApplicationEnvironment(KotlinCoreEnvironment.kt:587)
[ERROR]         at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment$Companion.getOrCreateApplicationEnvironment(KotlinCoreEnvironment.kt:518)
[ERROR]         at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment$Companion.getOrCreateApplicationEnvironmentForProduction(KotlinCoreEnvironment.kt:499)
[ERROR]         at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment$Companion.createForProduction(KotlinCoreEnvironment.kt:443)
[ERROR]         at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.createCoreEnvironment(K2JVMCompiler.kt:192)
[ERROR]         at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:143)
[ERROR]         at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:53)
[ERROR]         at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:99)
[ERROR]         at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:47)
[ERROR]         at org.jetbrains.kotlin.cli.common.CLITool.exec(CLITool.kt:101)
[ERROR]         at org.jetbrains.kotlin.maven.KotlinCompileMojoBase.execCompiler(KotlinCompileMojoBase.java:228)
[ERROR]         at org.jetbrains.kotlin.maven.K2JVMCompileMojo.execCompiler(K2JVMCompileMojo.java:237)
[ERROR]         at org.jetbrains.kotlin.maven.K2JVMCompileMojo.execCompiler(K2JVMCompileMojo.java:55)
[ERROR]         at org.jetbrains.kotlin.maven.KotlinCompileMojoBase.execute(KotlinCompileMojoBase.java:209)
[ERROR]         at org.jetbrains.kotlin.maven.K2JVMCompileMojo.execute(K2JVMCompileMojo.java:222)
[ERROR]         at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:137)
[ERROR]         at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute2(MojoExecutor.java:370)
[ERROR]         at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute(MojoExecutor.java:351)
[ERROR]         at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:215)
[ERROR]         at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:171)
[ERROR]         at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:163)
[ERROR]         at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:117)
[ERROR]         at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:81)
[ERROR]         at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:56)
[ERROR]         at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
[ERROR]         at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:294)
[ERROR]         at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:192)
[ERROR]         at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:105)
[ERROR]         at org.apache.maven.cli.MavenCli.execute(MavenCli.java:960)
[ERROR]         at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:293)
[ERROR]         at org.apache.maven.cli.MavenCli.main(MavenCli.java:196)
[ERROR]         at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[ERROR]         at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[ERROR]         at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[ERROR]         at java.base/java.lang.reflect.Method.invoke(Method.java:566)
[ERROR]         at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:282)
[ERROR]         at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:225)
[ERROR]         at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:406)
[ERROR]         at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:347)
[ERROR]
[ERROR]
[ERROR] -> [Help 1]

The code compiles just fine when I run the same mvn clean install -e on my local machine without docker. After a bit of Googling, I added the below entry to plugins. The requiresUnpack section.

<plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring.boot.version}</version>
                <configuration>
                    <requiresUnpack>
                        <dependency>
                            <groupId>org.jetbrains.kotlin</groupId>
                            <artifactId>kotlin-compiler</artifactId>
                        </dependency>
                    </requiresUnpack>
                </configuration>
            </plugin>

My Dockerfile is:

# AS <NAME> to name this stage as maven
FROM scratch
LABEL MAINTAINER="Sam"

#SETUP Ubuntu ENVIRONMENT
FROM ubuntu:latest as ubuntu

RUN export http_proxy=MY_PROXY_URL && export https_proxy=MY_PROXY_HTTPS_URL

#INSTALL JAVA
FROM eclipse-temurin:11-jdk-alpine AS jdk    

#INSTALL MAVEN
FROM maven:3.8.6-eclipse-temurin-11-alpine AS maven

COPY settings.xml /usr/share/maven/conf/

RUN apk update && apk add git && apk add net-tools procps openssh-client openssh-server

RUN mkdir -p $HOME/images/lib/ && cd $HOME/images/lib/

RUN git clone MY_GIT_URL

WORKDIR /download_code

RUN git checkout feature/docker-branch

RUN mvn clean install -e

It is a SpringBoot Application with Kotlin. And if its of any use, I am on Windows 10.

But I still get the same error. Any pointers would be helpful.

CodePudding user response:

I am test ok.

$ cd MySpringBootPrj

$ docker run -v `pwd`:/data --workdir /data -v ~/.m2:/var/maven/.m2 -ti  -u 1000 -e MAVEN_CONFIG=/var/maven/.m2 maven:3.8.6-eclipse-temurin-17-alpine mvn -Duser.home=/var/maven clean package install
  • mapping MySpringBootPrj as pwd to /data(container)

  • --workdir point to /data(container)

  • mapping host ~/.m2 to container /var/maven/.m2

  • docker image is maven:3.8.6-eclipse-temurin-17-alpine

  • -u 1000 run as non-root

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo-11</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo-11</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
        <kotlin.version>1.7.21</kotlin.version>
        <kotlin.compiler.incremental>true</kotlin.compiler.incremental>
        <kotlin.compiler.languageVersion>1.7</kotlin.compiler.languageVersion>        
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.module</groupId>
            <artifactId>jackson-module-kotlin</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-reflect</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-stdlib-jdk8</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
        <testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-maven-plugin</artifactId>
                <configuration>
                    <args>
                        <arg>-Xjsr305=strict</arg>
                    </args>
                    <compilerPlugins>
                        <plugin>spring</plugin>
                    </compilerPlugins>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.jetbrains.kotlin</groupId>
                        <artifactId>kotlin-maven-allopen</artifactId>
                        <version>${kotlin.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

</project>

CodePudding user response:

if build jar is demo-11-0.0.1-SNAPSHOT.jar

Dockerfile

FROM eclipse-temurin:17.0.5_8-jdk-ubi9-minimal

EXPOSE 8080
WORKDIR /data
COPY ./demo-11-0.0.1-SNAPSHOT.jar /data/app.jar
RUN chown 1000:1000 /data/app.jar
USER 1000
ENTRYPOINT ["java", "-jar", "/data/app.jar"]

Build command

docker build --no-cache -t demoapp:0.0.1 .
docker tag demoapp:0.0.1 demoapp:latest

docker run -d -p 8080:8080 demoapp

curl http://localhost:8080/api/hello
  • Related