我有三个模块:客户端,共享,服务器。
服务器是"正常" 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文件?
答案 0 :(得分:1)
解决方案确实在kotlin的github examples。以下是摘要:
库必须生成一个包含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>