Kotlin/Spring Boot Application Fails to Build in Maven using Docker

575 views Asked by At

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.

1

There are 1 answers

0
Alexander On

This is the issue with custom classloading mechanism implemented in Kotlin library.

Check these two links:

The solution that worked for me:

tasks.named<BootJar>("bootJar") {
  requiresUnpack("**/kotlin-*.jar")
}

But this is gradle. Here's similar feature for maven:

<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  <executions>
    <execution>
      <goals>
        <goal>repackage</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <requiresUnpack>
      <dependency>
        <groupId>org.jetbrains.kotlin</groupId>
        <artifactId>kotlin-compiler-embeddable</artifactId>
      </dependency>
    </requiresUnpack>
  </configuration>
</plugin>

not sure if it works, maybe you will need to mark some more libraries.