build.gradle.kts.jinja 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. val baseBuildDir = "{{ export_root }}/gradle.build/"
  2. buildDir = file(baseBuildDir + project.path.replaceFirst(":", "/").replace(":", "."))
  3. subprojects {
  4. buildDir = file(baseBuildDir + project.path.replaceFirst(":", "/").replace(":", "."))
  5. }
  6. {%- macro OutDirs(runs, prefix, suffix) -%}
  7. {%- if run.args|length and run.out_dir|length -%}
  8. {%- for out_dir in run.out_dir -%}
  9. {#- search all run arguments ended by /<out_dir> -#}
  10. {%- set out_dirs = select_by_ends(run.args, "/" + out_dir) -%}
  11. {%- if out_dirs|length %}
  12. {{ prefix }}{{ out_dirs|first }}{{ suffix }}
  13. {%- endif -%}
  14. {%- endfor -%}
  15. {%- endif -%}
  16. {%- endmacro %}
  17. {%- set mainClass = target.app_main_class -%}
  18. {%- set publish = target.publish -%}
  19. {%- set with_kotlin = target.with_kotlin -%}
  20. {%- set kotlin_version = target.kotlin_version -%}
  21. {%- set hasJunit5Test = extra_targets|selectattr('junit5_test') -%}
  22. {%- set errorprone_plugin_version = "4.0.0" -%}
  23. {%- if not target.required_jdk -%}
  24. {%- set has_required_jdk = false -%}
  25. {#- If no required JDK, set default JDK for Kotlin parts -#}
  26. {%- set required_jdk = '17' -%}
  27. {%- else -%}
  28. {%- set has_required_jdk = true -%}
  29. {#- Use JDK 23 instead 22 -#}
  30. {%- if target.required_jdk == '22' -%}
  31. {%- set required_jdk = '23' -%}
  32. {%- else -%}
  33. {%- set required_jdk = target.required_jdk -%}
  34. {%- endif -%}
  35. {%- endif %}
  36. plugins {
  37. {%- if mainClass %}
  38. `application`
  39. {%- else %}
  40. `java-library`
  41. {%- endif %}
  42. {%- if publish %}
  43. `maven-publish`
  44. `signing`
  45. {%- endif -%}
  46. {%- if with_kotlin and kotlin_version %}
  47. kotlin("jvm") version "{{ kotlin_version }}"
  48. {%- if target.with_kotlinc_plugin_allopen|length %}
  49. kotlin("plugin.allopen") version "{{ kotlin_version }}"
  50. {% endif -%}
  51. {%- if target.with_kotlinc_plugin_lombok|length %}
  52. kotlin("plugin.lombok") version "{{ kotlin_version }}"
  53. {% endif -%}
  54. {%- if target.with_kotlinc_plugin_noarg|length %}
  55. kotlin("plugin.noarg") version "{{ kotlin_version }}"
  56. {% endif -%}
  57. {%- if target.with_kotlinc_plugin_serialization|length %}
  58. kotlin("plugin.serialization") version "{{ kotlin_version }}"
  59. {% endif -%}
  60. {%- endif -%}
  61. {#- errorprone plugin configuration -#}
  62. {%- if target.consumer|selectattr('jar', 'startsWith', 'contrib/java/com/google/errorprone/error_prone_annotations')|length %}
  63. id("net.ltgt.errorprone") version "{{ errorprone_plugin_version }}"
  64. {%- endif -%}
  65. {#- lombok plugin configuration -#}
  66. {#- TODO remove usings annotation_processors semantic -#}
  67. {%- if ("lombok.launch.AnnotationProcessorHider$AnnotationProcessor" in target.annotation_processors) or (target.use_annotation_processor|length and target.use_annotation_processor|select('startsWith', 'contrib/java/org/projectlombok/lombok')|length) %}
  68. id("io.freefair.lombok") version "8.6"
  69. {%- endif %}
  70. }
  71. {#- language level -#}
  72. {%- if has_required_jdk %}
  73. java {
  74. toolchain {
  75. languageVersion = JavaLanguageVersion.of("{{ required_jdk }}")
  76. }
  77. }
  78. {% endif -%}
  79. {%- if target.with_kotlinc_plugin_allopen|length -%}
  80. {%- set allopen_annotations = [] -%}
  81. {%- if target.with_kotlinc_plugin_allopen|select('eq', 'preset=spring')|length -%}
  82. {%- set allopen_annotations = allopen_annotations + ['org.springframework.stereotype.Component', 'org.springframework.transaction.annotation.Transactional', 'org.springframework.scheduling.annotation.Async', 'org.springframework.cache.annotation.Cacheable', 'org.springframework.boot.test.context.SpringBootTest', 'org.springframework.validation.annotation.Validated'] -%}
  83. {%- endif -%}
  84. {%- if target.with_kotlinc_plugin_allopen|select('eq', 'preset=quarkus')|length -%}
  85. {%- set allopen_annotations = allopen_annotations + ['javax.enterprise.context.ApplicationScoped', 'javax.enterprise.context.RequestScoped'] -%}
  86. {%- endif -%}
  87. {%- if target.with_kotlinc_plugin_allopen|select('eq', 'preset=micronaut')|length -%}
  88. {%- set allopen_annotations = allopen_annotations + ['io.micronaut.aop.Around', 'io.micronaut.aop.Introduction', 'io.micronaut.aop.InterceptorBinding', 'io.micronaut.aop.InterceptorBindingDefinitions'] -%}
  89. {%- endif -%}
  90. {%- if target.with_kotlinc_plugin_allopen|select('startsWith', 'annotation=')|length -%}
  91. {%- set sannotations = target.with_kotlinc_plugin_allopen|select('startsWith', 'annotation=')|join('|')|replace('annotation=','') -%}
  92. {%- set annotations = split(sannotations, '|') -%}
  93. {%- set allopen_annotations = allopen_annotations + annotations -%}
  94. {%- endif -%}
  95. {%- set allopen_options = target.with_kotlinc_plugin_allopen|reject('startsWith', 'preset=')|reject('startsWith', 'annotation=')|reject('eq', 'default') %}
  96. allOpen {
  97. {%- if allopen_options|length -%}
  98. {%- for option in allopen_options|unique %}
  99. {{ option }}
  100. {%- endfor -%}
  101. {%- endif %}
  102. {%- if allopen_annotations|length -%}
  103. {%- for annotation in allopen_annotations|unique %}
  104. annotation("{{ annotation }}")
  105. {%- endfor -%}
  106. {%- endif %}
  107. }
  108. {% endif -%}
  109. {%- if target.with_kotlinc_plugin_noarg|length -%}
  110. {%- set noarg_annotations = [] -%}
  111. {%- if target.with_kotlinc_plugin_noarg|select('eq', 'preset=jpa')|length -%}
  112. {%- set noarg_annotations = noarg_annotations + ['javax.persistence.Entity', 'javax.persistence.Embeddable', 'javax.persistence.MappedSuperclass', 'jakarta.persistence.Entity', 'jakarta.persistence.Embeddable', 'jakarta.persistence.MappedSuperclass'] -%}
  113. {%- endif -%}
  114. {%- if target.with_kotlinc_plugin_noarg|select('startsWith', 'annotation=')|length -%}
  115. {%- set sannotations = target.with_kotlinc_plugin_noarg|select('startsWith', 'annotation=')|join('|')|replace('annotation=','') -%}
  116. {%- set annotations = split(sannotations, '|') -%}
  117. {%- set noarg_annotations = noarg_annotations + annotations -%}
  118. {%- endif -%}
  119. {%- set noarg_options = target.with_kotlinc_plugin_noarg|reject('startsWith', 'preset=')|reject('startsWith', 'annotation=')|reject('eq', 'default') %}
  120. noArg {
  121. {%- if noarg_options|length -%}
  122. {%- for option in noarg_options|unique %}
  123. {{ option }}
  124. {%- endfor -%}
  125. {%- endif %}
  126. {%- if noarg_annotations|length -%}
  127. {%- for annotation in noarg_annotations|unique %}
  128. annotation("{{ annotation }}")
  129. {%- endfor -%}
  130. {%- endif %}
  131. }
  132. {% endif -%}
  133. {%- if with_kotlin %}
  134. kotlin {
  135. jvmToolchain({{ required_jdk }})
  136. }
  137. {% endif -%}
  138. {%- if publish %}
  139. group = "{{ target.publish_group }}"
  140. version = {% if target.publish_version and target.publish_version != "no" -%}"{{ target.publish_version }}"{%- else -%}project.properties["version"]!!{%- endif %}
  141. {% endif -%}
  142. {%- if target.enable_preview %}
  143. tasks.withType<JavaCompile> {
  144. options.compilerArgs.add("--enable-preview")
  145. options.compilerArgs.add("-Xlint:preview")
  146. options.release.set({{ required_jdk }})
  147. }
  148. tasks.withType<JavaExec> {
  149. jvmArgs?.add("--enable-preview")
  150. }
  151. tasks.withType<Test> {
  152. jvmArgs?.add("--enable-preview")
  153. environment["JAVA_TOOL_OPTIONS"] = "--enable-preview"
  154. }
  155. tasks.withType<Javadoc> {
  156. val javadocOptions = options as CoreJavadocOptions
  157. javadocOptions.addStringOption("source", "{{ required_jdk }}")
  158. javadocOptions.addBooleanOption("-enable-preview", true)
  159. }
  160. {% endif -%}
  161. {#- javac flags -#}
  162. {%- if target.javac.flags|length -%}
  163. {%- set javac_flags = target.javac.flags|reject('startsWith', '-Xep:') -%}
  164. {%- if javac_flags|length %}
  165. tasks.withType<JavaCompile> {
  166. {%- for javac_flag in javac_flags %}
  167. options.compilerArgs.add("{{ javac_flag }}")
  168. {%- endfor %}
  169. }
  170. {%- endif -%}
  171. {%- endif %}
  172. val bucketUsername: String by project
  173. val bucketPassword: String by project
  174. repositories {
  175. repositories {
  176. maven {
  177. url = uri("https://bucket.yandex-team.ru/v1/maven/central")
  178. credentials {
  179. username = "$bucketUsername"
  180. password = "$bucketPassword"
  181. }
  182. }
  183. }
  184. }
  185. val project_root = "{{ arcadia_root }}"
  186. {% if mainClass -%}
  187. application {
  188. mainClass.set("{{ mainClass }}")
  189. }
  190. {% endif -%}
  191. java {
  192. withSourcesJar()
  193. withJavadocJar()
  194. }
  195. configurations.api {
  196. isTransitive = false
  197. }
  198. configurations.implementation {
  199. isTransitive = false
  200. }
  201. configurations.testImplementation {
  202. isTransitive = false
  203. }
  204. {%- if has_test %}
  205. val testsJar by tasks.registering(Jar::class) {
  206. dependsOn(JavaPlugin.COMPILE_TEST_JAVA_TASK_NAME)
  207. archiveClassifier.set("tests")
  208. from(sourceSets["test"].output)
  209. }
  210. artifacts.add(configurations.create("testArtifacts").name, testsJar)
  211. tasks.test {
  212. testLogging {
  213. showStandardStreams = true
  214. events("passed", "skipped", "failed")
  215. }
  216. }
  217. {% endif -%}
  218. {%- if target.jar_source_set is defined %}
  219. {%- for source_set in target.jar_source_set -%}
  220. {%- set srcdir_glob = split(source_set, ':') %}
  221. sourceSets.main.java.srcDirs += "{{ srcdir_glob[0] }}"
  222. {%- endfor -%}
  223. {%- endif -%}
  224. {% for extra_target in extra_targets -%}
  225. {%- if extra_target.jar_source_set is defined -%}
  226. {%- for source_set in extra_target.jar_source_set -%}
  227. {%- set srcdir_glob = split(source_set, ':') %}
  228. sourceSets.main.java.srcDirs += "{{ srcdir_glob[0] }}"
  229. {%- endfor -%}
  230. {%- endif -%}
  231. {%- endfor -%}
  232. sourceSets {
  233. {%- if target.runs|length %}
  234. main {
  235. {#-
  236. Default by Gradle:
  237. java.srcDir("src/main/java")
  238. resources.srcDir("src/main/resources")
  239. #}
  240. {%- for run in target.runs -%}
  241. {{ OutDirs(run, ' java.srcDir("', '")') }}
  242. {%- endfor %}
  243. }
  244. {%- endif %}
  245. test {
  246. {#-
  247. Default by Gradle:
  248. java.srcDir("src/test/java")
  249. resources.srcDir("src/test/resources")
  250. #}
  251. java.srcDir("ut/java")
  252. resources.srcDir("ut/resources")
  253. java.srcDir("src/test-integration/java")
  254. resources.srcDir("src/test-integration/resources")
  255. java.srcDir("src/testFixtures/java")
  256. resources.srcDir("src/testFixtures/resources")
  257. java.srcDir("src/intTest/java")
  258. resources.srcDir("src/intTest/resources")
  259. {%- for extra_target in extra_targets -%}
  260. {%- if extra_target|length -%}
  261. {%- for run in extra_target.runs -%}
  262. {{ OutDirs(run, ' java.srcDir("', '")') }}
  263. {%- endfor -%}
  264. {%- endif -%}
  265. {%- endfor %}
  266. }
  267. }
  268. dependencies {
  269. {%- for library in target.consumer if library.classpath -%}
  270. {%- if library.prebuilt and (library.type != "contrib" or build_contribs) and ("contrib/java/com/google/errorprone/error_prone_annotations" in library.jar) -%}
  271. {%- set errorprone_version = library.jar|replace("contrib/java/com/google/errorprone/error_prone_annotations/", "") -%}
  272. {%- set errorprone_parts = split(errorprone_version, '/', 2) %}
  273. errorprone("com.google.errorprone:error_prone_core:{{ errorprone_parts[0] }}")
  274. {%- endif -%}
  275. {%- if library.prebuilt and library.jar and (library.type != "contrib" or build_contribs) %}
  276. implementation(files("$project_root/{{ library.jar }}"))
  277. {%- else -%}
  278. {%- set classpath = library.classpath -%}
  279. {%- if classpath|replace('"','') == classpath -%}
  280. {%- set classpath = '"' + classpath + '"' -%}
  281. {%- endif -%}
  282. {%- if library.type != "contrib" %}
  283. {%- if library.testdep %}
  284. implementation(project(path = ":{{ library.testdep | replace("/", ":") }}", configuration = "testArtifacts"))
  285. {%- else %}
  286. implementation({{ classpath }})
  287. {%- endif -%}
  288. {%- else %}
  289. api({{ classpath }})
  290. {%- endif -%}
  291. {%- if library.excludes.consumer is defined %} {
  292. {% for exclude in library.excludes.consumer if exclude.classpath -%}
  293. {%- set classpath = exclude.classpath|replace('"','') -%}
  294. {%- set classpath_parts = split(classpath, ':') -%}
  295. exclude(group = "{{ classpath_parts[0] }}", module = "{{ classpath_parts[1] }}")
  296. {% endfor -%}
  297. }
  298. {%- endif -%}
  299. {%- endif -%}
  300. {%- endfor -%}
  301. {%- for annotation_processor in target.use_annotation_processor %}
  302. annotationProcessor(files("$project_root/{{ annotation_processor}}"))
  303. {%- endfor -%}
  304. {%- for extra_target in extra_targets -%}
  305. {%- for library in extra_target.consumer if library.classpath -%}
  306. {%- if library.prebuilt and library.jar and (library.type != "contrib" or build_contribs) %}
  307. testImplementation(files("$project_root/{{ library.jar }}"))
  308. {%- else -%}
  309. {%- set classpath = library.classpath -%}
  310. {%- if classpath|replace('"','') == classpath -%}
  311. {%- set classpath = '"' + classpath + '"' -%}
  312. {%- endif %}
  313. {%- if library.type != "contrib" and library.testdep %}
  314. testImplementation(project(path = ":{{ library.testdep | replace("/", ":") }}", configuration = "testArtifacts"))
  315. {%- else %}
  316. testImplementation({{ classpath }})
  317. {%- endif -%}
  318. {%- if library.excludes.consumer is defined %} {
  319. {% for exclude in library.excludes.consumer if exclude.classpath -%}
  320. {%- set classpath = exclude.classpath|replace('"','') -%}
  321. {%- set classpath_parts = split(classpath, ':') -%}
  322. exclude(group = "{{ classpath_parts[0] }}", module = "{{ classpath_parts[1] }}")
  323. {% endfor -%}
  324. }
  325. {%- endif -%}
  326. {%- endif -%}
  327. {%- endfor -%}
  328. {%- for annotation_processor in extra_target.use_annotation_processor %}
  329. testAnnotationProcessor(files("$project_root/{{ annotation_processor}}"))
  330. {%- endfor -%}
  331. {%- endfor %}
  332. }
  333. {%- if hasJunit5Test %}
  334. tasks.named<Test>("test") {
  335. useJUnitPlatform()
  336. }
  337. {% endif -%}
  338. {#- run_java_program -#}
  339. {%- if target.runs|length -%}
  340. {%- for run in target.runs %}
  341. val runJav{{ loop.index }} = task<JavaExec>("runJavaProgram{{ loop.index }}") {
  342. group = "build"
  343. description = "Code generation by run java program"
  344. {%- if run.classpath|length %}
  345. {% for classpath in run.classpath -%}
  346. {%- set rel_file_classpath = classpath|replace('@', '')|replace(export_root, '')|replace(arcadia_root, '') %}
  347. val classpaths = "$project_root/" + File("$project_root{{ rel_file_classpath }}").readText().trim().replace(":", ":$project_root/")
  348. classpath = files(classpaths.split(":"))
  349. {%- endfor -%}
  350. {% else %}
  351. classpath = sourceSets.main.get().runtimeClasspath
  352. {%- endif %}
  353. mainClass.set("{{ run.args[0] }}")
  354. {%- if run.args|length > 1 %}
  355. args = listOf(
  356. {%- for arg in run.args -%}
  357. {%- if not loop.first %}
  358. "{{ arg }}",
  359. {%- endif -%}
  360. {%- endfor %}
  361. )
  362. {% endif -%}
  363. {%- if run.in_dir %}
  364. {% for in_dir in run.in_dir -%}
  365. inputs.files(fileTree("{{ in_dir }}"))
  366. {% endfor -%}
  367. {%- endif -%}
  368. {%- if run.in %}
  369. {% for in_file in run.in -%}
  370. inputs.files("{{ in_file }}")
  371. {% endfor -%}
  372. {%- endif -%}
  373. {{ OutDirs(run, ' outputs.dir("', '")') }}
  374. {#
  375. Не использованы аттрибуты
  376. run-cwd="str"
  377. run-in_dirs_inputs="list"
  378. run-in_noparse="list"
  379. run-tool="list"
  380. -#}
  381. }
  382. tasks.getByName("sourcesJar").dependsOn(runJav{{ loop.index }})
  383. tasks.compileJava.configure {
  384. dependsOn(runJav{{ loop.index }})
  385. }
  386. {% endfor -%}
  387. {% endif -%}
  388. {% include "extra-tests.gradle.kts" ignore missing %}
  389. {% if publish -%}
  390. {% include 'publish.gradle.kts' ignore missing -%}
  391. {% endif -%}
  392. {#- To disable redundant javadoc (it may fail the build) #}
  393. tasks.withType<Javadoc>().configureEach {
  394. isEnabled = false
  395. }
  396. {%- include "[generator]/debug.jinja" ignore missing -%}