build.gradle.kts.proto.jinja 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. {%- set publish = target.publish -%}
  2. {%- set libraries = target.consumer|selectattr('type', 'eq', 'library') -%}
  3. import com.google.protobuf.gradle.*
  4. val baseBuildDir = "{{ export_root }}/gradle.build/"
  5. buildDir = file(baseBuildDir + project.path.replaceFirst(":", "/").replace(":", "."))
  6. subprojects {
  7. buildDir = file(baseBuildDir + project.path.replaceFirst(":", "/").replace(":", "."))
  8. }
  9. val mainProtosDir = File(buildDir, "main_protos")
  10. {%- if libraries|length %}
  11. val mainExtractedIncludeProtosDir = File(buildDir, "extracted-include-protos/main")
  12. {%- endif %}
  13. plugins {
  14. id("java-library")
  15. id("com.google.protobuf") version "0.8.19"
  16. {%- if publish %}
  17. `maven-publish`
  18. `signing`
  19. {%- endif %}
  20. }
  21. {%- if publish %}
  22. group = "{{ target.publish_group }}"
  23. version = {% if target.publish_version -%}"{{ target.publish_version }}"{%- else -%}project.properties["version"]{%- endif %}
  24. {%- endif %}
  25. val bucketUsername: String by project
  26. val bucketPassword: String by project
  27. repositories {
  28. repositories {
  29. maven {
  30. url = uri("https://bucket.yandex-team.ru/v1/maven/central")
  31. credentials {
  32. username = "$bucketUsername"
  33. password = "$bucketPassword"
  34. }
  35. }
  36. }
  37. }
  38. val project_root = "{{ arcadia_root }}"
  39. sourceSets {
  40. main {
  41. java.srcDir("$buildDir/generated/sources/proto/main/java")
  42. {%- if target.proto_grpc %}
  43. java.srcDir("$buildDir/generated/sources/proto/main/grpc")
  44. {%- endif %}
  45. }
  46. test {
  47. java.srcDir("$buildDir/generated/sources/proto/test/java")
  48. {%- if target.proto_grpc %}
  49. java.srcDir("$buildDir/generated/sources/proto/test/grpc")
  50. {%- endif %}
  51. }
  52. }
  53. java {
  54. withSourcesJar()
  55. withJavadocJar()
  56. }
  57. configurations.api {
  58. isTransitive = false
  59. }
  60. configurations.implementation {
  61. isTransitive = false
  62. }
  63. configurations.testImplementation {
  64. isTransitive = false
  65. }
  66. {%- if target.jar_source_set is defined -%}
  67. {%- for source_set in target.jar_source_set -%}
  68. {%- set srcdir_glob = split(source_set, ':') %}
  69. sourceSets.main.java.srcDirs += "{{ srcdir_glob[0] }}"
  70. {% endfor -%}
  71. {%- endif %}
  72. dependencies {
  73. {%- for library in target.consumer if library.classpath -%}
  74. {%- if library.prebuilt and library.jar and (library.type != "contrib" or target.handler.build_contribs) %}
  75. implementation(files("$project_root/{{ library.jar }}"))
  76. {%- else -%}
  77. {%- set classpath = library.classpath -%}
  78. {%- if classpath|replace('"','') == classpath -%}
  79. {%- set classpath = '"' + classpath + '"' -%}
  80. {%- endif %}
  81. {%- if library.type != "contrib" %}
  82. implementation
  83. {%- else %}
  84. api
  85. {%- endif -%}({{ classpath }})
  86. {%- if library.excludes.consumer is defined %} {
  87. {% for exclude in library.excludes.consumer -%}
  88. {% set classpath = exclude.classpath|replace('"','') -%}
  89. {% set classpath_parts = split(classpath, ':') -%}
  90. exclude(group = "{{ classpath_parts[0] }}", module = "{{ classpath_parts[1] }}")
  91. {% endfor -%}
  92. }
  93. {%- endif -%}
  94. {%- endif -%}
  95. {%- endfor -%}
  96. {%- if target.proto_namespace %}
  97. protobuf(files(File(mainProtosDir, "{{ target.proto_namespace }}")))
  98. {%- else %}
  99. protobuf(files(mainProtosDir))
  100. {%- endif %}
  101. }
  102. protobuf {
  103. generatedFilesBaseDir = "$buildDir/generated/sources/proto"
  104. protoc {
  105. // Download from repositories
  106. artifact = "com.google.protobuf:protoc:{%- if target.proto_compiler_version -%}{{ target.proto_compiler_version }}{%- else -%}3.22.5{%- endif -%}"
  107. }
  108. {%- if target.proto_grpc %}
  109. plugins {
  110. id("grpc") {
  111. artifact = "io.grpc:protoc-gen-grpc-java:{%- if target.proto_grpc_version -%}{{ target.proto_grpc_version }}{%- else -%}1.45.0{%- endif -%}"
  112. }
  113. {%- if target.proto_kotlin_grpc %}
  114. id("grpckt") {
  115. artifact = "io.grpc:protoc-gen-grpc-kotlin:{%- if target.proto_kotlin_grpc_version -%}{{ target.proto_kotlin_grpc_version }}{%- else -%}1.3.1{%- endif -%}:jdk8@jar"
  116. }
  117. {%- endif %}
  118. }
  119. generateProtoTasks {
  120. all().forEach {
  121. it.plugins {
  122. id("grpc")
  123. {%- if target.proto_kotlin_grpc %}
  124. id("grpckt")
  125. {%- endif %}
  126. }
  127. {%- if target.proto_kotlin_grpc %}
  128. it.builtins {
  129. create("kotlin")
  130. }
  131. {%- endif %}
  132. }
  133. }
  134. {%- endif %}
  135. }
  136. val prepareMainProtos = tasks.register<Copy>("prepareMainProtos") {
  137. from("$project_root") {
  138. {#- list of all current project proto files -#}
  139. {%- for proto in target.proto_files %}
  140. include("{{ proto }}")
  141. {%- endfor %}
  142. }
  143. into(mainProtosDir)
  144. }
  145. {% if libraries|length -%}
  146. val extractMainLibrariesProtos = tasks.register<Copy>("extractMainLibrariesProtos") {
  147. from("$project_root") {
  148. {#- list of all library directories -#}
  149. {%- for library in libraries -%}
  150. {%- set path_and_jar = rsplit(library.jar, '/', 2) %}
  151. include("{{ path_and_jar[0] }}/**/*.proto")
  152. {%- endfor %}
  153. }
  154. into(mainExtractedIncludeProtosDir)
  155. }
  156. {% endif -%}
  157. afterEvaluate {
  158. tasks.getByName("extractProto").dependsOn(prepareMainProtos)
  159. {%- if libraries|length %}
  160. tasks.getByName("extractProto").dependsOn(extractMainLibrariesProtos)
  161. {%- endif %}
  162. }
  163. {# To avoid problems when build project with proto #}
  164. tasks.getByName("sourcesJar").dependsOn("generateProto")
  165. {# To disable redundant javadoc (it may fail the build) #}
  166. tasks.withType<Javadoc>().configureEach {
  167. isEnabled = false
  168. }
  169. {%- include "[generator]/debug.jinja" ignore missing -%}