1+ <table ><tr >
2+ <td ><img src =" https://github.com/pubref/rules_protobuf/blob/master/images/bazel.png " width =" 120 " /></td >
3+ <td ><img src =" https://kotlinlang.org/assets/images/open-graph/kotlin_250x250.png " width =" 120 " /></td >
4+ </tr ><tr >
5+ <td >Bazel</td >
6+ <td >Kotlin</td >
7+ </tr ></table >
8+
19# Kotlin Rules for Bazel
210[ ![ Build Status] ( https://travis-ci.org/pubref/rules_kotlin.svg?branch=master )] ( https://travis-ci.org/pubref/rules_kotlin )
311
@@ -9,6 +17,7 @@ These rules are for building [Kotlin][kotlin] source with with
9171 . [ kotlin_repositories] ( #kotlin_repositories )
10181 . [ kotlin_library] ( #kotlin_library )
11191 . [ kotlin_binary] ( #kotlin_binary )
20+ 1 . [ kotlin_test] ( #kotlin_test )
1221
1322## Workspace rules
1423
@@ -18,7 +27,7 @@ Add the following to your `WORKSPACE` file:
1827git_repository(
1928 name = " org_pubref_rules_kotlin" ,
2029 remote = " https://github.com/pubref/rules_kotlin.git" ,
21- tag = " v0.3.0 " , # update as needed
30+ tag = " v0.3.1 " , # update as needed
2231)
2332
2433load(" @org_pubref_rules_kotlin//kotlin:rules.bzl" , " kotlin_repositories" )
@@ -40,7 +49,7 @@ dagger (used to build the `KotlinCompiler` bazel worker).
4049Add the following to your BUILD file:
4150
4251``` python
43- load(" @org_pubref_rules_kotlin//kotlin:rules.bzl" , " kotlin_library" , " kotlin_binary " )
52+ load(" @org_pubref_rules_kotlin//kotlin:rules.bzl" , " kotlin_library" )
4453```
4554
4655### kotlin_library
@@ -95,18 +104,18 @@ android_binary(
95104| ` srcs ` | ` label_list ` | Kotlin source files ` *.kt ` |
96105| ` deps ` | ` label_list ` | List of ` kotlin_library ` targets |
97106| ` java_deps ` | ` label_list ` | List of java provider targets (` java_library ` , ` java_import ` , ` ... ` ) |
107+ | ` android_deps ` | ` label_list ` | List of android provider targets (` android_library ` ) |
98108| ` jars ` | ` label_list ` | List of jar file targets (` *.jar ` ) |
99109| ` x_opts ` | ` string_list ` | List of additional ` -X ` options to ` kotlinc ` |
100110| ` plugin_opts ` | ` string_dict ` | List of additional ` -P ` options to ` kotlinc ` |
101- | ` use_worker ` | ` boolean ` | Assign to ` False ` to disable the use of [ bazel workers] ( https://bazel.build/blog/2015/12/10/java-workers.html ) . |
102111
103112
104113### kotlin_binary
105114
106- A ` kotlin_binary ` rule takes the same arguments as a ` kotlin_library ` ,
107- plus a required ` main_class ` argument (the name of the compiled kotlin
108- class to run, in java package notation). This class should have a
109- ` fun main(...) ` entrypoint. Example:
115+ A ` kotlin_binary ` macro takes the same arguments as a
116+ ` kotlin_library ` , plus a required ` main_class ` argument (the name of
117+ the compiled kotlin class to run, in java package notation). This
118+ class should have a ` fun main(...) ` entrypoint. Example:
110119
111120``` python
112121kotlin_binary(
@@ -128,6 +137,8 @@ Target :main_kt_deploy.jar up-to-date:
128137$ java -jar ./bazel-bin/.../main_kt_deploy.jar
129138```
130139
140+ > The ` kotlin-runtime.jar ` is implicitly included by the ` kotlin_binary ` rule.
141+
131142#### kotlin_binary attributes
132143
133144Includes all ` kotlin_library ` attributes as well as:
@@ -137,25 +148,52 @@ Includes all `kotlin_library` attributes as well as:
137148| ` main_class ` | ` string ` | Main class to run with the ` kotlin_binary ` rule |
138149
139150
151+ ### kotlin_test
152+
153+ The ` kotlin_test ` rule is nearly identical the ` kotlin_binary ` rule
154+ (other than calling ` java_test ` internally rather than ` java_binary ` ).
155+
156+
157+ ``` python
158+ kotlin_test(
159+ name = " main_kt_test" ,
160+ test_class = " examples.helloworld.MainKtTest" ,
161+ srcs = [" MainKtTest.kt" ],
162+ size = " small" ,
163+ deps = [
164+ " :rules" ,
165+ ],
166+ java_deps = [
167+ " @junit4//jar" ,
168+ ],
169+ )
170+ ```
171+
172+ ``` sh
173+ $ bazel test :main_kt_test.jar
174+ ```
175+
176+ > The ` kotlin-test.jar ` is implicitly included by the ` kotlin_test ` rule.
177+
140178### kotlin_compile
141179
142- The ` kotlin_compile ` rule runs the ` kotlinc ` tool to generate a ` .jar `
143- file from a list of kotlin source files. The ` kotlin_library ` rule
144- (actually, macro) calls this internally and then makes the jarfile
145- available to other java rules via a ` java_import ` rule.
180+ > TL;DR; You most likely do not need to interact with the
181+ > ` kotlin_compile ` rule directly.
146182
147- In summary, you most likely do not need to interact with the
148- ` kotlin_compile ` rule directly.
183+ The ` kotlin_compile ` rule runs the kotlin compiler to generate a
184+ ` .jar ` file from a list of kotlin source files. The ` kotlin_library `
185+ rule calls this internally and then makes the jarfile available to
186+ other java rules via a ` java_import ` rule.
149187
150188# Summary
151189
152190That's it! Hopefully these rules with make it easy to mix kotlin and
153191traditional java code in your projects and take advantage of bazel's
154192approach to fast, repeatable, and reliable builds.
155193
156- > Note: if you have a bunch of maven (central) dependencies, consider
157- > [ rules_maven ] ( https://github.com/pubref/rules_maven ) for taming the
158- > issue of transitive dependencies with your java/kotlin projects.
194+ > Note: Consider [ rules_maven ] ( https://github.com/pubref/rules_maven )
195+ > for handling transitive maven dependencies with your java/kotlin
196+ > projects.
159197
160198## Examples
161199
@@ -167,13 +205,14 @@ $ cd rules_kotlin
167205$ bazel query //... --output label_kind
168206$ bazel run examples/helloworld:main_kt
169207$ bazel run examples/helloworld:main_java
208+ $ bazel test examples/helloworld:main_test
209+ $ bazel test examples/helloworld:main_kt_test
170210```
171211
172212## TODO
173213
174- 1 . Implement a ` kotlin_test ` rule.
1752141 . Proper ` data ` and runfiles support.
176- 2 . Android support.
215+ 2 . Proper android support.
1772164 . kapt support.
1782173 . Incremental compilation.
179218
0 commit comments