能否基于Maven构建Kotlin Multiplatform项目?
Absolutely! You can absolutely build a Kotlin Multiplatform (KMP) library targeting JS, JVM (which naturally covers Java compatibility) using Maven—Kotlin's official tooling fully supports this setup, even though Gradle tends to get more focus in KMP documentation.
Here’s a step-by-step breakdown to make it work:
1. Use the Official Kotlin Maven Plugin
First, you’ll need the kotlin-maven-plugin (maintained by JetBrains), which includes full KMP support. Ensure your plugin version matches your Kotlin runtime version to avoid compatibility gaps.
2. Configure Your Target Platforms
In your pom.xml, set up the plugin to include both JVM and JS targets. The JVM target will produce a standard JAR that Java projects can depend on directly, while the JS target outputs artifacts compatible with browsers or Node.js.
Here’s a simplified plugin configuration snippet:
<build> <plugins> <plugin> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-maven-plugin</artifactId> <version>1.9.20</version> <executions> <execution> <id>compile</id> <goals> <goal>compile</goal> </goals> <configuration> <targets> <!-- JVM target (supports Java interoperability) --> <target> <name>jvm</name> <jdkVersion>11</jdkVersion> </target> <!-- JS target --> <target> <name>js</name> <moduleKind>umd</moduleKind> <!-- Or commonjs/es for specific environments --> <sourceMap>true</sourceMap> </target> </targets> </configuration> </execution> </executions> </plugin> </plugins> </build>
3. Follow the Source Code Structure
Maven uses a familiar convention for KMP, just like Gradle:
src/main/kotlin: Shared multiplatform code (usable by both JVM and JS)src/main/jvm: JVM-specific code (including any Java-interop logic you need)src/main/js: JS-specific code
The plugin will automatically compile these directories into platform-specific artifacts.
4. Set Up Dependencies
Include the appropriate Kotlin standard libraries for each target in your pom.xml:
<dependencies> <!-- Shared common library --> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-stdlib-common</artifactId> <version>1.9.20</version> </dependency> <!-- JVM-specific library --> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-stdlib-jdk8</artifactId> <version>1.9.20</version> <scope>compile</scope> </dependency> <!-- JS-specific library --> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-stdlib-js</artifactId> <version>1.9.20</version> <scope>compile</scope> </dependency> </dependencies>
Quick Notes to Keep in Mind
- Java Compatibility: The JVM target’s output is a standard JAR file—Java projects can add it as a dependency just like any other Java library, and call your Kotlin code seamlessly thanks to Kotlin’s built-in JVM interoperability.
- Minor Limitations: While Maven’s KMP support is fully functional for library building, it lacks some of Gradle’s advanced features like fine-grained task customization or incremental compilation optimizations. For most standard use cases, though, this won’t be a blocker.
- JS Flexibility: Adjust the
moduleKindin the JS target configuration to match your use case (e.g.,umdfor broad browser support,commonjsfor Node.js).
In short: Yes, Maven is a valid choice for your KMP library targeting JS, Kotlin, and Java. With the official plugin and proper configuration, you can build and publish artifacts that work across all three environments.
内容的提问来源于stack exchange,提问作者sab

