kotlin - 将 Kotlin JavaScript 代码拆分为多个 maven 模块

标签 kotlin

我有三个模块:客户端、共享、服务器。

Server 是一个“普通”的 Kotlin 模块,编译为 JVM ByteCode。
Share 应该编译成 JS 和 JVM ByteCod
客户端仅编译为 JS。

我已经设置共享来编译 JS 和 ByteCode - 这工作正常。问题是,客户端模块无法编译,因为它看不到来自客户端的类。

Kotlin 代码:

import com.a.typical.super.long.package.which.contains.ModelId

interface ClientOrca {

  fun test(): ModelId
}

[ERROR] <ClientKt>:[1,8] Unresolved reference: com
[ERROR] <ClientKt>:[5,15] Unresolved reference: ModelId

如何设置一个项目(使用 maven),以便将我的代码(拆分为多个 maven 模块)一次性编译到一个 JS 文件中?

最佳答案

解决方案确实在github examples Kotlin 的。这是一个摘要:

  • 该库必须生成一个包含 js 文件和 *.class 文件的 jar 文件。为此,您必须告诉 kotlin 编译器生成 js 文件并将它们放入类文件夹中,并将所有 kotlin 文件编译为类目录(目标/类)中的 *.class 文件
        <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>js</goal>
                        <goal>compile</goal>
                    </goals>
                    <configuration>
                        <sourceDirs>
                            <sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
                            <sourceDir>${project.basedir}/src/main/java</sourceDir>
                        </sourceDirs>
                        <outputFile>${project.build.outputDirectory}/${project.artifactId}.js</outputFile>  <!-- super important! -->
                    </configuration>
                </execution>
                <execution>
                    <id>test-compile</id>
                    <phase>test-compile</phase>
                    <goals>
                        <goal>test-js</goal>
                        <goal>test-compile</goal>
                    </goals>
                    <configuration>
                        <sourceDirs>
                            <sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
                            <sourceDir>${project.basedir}/src/test/java</sourceDir>
                        </sourceDirs>
                    </configuration>
                </execution>
            </executions>
            <!-- Insert these lines -->
            <configuration>
                <moduleKind>commonjs</moduleKind>
            </configuration>
            <!-- end of inserted text -->
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.0.2</version>
            <configuration>
                <forceCreation>true</forceCreation>
                <includes>
                    <include>**/*</include>
                </includes>
                <archive>
                    <forced/>
                    <manifestEntries>
                        <Built-By>Me</Built-By>
                        <Implementation-Vendor>Me</Implementation-Vendor>
                        <Implementation-Version>1.0.0</Implementation-Version>
                    </manifestEntries>
                </archive>
            </configuration>
        </plugin>
    
  • 在使用该库的 js 项目中,您必须告诉 maven 解压缩依赖项并使 js 文件可用。通过以下方式更改 pom.xml:
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>unpack</id>
                    <phase>compile</phase>
                    <goals>
                        <goal>unpack</goal>
                    </goals>
                    <configuration>
                        <artifactItems>
                            <artifactItem>
                                <groupId>org.jetbrains.kotlin</groupId>
                                <artifactId>kotlin-stdlib-js</artifactId>
                                <version>${kotlin.version}</version>
                                <outputDirectory>${project.build.directory}/js/lib</outputDirectory>
                                <includes>*.js</includes>
                            </artifactItem>
                            <artifactItem>
                                <groupId>ch.viseon.openOrca</groupId>
                                <artifactId>share</artifactId>
                                <version>${project.version}</version>
                                <outputDirectory>${project.build.directory}/js/lib</outputDirectory>
                                <includes>*.js</includes>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    
  • 关于kotlin - 将 Kotlin JavaScript 代码拆分为多个 maven 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42734278/

    相关文章:

    kotlin - kotlin 中类的私有(private)可见性修饰符的范围

    kotlin - 为什么在 build.gradle.kts 中不能使用 `const val`

    android - Kotlin 多层 IT 引用

    Android:在端口 9150 连接到 Tor SOCKS 代理抛出 `SocketException` ;仅当我安装 Tor Android 应用程序时才有效

    Kotlin 序列类型推断

    java - 如何在 Android Kotlin 中包装异步 Java 库?

    android - Activity 结果API返回的数据为空

    java - 将数据从 kotlin Activity 传递到 java DialogFragment

    kotlin - 如何在 kotlin 注释中写入 "/*"?

    syntax - Kotlin:访问 when 语句的参数