build.gradle.kts.jinja 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. {%- set mainClass = target.app_main_class -%}
  2. {%- set publish = target.publish -%}
  3. {%- set with_kotlin = target.with_kotlin -%}
  4. {%- set kotlin_version = target.kotlin_version -%}
  5. {%- set hasJunit5Test = extra_targets|selectattr('junit5_test') -%}
  6. {%- set errorprone_plugin_version = "4.0.0" -%}
  7. plugins {
  8. {# some plugins configuration #}
  9. {%- for library in target.consumer if library.classpath -%}
  10. {# error prone plugin configuration #}
  11. {%- if library.prebuilt and library.jar and (library.type != "contrib" or build_contribs) and "contrib/java/com/google/errorprone/error_prone_annotations/" in library.jar -%}
  12. id("net.ltgt.errorprone") version "{{ errorprone_plugin_version }}"
  13. {%- endif -%}
  14. {%- endfor -%}
  15. {# lombok configuration #}
  16. {%- if "lombok.launch.AnnotationProcessorHider$AnnotationProcessor" in target.annotation_processors %}
  17. id("io.freefair.lombok") version "8.6"
  18. {%- endif -%}
  19. {%- if mainClass %}
  20. `application`
  21. {%- else %}
  22. `java-library`
  23. {%- endif %}
  24. {%- if publish %}
  25. `maven-publish`
  26. `signing`
  27. {%- endif -%}
  28. {%- if with_kotlin and kotlin_version %}
  29. kotlin("jvm") version "{{ kotlin_version }}"
  30. {%- if target.with_kotlinc_plugin_allopen %}
  31. kotlin("plugin.allopen") version "{{ kotlin_version }}"
  32. {% endif -%}
  33. {%- if target.with_kotlinc_plugin_lombok %}
  34. kotlin("plugin.lombok") version "{{ kotlin_version }}"
  35. {% endif -%}
  36. {%- if target.with_kotlinc_plugin_noarg %}
  37. kotlin("plugin.noarg") version "{{ kotlin_version }}"
  38. {% endif -%}
  39. {%- if target.with_kotlinc_plugin_serialization %}
  40. kotlin("plugin.serialization") version "{{ kotlin_version }}"
  41. {% endif -%}
  42. {%- endif %}
  43. }
  44. {# language level #}
  45. {%- if target.required_jdk != "" %}
  46. java {
  47. toolchain {
  48. languageVersion = JavaLanguageVersion.of("{{ target.required_jdk }}")
  49. }
  50. }
  51. {% endif -%}
  52. {%- if target.with_kotlinc_plugin_allopen %}
  53. allOpen {
  54. annotation("org.springframework.stereotype.Component")
  55. }
  56. {% endif -%}
  57. {%- if with_kotlin %}
  58. kotlin {
  59. jvmToolchain({%- if target.required_jdk -%}{{ target.required_jdk }}{%- else -%}17{%- endif -%})
  60. }
  61. {% endif -%}
  62. {%- if publish %}
  63. group = "{{ target.publish_group }}"
  64. version = {% if target.publish_version and target.publish_version != "no" -%}"{{ target.publish_version }}"{%- else -%}project.properties["version"]!!{%- endif %}
  65. {% endif %}
  66. {% if target.enable_preview %}
  67. tasks.withType<JavaCompile> {
  68. options.compilerArgs.add("--enable-preview")
  69. options.compilerArgs.add("-Xlint:preview")
  70. options.release.set({%- if target.required_jdk -%}{{ target.required_jdk }}{%- else -%}17{%- endif -%})
  71. }
  72. tasks.withType<JavaExec> {
  73. jvmArgs?.add("--enable-preview")
  74. }
  75. tasks.withType<Test> {
  76. jvmArgs?.add("--enable-preview")
  77. environment["JAVA_TOOL_OPTIONS"] = "--enable-preview"
  78. }
  79. tasks.withType<Javadoc> {
  80. val javadocOptions = options as CoreJavadocOptions
  81. javadocOptions.addStringOption("source", "{%- if target.required_jdk -%}{{ target.required_jdk }}{%- else -%}17{%- endif -%}")
  82. javadocOptions.addBooleanOption("-enable-preview", true)
  83. }
  84. {% endif %}
  85. {# javac flags #}
  86. {%- if (target.javac.flags is defined) and (target.javac.flags|length) %}
  87. tasks.withType<JavaCompile> {
  88. {%- for javac_flag in target.javac.flags %}
  89. {%- if '-Xep:' in javac_flag %}
  90. {% else %}
  91. options.compilerArgs.add("{{ javac_flag }}")
  92. {%- endif %}
  93. {%- endfor %}
  94. }
  95. {%- endif %}
  96. val bucketUsername: String by project
  97. val bucketPassword: String by project
  98. repositories {
  99. repositories {
  100. maven {
  101. url = uri("https://bucket.yandex-team.ru/v1/maven/central")
  102. credentials {
  103. username = "$bucketUsername"
  104. password = "$bucketPassword"
  105. }
  106. }
  107. }
  108. }
  109. val project_root="{%- if export_root.startswith(arcadia_root + '/') -%}{{ arcadia_root }}{%- else -%}{{ export_root }}/.hic_sunt_dracones{%- endif -%}"
  110. {% if mainClass -%}
  111. application {
  112. mainClass.set("{{ mainClass }}")
  113. }
  114. {% endif -%}
  115. java {
  116. withSourcesJar()
  117. withJavadocJar()
  118. }
  119. configurations.api {
  120. isTransitive = false
  121. }
  122. configurations.implementation {
  123. isTransitive = false
  124. }
  125. configurations.testImplementation {
  126. isTransitive = false
  127. }
  128. {% if has_test -%}
  129. val testsJar by tasks.registering(Jar::class) {
  130. dependsOn(JavaPlugin.COMPILE_TEST_JAVA_TASK_NAME)
  131. archiveClassifier.set("tests")
  132. from(sourceSets["test"].output)
  133. }
  134. artifacts.add(configurations.create("testArtifacts").name, testsJar)
  135. tasks.test {
  136. testLogging {
  137. showStandardStreams = true
  138. events("passed", "skipped", "failed")
  139. }
  140. }
  141. {% endif -%}
  142. {%- if target.jar_source_set is defined -%}
  143. {%- for source_set in target.jar_source_set -%}
  144. {%- set srcdir_glob = split(source_set, ':') -%}
  145. sourceSets.main.java.srcDirs += "{{ srcdir_glob[0] }}"
  146. {% endfor -%}
  147. {%- endif -%}
  148. {% for extra_target in extra_targets -%}
  149. {%- if extra_target.jar_source_set is defined -%}
  150. {%- for source_set in extra_target.jar_source_set -%}
  151. {%- set srcdir_glob = split(source_set, ':') -%}
  152. sourceSets.main.java.srcDirs += "{{ srcdir_glob[0] }}"
  153. {% endfor -%}
  154. {%- endif -%}
  155. {%- endfor -%}
  156. sourceSets {
  157. val test by getting {
  158. java.srcDir("ut/java")
  159. resources.srcDir("ut/resources")
  160. java.srcDir("src/test-integration/java")
  161. resources.srcDir("src/test-integration/resources")
  162. java.srcDir("src/testFixtures/java")
  163. resources.srcDir("src/testFixtures/resources")
  164. java.srcDir("src/intTest/java")
  165. resources.srcDir("src/intTest/resources")
  166. }
  167. }
  168. dependencies {
  169. {%- for library in target.consumer if library.classpath -%}
  170. {# error prone plugin configuration #}
  171. {%- if library.prebuilt and library.jar and (library.type != "contrib" or build_contribs) and "contrib/java/com/google/errorprone/error_prone_annotations" in library.jar -%}
  172. {% set errorprone_version = library.jar -%}
  173. {% set errorprone_parts = errorprone_version|replace("contrib/java/com/google/errorprone/error_prone_annotations/") -%}
  174. {% set errorprone_parts = split(errorprone_parts, '/') -%}
  175. errorprone("com.google.errorprone:error_prone_core:{{ errorprone_parts[0] }}")
  176. {%- endif -%}
  177. {%- if library.prebuilt and library.jar and (library.type != "contrib" or build_contribs) %}
  178. implementation(files("$project_root/{{ library.jar }}"))
  179. {%- else -%}
  180. {%- set classpath = library.classpath -%}
  181. {%- if classpath|replace('"','') == classpath -%}
  182. {%- set classpath = '"' + classpath + '"' -%}
  183. {%- endif -%}
  184. {%- if library.type != "contrib" %}
  185. {%- if library.testdep %}
  186. implementation(project(path = ":{{ library.testdep | replace("/", ":") }}", configuration = "testArtifacts"))
  187. {%- else %}
  188. implementation({{ classpath }})
  189. {%- endif -%}
  190. {%- else %}
  191. api({{ classpath }})
  192. {%- endif -%}
  193. {%- if library.excludes.consumer is defined %} {
  194. {% for exclude in library.excludes.consumer if exclude.classpath -%}
  195. {% set classpath = exclude.classpath|replace('"','') -%}
  196. {% set classpath_parts = split(classpath, ':') -%}
  197. exclude(group = "{{ classpath_parts[0] }}", module = "{{ classpath_parts[1] }}")
  198. {% endfor -%}
  199. }
  200. {%- endif -%}
  201. {%- endif -%}
  202. {%- endfor -%}
  203. {%- for extra_target in extra_targets -%}
  204. {%- for library in extra_target.consumer if library.classpath -%}
  205. {%- if library.prebuilt and library.jar and (library.type != "contrib" or build_contribs) %}
  206. testImplementation(files("$project_root/{{ library.jar }}"))
  207. {%- else -%}
  208. {%- set classpath = library.classpath -%}
  209. {%- if classpath|replace('"','') == classpath -%}
  210. {%- set classpath = '"' + classpath + '"' -%}
  211. {%- endif %}
  212. {%- if library.type != "contrib" and library.testdep %}
  213. testImplementation(project(path = ":{{ library.testdep | replace("/", ":") }}", configuration = "testArtifacts"))
  214. {%- else %}
  215. testImplementation({{ classpath }})
  216. {%- endif -%}
  217. {%- if library.excludes.consumer is defined %} {
  218. {% for exclude in library.excludes.consumer if exclude.classpath -%}
  219. {% set classpath = exclude.classpath|replace('"','') -%}
  220. {% set classpath_parts = split(classpath, ':') -%}
  221. exclude(group = "{{ classpath_parts[0] }}", module = "{{ classpath_parts[1] }}")
  222. {% endfor -%}
  223. }
  224. {%- endif -%}
  225. {%- endif -%}
  226. {%- endfor -%}
  227. {%- endfor %}
  228. }
  229. {% if hasJunit5Test -%}
  230. tasks.named<Test>("test") {
  231. useJUnitPlatform()
  232. }
  233. {% endif -%}
  234. {# run_java_program #}
  235. {# {% set runs = targets|selectattr("runs") -%} #}
  236. {% set runs = target.runs -%}
  237. {% if runs -%}
  238. {% for run in runs -%}
  239. val runJav{{ loop.index }} = task<JavaExec>("runJavaProgram{{ loop.index }}") {
  240. group = "build"
  241. description = "Code generation by run java program"
  242. mainClass.set("{{ run.args[0] }}")
  243. {% if run.classpath -%}
  244. {% for classpath in run.classpath -%}
  245. {% set real_classpath = classpath|replace('@', '') -%}
  246. {% set real_classpath = real_classpath|replace('.run.cp', '') -%}
  247. {% set real_classpath = real_classpath|replace('.cplst', '') -%}
  248. {% set real_classpath = real_classpath|replace(export_root, '') -%}
  249. {% set real_gradle_classpath = real_classpath|replace('/', ':') %}
  250. val classPath = "{{ real_gradle_classpath }}"
  251. val classPathParts = classPath.split(":")
  252. classPathParts[classPathParts.size - 2]
  253. classpath = files("$project_root{{ real_classpath }}") + project(classPath.replace(":${classPathParts[classPathParts.size - 2]}.jar", "")).configurations.runtimeClasspath.get()
  254. {% endfor -%}
  255. {% else -%}
  256. classpath = sourceSets.main.get().runtimeClasspath
  257. {% endif -%}
  258. {% set args = run.args -%}
  259. {% if args -%}
  260. val argsList = mutableListOf(
  261. {% for arg in args -%}
  262. "{{ arg }}",
  263. {% endfor -%}
  264. )
  265. argsList.removeAt(0)
  266. args = argsList
  267. {% endif -%}
  268. {% if run.in_dir -%}
  269. {% for dir in run.in_dir -%}
  270. inputs.files(fileTree("{{ dir }}"))
  271. {% endfor -%}
  272. {% endif -%}
  273. {% if run.in -%}
  274. {% for file in run.in -%}
  275. inputs.files("{{ file }}")
  276. {% endfor -%}
  277. {% endif -%}
  278. {% if run.out_dir -%}
  279. {% for dir in run.out_dir -%}
  280. outputs.dir("{{ dir }}")
  281. {% endfor -%}
  282. {#
  283. Не использованы аттрибуты
  284. run-cwd="str"
  285. run-in_dirs_inputs="list"
  286. run-in_noparse="list"
  287. run-out_dir="list"
  288. run-tool="list"
  289. #}
  290. {% endif -%}
  291. }
  292. tasks {
  293. build {
  294. dependsOn(runJav{{ loop.index }})
  295. }
  296. }
  297. {% endfor -%}
  298. {% endif -%}
  299. {% include "extra-tests.gradle.kts" ignore missing %}
  300. {% if publish -%}
  301. {% include 'publish.gradle.kts' ignore missing -%}
  302. {% endif -%}
  303. {# To disable redundant javadoc (it may fail the build) #}
  304. tasks.withType<Javadoc>().configureEach {
  305. isEnabled = false
  306. }
  307. {%- include "[generator]/debug.jinja" ignore missing -%}