Search code examples
mavenkotlinintellij-idea

Kotlinx serialisation not working with maven


I have followed the instructions provided on the kotlinx.serialization github page but when I try to serialize my class, it throws the following runtime exception

Mark the class as @Serializable or provide the serializer explicitly.

Even though the I have marked the class with @Serializable.

My assumption is that compiler plugin is not kicking in, since the dependency is not downloaded in my m2 repository kotlin-maven-serialization is missing But IntelliJ is not showing any error in my pom file

Here's my pom file

  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>net.Kilobyte1000</groupId>
    <artifactId>ElectFX</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>14</maven.compiler.source>
        <maven.compiler.target>14</maven.compiler.target>
        <kotlin.version>1.4.21</kotlin.version>
        <serialization.version>1.0.1</serialization.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>14</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>14</version>
        </dependency>
        <dependency>
            <groupId>org.jfxtras</groupId>
            <artifactId>jmetro</artifactId>
            <version>11.6.12</version>
        </dependency>
        <dependency>
            <groupId>com.jfoenix</groupId>
            <artifactId>jfoenix</artifactId>
            <version>9.0.10</version>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-stdlib-jdk8</artifactId>
            <version>${kotlin.version}</version>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-test</artifactId>
            <version>${kotlin.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-text</artifactId>
            <version>1.9</version>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlinx</groupId>
            <artifactId>kotlinx-serialization-json-jvm</artifactId>
            <version>${serialization.version}</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>0.0.4</version>
                <configuration>
                    <mainClass>edu.opjms.candidateSelector.main.Main</mainClass>
                    <launcher>start</launcher>
                    <jlinkImageName>ElectFX</jlinkImageName>
                    <compress>2</compress>
                    <noManPages>true</noManPages>
                    <noHeaderFiles>true</noHeaderFiles>
                </configuration>
            </plugin>
            <plugin>
                <groupId>com.gluonhq</groupId>
                <artifactId>client-maven-plugin</artifactId>
                <version>0.1.26</version>
                <configuration>
                    <mainClass>edu.opjms.candidateSelector.main.Main</mainClass>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-maven-plugin</artifactId>
                <version>${kotlin.version}</version>
                <executions>
                    <execution>
                        <id>compile</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>test-compile</id>
                        <phase>test-compile</phase>
                        <goals>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <jvmTarget>14</jvmTarget>
                    <compilerPlugins>
                        <plugin>kotlinx-serialization</plugin>
                    </compilerPlugins>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.jetbrains.kotlin</groupId>
                        <artifactId>kotlin-maven-serialization</artifactId>
                        <version>1.4.21</version>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <executions>
                    <execution>
                        <id>compile</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>testCompile</id>
                        <phase>test-compile</phase>
                        <goals>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <release>14</release>
                    <source>14</source>
                    <target>14</target>
                </configuration>
            </plugin>

        </plugins>
    </build>
</project>

Update: I ran kotlin:compile from maven which downloaded the plugin, but gives this error

[INFO] Scanning for projects...
[INFO] 
[INFO] ----------------------< net.Kilobyte1000:ElectFX >----------------------
[INFO] Building ElectFX 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- kotlin-maven-plugin:1.4.21:compile (default-cli) @ ElectFX ---
[INFO] Applied plugin: 'kotlinx-serialization'
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.intellij.util.ReflectionUtil (file:/C:/Users/Name/.m2/repository/org/jetbrains/kotlin/kotlin-compiler/1.4.21/kotlin-compiler-1.4.21.jar) to method java.util.ResourceBundle.setParent(java.util.ResourceBundle)
WARNING: Please consider reporting this to the maintainers of com.intellij.util.ReflectionUtil
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  7.557 s
[INFO] Finished at: 2021-01-25T12:16:21+05:30
[INFO] ------------------------------------------------------------------------

Solution

  • It turns out that the package containing the kotlin file needs to be opened in a modular project in order for compiler plugin to generate the serializer. Adding opens in module-info.java fixed it