build.gradle.kts.jinja 11 KB

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