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
+
1
9
# Kotlin Rules for Bazel
2
10
[ ![ Build Status] ( https://travis-ci.org/pubref/rules_kotlin.svg?branch=master )] ( https://travis-ci.org/pubref/rules_kotlin )
3
11
@@ -9,6 +17,7 @@ These rules are for building [Kotlin][kotlin] source with with
9
17
1 . [ kotlin_repositories] ( #kotlin_repositories )
10
18
1 . [ kotlin_library] ( #kotlin_library )
11
19
1 . [ kotlin_binary] ( #kotlin_binary )
20
+ 1 . [ kotlin_test] ( #kotlin_test )
12
21
13
22
## Workspace rules
14
23
@@ -18,7 +27,7 @@ Add the following to your `WORKSPACE` file:
18
27
git_repository(
19
28
name = " org_pubref_rules_kotlin" ,
20
29
remote = " https://github.com/pubref/rules_kotlin.git" ,
21
- tag = " v0.3.0 " , # update as needed
30
+ tag = " v0.3.1 " , # update as needed
22
31
)
23
32
24
33
load(" @org_pubref_rules_kotlin//kotlin:rules.bzl" , " kotlin_repositories" )
@@ -40,7 +49,7 @@ dagger (used to build the `KotlinCompiler` bazel worker).
40
49
Add the following to your BUILD file:
41
50
42
51
``` 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" )
44
53
```
45
54
46
55
### kotlin_library
@@ -95,18 +104,18 @@ android_binary(
95
104
| ` srcs ` | ` label_list ` | Kotlin source files ` *.kt ` |
96
105
| ` deps ` | ` label_list ` | List of ` kotlin_library ` targets |
97
106
| ` 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 ` ) |
98
108
| ` jars ` | ` label_list ` | List of jar file targets (` *.jar ` ) |
99
109
| ` x_opts ` | ` string_list ` | List of additional ` -X ` options to ` kotlinc ` |
100
110
| ` 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 ) . |
102
111
103
112
104
113
### kotlin_binary
105
114
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:
110
119
111
120
``` python
112
121
kotlin_binary(
@@ -128,6 +137,8 @@ Target :main_kt_deploy.jar up-to-date:
128
137
$ java -jar ./bazel-bin/.../main_kt_deploy.jar
129
138
```
130
139
140
+ > The ` kotlin-runtime.jar ` is implicitly included by the ` kotlin_binary ` rule.
141
+
131
142
#### kotlin_binary attributes
132
143
133
144
Includes all ` kotlin_library ` attributes as well as:
@@ -137,25 +148,52 @@ Includes all `kotlin_library` attributes as well as:
137
148
| ` main_class ` | ` string ` | Main class to run with the ` kotlin_binary ` rule |
138
149
139
150
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
+
140
178
### kotlin_compile
141
179
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.
146
182
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.
149
187
150
188
# Summary
151
189
152
190
That's it! Hopefully these rules with make it easy to mix kotlin and
153
191
traditional java code in your projects and take advantage of bazel's
154
192
approach to fast, repeatable, and reliable builds.
155
193
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.
159
197
160
198
## Examples
161
199
@@ -167,13 +205,14 @@ $ cd rules_kotlin
167
205
$ bazel query //... --output label_kind
168
206
$ bazel run examples/helloworld:main_kt
169
207
$ bazel run examples/helloworld:main_java
208
+ $ bazel test examples/helloworld:main_test
209
+ $ bazel test examples/helloworld:main_kt_test
170
210
```
171
211
172
212
## TODO
173
213
174
- 1 . Implement a ` kotlin_test ` rule.
175
214
1 . Proper ` data ` and runfiles support.
176
- 2 . Android support.
215
+ 2 . Proper android support.
177
216
4 . kapt support.
178
217
3 . Incremental compilation.
179
218
0 commit comments