I really like the idea of coding a framework once, and then being able to pile it as jvm byte code as well as to javascript for web use.
Is this currently possible with the kotlin piler?
I really like the idea of coding a framework once, and then being able to pile it as jvm byte code as well as to javascript for web use.
Is this currently possible with the kotlin piler?
Share Improve this question asked Jun 5, 2015 at 16:33 Jack ShadeJack Shade 5015 silver badges13 bronze badges3 Answers
Reset to default 7It is possible but you may face some difficulties. First of all you can build and configure it only with Maven: just setup both executions. The second issue is that IDE can't deal with multiple targets so you can use tricks to enable/disable stdlib/kotlin-js-library
You can see it at https://github./Kotlin/kotlinx.html
It is multimodule project.. Module jvm is only piled for JVM, module js only to javascript, module shared to both
Notice maven profiles: when you edit shared module you can enable js or jvm but not both: otherwise IDE will go crazy. During pilation both profiles are active by default so you will get multitarget jar
I created a projet kotlin maven targeted JVM, it can be piled to both JVM and JS.
Open Intellij IDEA -> File -> new -> Project -> Maven -> check "create from archetype" -> choose "org.jetbrains.kotlin:kotlin-archetype-jvm"
Edit GroupId:.example.training; ArtifactId:kotlin2js; Version:1.0-SNAPSHOT
NOTE: a name of project (module) should not contains "-" (a dash) but "_" (an underscore) is ok.
Create a class kotlin named Person in path src/main/kotlin/.example.training/
data class Person ( val id, Int, val firstname: String)
Edit pom.xml
a) Add dependency "kotlin-stdlib.js"
<dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-stdlib-js</artifactId> <version>${kotlin.version}</version> </dependency>
b) Add goal "js" in plugin "kotlin-maven-plugin"
c) (Optional for interoperability Kotlin/JS) Add plugin for unpacking js files needed which are in lib "kotlin-stdlib-js"
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>unpack</id> <phase>pile</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> </artifactItems> </configuration> </execution> </executions> </plugin>
Run mvn clean pile
Folder classes contains Person.class for java. Folder js contains kotlin2js.js and kotlin2js.meta.js for JS, all js files unpacked are in child folder lib.
For some time this would cause the problems. However the idea is very good, so people keep asking.
Check my project https://github./C06A/KUrlet where I did just this: include shared code in the root-level module and included its source directory into sourceSets property of each submodule (one targeting JVM and one -- JS).
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744270807a4566091.html
评论列表(0条)